question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Android warning response.body().string();

See original GitHub issue

Hello.

I am getting this warning in this code: myString = response0.body().string();

Method invocation ‘string’ may produce ‘java.lang.NullPointerException’

The problem appears in string(). This code is inside try-catch.

How can I solve this safety? I searched in StackOverflow but I found nothing.

The app is working perfectly, it is just a warning.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:6
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

4reactions
JakeWhartoncommented, Oct 1, 2017

The warning can be suppressed for a normal response. The body is only null for a cached response or network response.

3reactions
AhmadullahSaikatcommented, Sep 6, 2018

replace response.body().string() with String successResponse = new Gson().toJson(response.body());

full code:

call.enqueue(new Callback<Object>() {
        @Override
        public void onResponse(@NonNull Call<Object> call, @NonNull Response<Object> response) {
            if (response.isSuccessful()) {
                Gson gson = new Gson();
                String successResponse = gson.toJson(response.body());
                Log.d(LOG_TAG, "successResponse: " + successResponse);
            } else {
                try {
                    if (null != response.errorBody()) {
                        String errorResponse = response.errorBody().string();
                        Log.d(LOG_TAG, "errorResponse: " + errorResponse);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }

        @Override
        public void onFailure(@NonNull Call<Object> call, @NonNull Throwable t) {
        }
    });

Read more comments on GitHub >

github_iconTop Results From Across the Web

Retrofit response.errorBody.string() gives me warning of ...
When I do response.errorBody()?.string() , Android Studio gives me a warning saying "inappropriate blocking method call".
Read more >
okhttp3.ResponseBody.source java code examples - Tabnine
string. Returns the response as a string decoded with the charset of the Content-Type header. · byteStream · contentType · contentLength. Returns the...
Read more >
5.4.1 release causes okhttp error response body to be ...
I.e. calling response.body().string() in the onResponse callback ... a new version of the Android agent 5.5.0 has been released with a fix.
Read more >
ResponseBody - OkHttp - Square Open Source
bytes(); Response.body().string(). There is no benefit to invoking multiple close() methods for the same response body. For synchronous calls ...
Read more >
okhttp responsebody can not be converted to a string. Please ...
newCall(request); call.enqueue(new Callback() { @Override public void ... "Check Log " + response.body().string()); String jsonData ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found