Reputation:
So I'm not really having a problem but I do have a question about how-to debug the proper way. For example, in my case I have method inside Global class.
When I run this from another class I'd have to surround it with a try/catch. However, when something fails inside my code it just shows me the "throw". All the other Log.*/System.out.println() won't show.
public <T> T run(Class<T> a, String url, File file, String jsonObject, String contentType) throws Exception {
if(file.exists()){
requestBody = new MultipartBody.Builder()
.setType(MultipartBody.FORM)
.addFormDataPart("jsonObject", jsonObject)
.addFormDataPart("image", file.getName(), RequestBody.create(MediaType.parse(contentType), file))
.build();
} else{
requestBody = new MultipartBody.Builder()
.setType(MultipartBody.FORM)
.addFormDataPart("jsonObject", jsonObject)
.build();
}
Request request = new Request.Builder()
.url(API_SERVER + url)
.post(requestBody)
.build();
System.out.println
Response response = client.newCall(request).execute();
if (response.isSuccessful()) throw new IllegalArgumentException("!response.isSuccessful():" + response.body().string());
String jsonResponse = response.body().string();
response.body().close();
Gson gson = new Gson();
return gson.fromJson(jsonResponse, a);
}
The only way to see anything in my case is to change the if-statement.
if (!response.isSuccessful()) throw new IllegalArgumentException("!response.isSuccessful():" + response.body().string());
What could you guys recommend me to change so I can debug a little easier instead of replacing my if, waiting for rebuilding, deploying and testing...
Upvotes: 1
Views: 69
Reputation: 2165
The class you have defined Throws
an exception
which means if there is any exception within this method it will be thrown back in the stack from where the method has been called.
You need to handle the thrown exception in try
& catch
blocks and you will receive the thrown exception in your catch
block where you can test the details of exception and accordingly take the next step on the code to run.
I think Log.e will print the exceptions in your debug console.
http://developer.android.com/reference/android/util/Log.html
Upvotes: 1
Reputation: 1582
If your response is successful why should it throw an error?
You are only seeing things on the second if because your response is always unsuccessful which seems the right way in my opinion to throw the error only in that case.
Upvotes: 1