0

Actually when we call API and send request in JSON format we are expecting response also come into JSON format. But here back end team sending me response in String format therefore my onErrorResponse () method get called. Here my status code is 200. But due to format of response not executed onResponse () method. So will you please help me to handle this? Might be I have to use CustomRequest here. Any suggestoin will be appreciated. Thanks

I have used volley json object request for accessing server.

My Request format is:

JSONObject json1 = new JSONObject();

try{
json1.put("","");
}catch(JSONException e){
e.printStackTrace();
}

new SampleJsonObjTask(MainActivity.this, json1);

and my SampleJsonObjTask class is:

public class SampleJsonObjTask {
    public static ProgressDialog progress;
    private static RequestQueue queue;
    JSONObject main;
    JsonObjectRequest req;
    private MainActivity context;
    public SampleJsonObjTask(MainActivity context, JSONObject main) {

        progress = new ProgressDialog(context);
        progress.setMessage("Loading...");
        progress.setCanceledOnTouchOutside(false);
        progress.setCancelable(false);
        progress.show();
        this.context = context;
        this.main = main;
        ResponseTask();
    }

    private void ResponseTask() {
        if (queue == null) {
            queue = Volley.newRequestQueue(context);
        }
        req = new JsonObjectRequest(Request.Method.POST, "", main,
                new Response.Listener<JSONObject>() {
                    @Override
                    public void onResponse(JSONObject response) {
                        progress.dismiss();
                        Log.e("response","response--->"+response.toString());

                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Log.e("error", "error--->" + error.toString());
                NetworkResponse response = error.networkResponse;
                Log.e("statusCode","statusCode--->"+response.statusCode);
                String json = null;
                if (response != null && response.data != null) {
                    switch (response.statusCode) {
                        case 400:
                            break;

                        case 401:
                            json = new String(response.data);
                            break;
                        case 502:
                            json = new String(response.data);
                            break;
                        case 200:
                            json = new String(response.data);
                            break;
                    }
                    progress.dismiss();
                }
            }
        })

        {
            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                Map<String, String> params = new HashMap<String, String>();
                params.put("Content-Type", "application/json");
                return params;
            }
        };
        req.setRetryPolicy(new DefaultRetryPolicy(20 * 1000, 0, 1f));
        queue.add(req);

    }}

Here the Response coming like string format that is Value OK, please help me to how to get the string response which i have mentioned below.

com.android.volley.ParseError: org.json.JSONException: Value OK of type java.lang.String cannot be converted to JSONObject

How to get this string response by using json object request.

karthik
  • 321
  • 1
  • 8
  • 21

1 Answers1

0
RequestQueue queue = Volley.newRequestQueue(context);
    StringRequest stringRequest = new StringRequest(Request.Method.POST, url,
            new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {
                    try {
                        dialog.cancel();
                        Log.d("response", response);
                    }catch (Exception e){
                        volleyResponseCallBack.onFailure("Something went wrong!");
                    }
                }
            },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError volleyError) {
                    String message = null;
                    dialog.cancel();

                    if (volleyError instanceof NetworkError) {
                        message = "Cannot connect to Internet...Please check your connection!";
                    } else if (volleyError instanceof ServerError) {
                        message = "The server could not be found. Please try again after some time!!";
                    } else if (volleyError instanceof AuthFailureError) {
                        message = "Cannot connect to Internet...Please check your connection!";
                    } else if (volleyError instanceof ParseError) {
                        message = "Parsing error! Please try again after some time!!";
                    } else if (volleyError instanceof NoConnectionError) {
                        message = "Cannot connect to Internet...Please check your connection!";
                    } else if (volleyError instanceof TimeoutError) {
                        message = "Connection TimeOut! Please check your internet connection.";
                    }

                    if (!(message == null)) {
                        Log.d("response", message);
                    }
                }
            }) {
        @Override
        protected Map<String, String> getParams() throws AuthFailureError {
            Map<String, String> params = new HashMap<String, String>();
            params.put("Content-Type", "application/json");
            return params;
        }
    };


    stringRequest.setRetryPolicy(new DefaultRetryPolicy(
            0,
            DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
            DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
    queue.add(stringRequest);
  • Is there any possbile to handle by using JsonObjectRequest reponse is which i given below. without using stringRequest ? com.android.volley.ParseError: org.json.JSONException: Value OK of type java.lang.String cannot be converted to JSONObject – karthik May 14 '18 at 10:47
  • log the response from server, what are you getting from server, because it is JSON parsing exception,. first print the resposnse what are you getting? – Dhananjay Gupta May 14 '18 at 10:57
  • Yes i have print the log the reponse is org.json.JSONException: Value OK of type java.lang.String cannot be converted to JSONObject – karthik May 14 '18 at 11:09
  • Actually when we call API and send request in JSON format we are expecting response also come into JSON format. But here back end team sending me response in String format therefore my onErrorResponse () method get called. Here my status code is 200. But due to format of response not executed onResponse () method. So will you please help me to handle this? Might be I have to use CustomRequest here. Any suggestoin will be appreceiated. Thanks – karthik May 14 '18 at 11:57