Note that there are some explanatory texts on larger screens.

plurals
  1. POResponding to Volley JSONException
    primarykey
    data
    text
    <p>I have a Volley JSONRequest that is getting stuck because the response from the server is not a valid JSON (it's html).</p> <p>How is it possible to gracefully handle this error? I have this implementation:</p> <pre><code>JsonObjectRequest jsObjRequest = new JsonObjectRequest(Request.Method.GET, url, null, new Response.Listener&lt;JSONObject&gt;() { @Override public void onResponse(JSONObject response) { … something … } }, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError error) { // TODO Auto-generated method stub Log.i(TAG,"error "+error.toString()); } }); queue.add(jsObjRequest); </code></pre> <p>However when i send the request and get a not valid JSON response, the onErrorResponse doesn't get called. Instead i get an exception </p> <pre><code>08-14 16:26:15.590: D/538 - JsonRequest(27888): exception error Value &lt;!DOCTYPE of type java.lang.String cannot be converted to JSONObject </code></pre> <p>and the whole app crashes.</p> <p>I managed to track that error to the following line in the JsonObjectRequest.java class:</p> <pre><code> @Override protected Response&lt;JSONObject&gt; parseNetworkResponse(NetworkResponse response) { try { Log.d(TAG,"response headers "+response.headers.toString()); String jsonString = new String(response.data, HttpHeaderParser.parseCharset(response.headers)); Log.d(TAG,"json string "+jsonString); return Response.success(new JSONObject(jsonString), HttpHeaderParser.parseCacheHeaders(response)); } catch (UnsupportedEncodingException e) { Log.d(TAG,"parse error"); return Response.error(new ParseError(e)); } catch (JSONException je) { Log.d(TAG,"exception error "+je.getLocalizedMessage()); return Response.error(new ParseError(je)); } } </code></pre> <p>the line that gets called is : <strong>return Response.error(new ParseError(je));</strong></p> <p>How do i catch that so it doesn't crash the app? </p> <p>Thanks in advance!</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload