Using Android Volley to Request and Response Json as a String

I have a situation where I use the Android-volleyto POSTmy json object , I was able to successfully publish all the contents, and my data is visible on the server, but the server responds like Stringno way json, this is the reason I get the error.

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

So can volley parse strings when we pass json objects? My working code is below,

            HashMap<String, String> params = new HashMap<String, String>();
    params.put("email", "dude@gmail.com");
    params.put("password", "qweffrty");
    params.put("name", "Dudeb");
    params.put("place", "Bangalore");
    params.put("phone", "991000000000");

    JsonObjectRequest request = new JsonObjectRequest(
            Request.Method.POST,
            Constants.BASE_URL+"register",
            new JSONObject(params),
            createSuccessListener(),
            createErrorListener());


         private static Response.Listener<JSONObject> createSuccessListener() {
    return new Response.Listener<JSONObject>() {
        @Override
        public void onResponse(JSONObject response) {
            // TODO parse response

            String test;
            test = "yyy";
        }
    };
}

private static Response.ErrorListener createErrorListener() {
    return new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
          //  Log.d(TAG, "Error Response code: " + error.getMessage());
            String test;
            test = "yyy";
        }
    };
}
+4
source share
1 answer

No, because look at these lines in the implementation, in JsonRequest we have:

public abstract class JsonRequest<T> extends Request<T>
...
abstract protected Response<T> parseNetworkResponse(NetworkResponse response);

and in JsonObjectRequest:

public class JsonObjectRequest extends JsonRequest<JSONObject>

and look at the response response in JsonObjectRequest:

protected Response<JSONObject> parseNetworkResponse(NetworkResponse response)

, JsonObjectRequest , json json. StringRequest.

+1

Source: https://habr.com/ru/post/1537334/


All Articles