Starting with API java SDK
Relatively new to this, and I'm struggling to get past the "Unhandled exception type SmartsheetException" in a pretty simple API GET using the java SDK for Smartsheet API. The following is basically copied from resources I've found online and I think I'm missing something really simple. It's throwing the exception at the "a.sheetResources()..." line. Any help would be greatly appreciated!
import com.smartsheet.api.*;
import com.smartsheet.api.models.*;
import com.smartsheet.api.oauth.*;
import java.io.FileInputStream;
import java.util.*;
public class Test
{public static void main(String[] args)
{String accessToken = "---";
Smartsheet a = SmartsheetFactory.createDefaultClient(accessToken);
PagedResult<Sheet> b = a.sheetResources().listSheets(null, null, null);
System.out.println(b);}}
Answers
-
I use the Javascript and Python SDK's, so I can't offer Java specific help. But here is what I would suggest:
- Start with the example usage on the Java SDK GitHub page
- Next try their minimal sample app java-read-write-sheet
If the error you are getting with your above code has any more details, it would be useful to post them to help us debug. If you are not getting more detailed errors, you could look at the Java SDK Advanced section to see about increasing the logging level.
-
unfortunately that's all it's giving me, I'll try that and update.
-
Ok so this is copied from their example usage on the Java SDK GitHub page with the tracing added and correct access token, of course.
import com.smartsheet.api.*;
import com.smartsheet.api.models.*;
import com.smartsheet.api.oauth.*;
import java.io.FileInputStream;
import java.util.*;
public class Test
{static {System.setProperty("Smartsheet.trace.parts", "RequestBodySummary, ResponseBodySummary");
System.setProperty("Smartsheet.trace.pretty", "true");}
public static void main(String[] args)
{String accessToken = Constants.accessToken;
Smartsheet smartsheet = SmartsheetFactory.createDefaultClient(accessToken);
PagedResult<Sheet> sheets = smartsheet.sheetResources().listSheets(null, null, null);
System.out.println("Found " + sheets.getTotalCount() + " sheets");}}
Still getting an "Unhandled exception type SmartsheetException" when debugging. I've attached a screenshot if that helps clarify anything.
-
Hi @Formliner,
The issue you're encountering with the "Unhandled exception type SmartsheetException" in your Java code when using the Smartsheet API is due to the fact that the SDK methods you're calling can throw a
SmartsheetException
, which is a checked exception. Java requires that checked exceptions be either caught or declared to be thrown by the method.In your case, the line
PagedResult<Sheet> b = a.sheetResources().listSheets(null, null, null);
is the one throwing theSmartsheetException
. To resolve this issue, you can use a try-catch block to catch the exception and handle it appropriately, or declare that yourmain
method throws this exception (although the latter is less common for handling exceptions in real-world applications).Here's how you can modify your code to include a try-catch block:
import com.smartsheet.api.*; import com.smartsheet.api.models.*; import com.smartsheet.api.oauth.*; import java.io.FileInputStream; import java.util.*; public class Test { public static void main(String[] args) { String accessToken = "---"; try { Smartsheet a = SmartsheetFactory.createDefaultClient(accessToken); PagedResult<Sheet> b = a.sheetResources().listSheets(null, null, null); System.out.println(b); } catch (SmartsheetException e) { e.printStackTrace(); // Prints the stack trace of the SmartsheetException } } }
This modification uses a try-catch block to catch the
SmartsheetException
. If an exception is thrown by thelistSheets
method, the catch block will catch it, and the stack trace of the exception will be printed to the standard error stream. This way, your program can handle errors gracefully without crashing unexpectedly.Make sure you replace
"---"
with your actual access token when you're testing this code.This approach is generally recommended for handling exceptions because it allows your program to continue running and handle the error, such as by logging it, performing some cleanup, or even attempting a recovery operation, depending on the context and the nature of the application you're developing.
bassam.khalil2009@gmail.com
☑️ Are you satisfied with my answer to your question? Please help the Community by marking it as an ( Accepted Answer), and I will be grateful for your "Vote Up" or "Insightful"
Categories
- All Categories
- 14 Welcome to the Community
- Smartsheet Customer Resources
- 64.2K Get Help
- 419 Global Discussions
- 221 Industry Talk
- 461 Announcements
- 4.8K Ideas & Feature Requests
- 143 Brandfolder
- 141 Just for fun
- 58 Community Job Board
- 462 Show & Tell
- 32 Member Spotlight
- 1 SmartStories
- 299 Events
- 38 Webinars
- 7.3K Forum Archives