Skip to content Skip to sidebar Skip to footer

How To Handle String Response From Php Using Android Volley Jsonobjectrequest [com.android.volley.parseerror: Org.json.jsonexception]?

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 therefor

Solution 1:

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);

Post a Comment for "How To Handle String Response From Php Using Android Volley Jsonobjectrequest [com.android.volley.parseerror: Org.json.jsonexception]?"