Android volley JsonObjectRequest, which does not return a single body of only 200

How to make a JSON request in a salvo, where I need to send an authentication header and a JSON object in the body, and I expect only 200 status code to respond

JsonObjectRequest request = new JsonObjectRequest( method, url, myJsonObject, responseListener, errorListener) { @Override public Map<String, String> getHeaders() throws AuthFailureError { HashMap<String, String> headers = new HashMap<String, String>(); String creds = String.format("%s:%s", login, password); String auth = "Basic " + Base64.encodeToString(creds.getBytes(), Base64.DEFAULT); headers.put("Authorization", auth); return headers; } }; new Response.Listener<String>(){ @Override public void onResponse(String response) { Toast.makeText(getActivity(), response, 1000).show(); } 

I tried different types of response listeners with a string or JSON object, object, but there is always an error: android volley org.json.JSONException: End of character input 0 from

Or is there any other request in volley that supports both the json object and the authentication header in the body and the response is just an http status code?

+6
source share
4 answers

I know his old question, but I thought I was still answering it, as my answer may help people in the future -

You need to create your own class that extends the Request class and overrides these methods in this class

  @Override protected Response parseNetworkResponse(NetworkResponse response) { return Response.success(response.statusCode, HttpHeaderParser.parseCacheHeaders(response)); } @Override protected void deliverResponse(Integer statusCode) { mListener.onResponse(statusCode); } 

This is the heart and soul of this class.

For a complete code and explanation, check out my block on the topic -

Getting server 200 response status from Volley Android library

reference 1

Hope this helps, Thanks

+6
source

Thanks to Varundroid and Ita Hansky. That's right, you just need to subclass JsonObjectRequest.

For convenience, here is my redefinition:

 public class MyJsonObjectRequest extends JsonObjectRequest { @Override protected Response<JSONObject> parseNetworkResponse(NetworkResponse response) { try { String jsonString = new String(response.data, HttpHeaderParser.parseCharset(response.headers)); // here the new code, if jsonString.length() == 0 don't parse if (jsonString.length() == 0) { return Response.success(null, HttpHeaderParser.parseCacheHeaders(response)); } // end of patch return Response.success(new JSONObject(jsonString), HttpHeaderParser.parseCacheHeaders(response)); } catch (UnsupportedEncodingException e) { return Response.error(new ParseError(e)); } catch (JSONException je) { return Response.error(new ParseError(je)); } } } 

So I just use this instead of JsonObjectRequest . Not too pretty, not too ugly.

+4
source

JsonObjectRequest used if you expect the network response to be a JSON object. Since you do not expect this, you should not use it.

Volley comes with several predefined popular request types for ease of use, but you can always create your own. I suggest creating a custom request based on JsonObjectRequest , but with a different implementation of the parseNetworkResponse() method, which does not expect the response to be a JSON object since you are not receiving it. You can add any other changes you need there.

+2
source

No need to create a custom class that extends Request as @varundroid. You can simply override the parseNetworkResponse method instead of the onResponse method and return a success response with empty JSONObject ().

Below is the code that I use to solve the same problem you are facing. I hope this helps too.

 @Override protected Response<JSONObject> parseNetworkResponse(NetworkResponse response) { if (response != null && response.data.length == 0) return Response.success(new JSONObject(), null); else return super.parseNetworkResponse(response); } 
+1
source

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


All Articles