5

I am doing some REST calls, and getting the response. Some times, the response will not be ok, so I need to parse the response, and present it in a proper way for the user to notice, and react. I have this code:

if(response.code()!=200){
      JSONArray jsonError=null;
      try {
         Log.i("David", "Incorrect response: "+response.errorBody().string());

         jsonError = new JSONArray(response.errorBody().string());//After this line, jsonError is null
         JSONObject message=jsonError.getJSONObject(0);

         String errorJson=message.getString("message");
         Log.i("David", "Error received: "+errorJson);
         AlertDialog.Builder dialog=new AlertDialog.Builder(getActivity());
         dialog.setTitle(getString(R.string.error));
         dialog.setMessage(getString(R.string.errorfound) + errorJson);
         dialog.setPositiveButton(getString(R.string.aceptar), new DialogInterface.OnClickListener() {
             @Override
             public void onClick(DialogInterface dialog, int which) {
                    dialog.dismiss();
              }
                  });
                            dialog.show();
                        } catch (IOException e) {
                            e.printStackTrace();
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }

                    }

This way, the logging line "Incorrect response" shows perfectly, with error code, message, etc. This is the JSON received:

[{"coderror":"-2","gravedad":"1","message":"Device imei 34543564768463435345 not logged in","cause":""}]

My problem is, as I said, the logging works ok, but when I try to convert the JSON to a JsonArray...it fails. JsonError object turns out to be null, in the very next line.

Why can't I parse the response? Thank you.

BNK
  • 23,994
  • 8
  • 77
  • 87
Fustigador
  • 6,339
  • 12
  • 59
  • 115
  • 3
    You should use `String s = response.errorBody().string();` then `Log.i("David", "Incorrect response: "+ s;` and `jsonError = new JSONArray(s);`. Don't call `response.errorBody().string()` twice – BNK Nov 11 '15 at 07:53
  • Is it set to null once i access it, or something like that? – Fustigador Nov 11 '15 at 07:54
  • 1
    Because you have called `response.errorBody().string()` in Log.i(...) line, when you call `response.errorBody().string()` again in new JsonArray(...), it will be null – BNK Nov 11 '15 at 07:56
  • It is working the way you said. Didn't know the response object get null, once you access it. Would like to know why... – Fustigador Nov 11 '15 at 07:59
  • 1
    Inside `string()`, it calls `bytes()`, you can debug it to see more details when it called twice. I have another answer at http://stackoverflow.com/questions/33089581/what-is-the-reason-of-this-error-java-io-ioexception-content-length-and-stream/33092586#33092586, which may not be exactly the same as yours, however I think it nearly :) – BNK Nov 11 '15 at 08:05
  • 2
    Thank you very much for your help :) – Fustigador Nov 11 '15 at 08:35

1 Answers1

13

As I have commented, you should use String s = response.errorBody().string(); then Log.i("David", "Incorrect response: "+ s; and jsonError = new JSONArray(s);. Don't call response.errorBody().string() twice.

If you try/catch IllegalStateException or Exception you will get info look like the following at logcat

W/System.err: java.lang.IllegalStateException: closed
W/System.err:     at com.squareup.okhttp.internal.http.HttpConnection$FixedLengthSource.read(HttpConnection.java:415)
W/System.err:     at okio.Buffer.writeAll(Buffer.java:956)
W/System.err:     at okio.RealBufferedSource.readByteArray(RealBufferedSource.java:92)
W/System.err:     at com.squareup.okhttp.ResponseBody.bytes(ResponseBody.java:57)
W/System.err:     at com.squareup.okhttp.ResponseBody.string(ResponseBody.java:83)

Hope it helps!

BNK
  • 23,994
  • 8
  • 77
  • 87