0

I use this code to make requests to a server, it usually works fine but sometimes it throws some errors since its under development, however if it throws an error volley tries to parse it into a JsonObject and will inevitably fail.

final JsonObjectRequest jsonObjectRequest = new JsonObjectRequest
                (Request.Method.POST, baseUrl, postparams, new Response.Listener<JSONObject>() {
                    @Override
                    public void onResponse(JSONObject response) {
                        Log.w("Response", response.toString());
                        callBackActivity.JsonCallback(response, "grupos");
                    }
                }, new Response.ErrorListener() {

                    @Override
                    public void onErrorResponse(VolleyError error) {
                        callBackActivity.ErrorCallback(error, "grupos");

                    }
                });

So is there any way to know whats the raw response of the server before parsing it into a JsonObject?

ivan
  • 1,177
  • 8
  • 23
  • You can switch to Retrofit and use `HttpLoggingInterceptor` [library](https://stackoverflow.com/questions/42835315/httplogginginterceptor-for-http-request-response-logging) that will Log every request and response, their times and headers. Or if Volley uses `OkHttp` too (IDK), no need to switch anything, just add the thing to the `OkHttp`. – Vucko May 31 '19 at 18:03
  • Will check it out, thanks for the advice. – ivan May 31 '19 at 18:27
  • what http response are you sending back to android client upon error? volley's error listener should be called upon any kind of error. – namu May 31 '19 at 20:42

1 Answers1

2

Instead of JSONObjecRequest try StringRequest which will give the response in string. Log this response and look what is causing the error, then you can revert it back to JSONObjectRequest

StringRequest stringRequest = new StringRequest(Request.Method.GET, url,
            new Response.Listener<String>() {
    @Override
    public void onResponse(String response) {
        Log.v(response)
        try {
                JSONObject object = new JSONObject(response);       
            }
            } catch (Exception e) {
                Log.v("exception is " + e.toString());
            }        }
}, new Response.ErrorListener() {
    @Override
    public void onErrorResponse(VolleyError error) {
        //error log
    }
});
Psypher
  • 10,717
  • 12
  • 59
  • 83
  • Thanks for the alternative but im currently looking for an option that can work for both scenarios, an error and a succesful connection. – ivan May 31 '19 at 18:27
  • You can use for successful case as well...you can convert the response to `JSONObject` and use the JSONObject....I do the same in my projects – Psypher Jun 04 '19 at 17:21
  • Ah, so the stringrequest accepts both jsons and plaintext and if inside the response i try to convert it and it fails then i can check what was the response, right? – ivan Jun 04 '19 at 17:33
  • Thats right...i have updated the answer for you to refer – Psypher Jun 04 '19 at 17:50
  • Yeah that should work out just fine, thanks for your input! – ivan Jun 04 '19 at 17:51