Arya
Arya

Reputation: 1747

org.json.JSONException: End of input at character 0 of in retrofit

I have created an Android Application and trying to update profile with data base.

 if (response.isSuccessful()) {
         Log.d("TAG", "response: "+response.body().string());

         String result = response.body().string();
         JSONObject jsonObj = new JSONObject(result);

         if(jsonObj.has("status")){

         if(jsonObj.get("status").equals("success")){

             Snackbar snackbar = Snackbar
             .make(layout, "User details successfully updated",
             Snackbar.LENGTH_LONG);

             snackbar.show();

     } else {
         Log.d("TAG","CLICK Failed");
         buttonVisible();
     }

When i tried to Log the response, I am getting TAG: response: {"status":"success","message":"User details successfully updated."} but when i tried to convert response.body().string(); to string I am getting this error**org.json.JSONException: End of input at character 0 of** . can you please tell me how I can solve this problem?

Upvotes: 0

Views: 1583

Answers (2)

Nikunj
Nikunj

Reputation: 4157

In Log print your string object like :

String result = response.body().string();
Log.d("TAG", "response: "+result);

Upvotes: 0

Pradeep Kumar Kushwaha
Pradeep Kumar Kushwaha

Reputation: 2239

Try to put

String result = response.body().string();

before

Log.d("TAG", "response: "+response.body().string());

Such that it looks like

String result = response.body().string();
Log.d("TAG", "response: "+result);

Just in case someone bumps into the same weird thing as I have. I run my code during development in Debug Mode and apparently since OKHttp 2.4

..the response body is a one-shot value that may be consumed only once

So when in debug there is a call "behind the scene" from the inspector and the body is always empty. See: http://square.github.io/okhttp/2.x/okhttp/com/squareup/okhttp/Response.html

Upvotes: 4

Related Questions