1

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...

2 Answers 2

1

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

Sign up to request clarification or add additional context in comments.

3 Comments

I do realise the throw giving my current exception. The problem, if I remove this throw it won't show me any log printed. No Log.*, no System.out.printl, nothing. I'm calling the method from another class where the try/catch is. If it comes inside the catch none of the log's will show.
I think Log.e will print the exceptions in your debug console. developer.android.com/reference/android/util/Log.html
Thanks for your input. This did the trick actually! Could you update your answer so I can accept it? :)
1

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.

2 Comments

Well,sometimes I want to add some code to a existing method on my API Server. When I print my $_POST data for example, I want to see what is printed instead of the exception in my catch block.
I understand, well you can use the debugger and just create a watch on the variable or write to the log using the command Log.w("post_data", post_data_var); Take a look at: developer.android.com/reference/android/util/Log.html

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.