0

I am getting the following error from a webapi call in an Android app using volley:

org.json.JSONException: Value <!DOCTYPE of type java.lang.String cannot be converted to JSONObject

And I want to know how to see the exact message from the server as opposed to the Volley error. Here is the code:

    JsonObjectRequest request = new JsonObjectRequest(Request.Method.POST, url, null, new Response.Listener<JSONObject>() {
        @Override
        public void onResponse(JSONObject response) {
            Log.d(TAG, "onResponse: " + response.toString());
        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {

            if (error instanceof ParseError) {
                Log.d(TAG, "onErrorResponse: ParseError: ");

            }
        }
    });

    queue.add(request);

It is a really basic call and under most circumstances I get back the jsonobject, but in those cases where the server sends me something different I want to be able to account for it. The error which is thrown is parsing of a value which is not json because it is an error. How can I see what the server is sending from the ErrorListener?

lcj
  • 1,355
  • 16
  • 37
  • It sends a html document. – blackapps Feb 17 '21 at 22:50
  • Do not use JsonObjectRequest but something general so you get what is sent. If it is json you can still handle it as json. – blackapps Feb 17 '21 at 22:52
  • The JsonObjectRequest is needed if I am sending json data in the post, correct? This is what I intend to do. I could get the return as a string but that's not supported in JsonObjectRequest, correct? Otherwise I would like to see what was the server returned in the Response.ErrorListener. – lcj Feb 18 '21 at 13:28
  • I do not know if JsonObjectRequest assumes you send json or that you expect json back. Or both. Please find out. – blackapps Feb 18 '21 at 13:32
  • Looks like the JsonObjectRequest gets back a JsonObject and the StringRequest gets back a string (https://developer.android.com/training/volley/request) it's just that the StringRequest does not have a JSONObject request body parameter which maps to the post fields. Instead you need to override a protected getParams method (https://gist.github.com/mstfldmr/f6594b2337e3633673e5 and https://stackoverflow.com/questions/16626032/volley-post-get-parameters). So if I want to get a string back from a webapi call prior to turning it into json I need to use the StringRequest object, override getParams – lcj Feb 18 '21 at 17:40
  • That feels a little kludgy, as opposed to using a JsonObjectRequest and getting detail if there is a ParseError. – lcj Feb 18 '21 at 19:22
  • Sorry, i cannot test this for you. – blackapps Feb 18 '21 at 20:03

0 Answers0