android volley JsonObjectRequest which returns no body just 200
Asked Answered
L

4

7

How to make a JSON request in volley where I need to send an authentification header and JSON object in body and I expect only an status code 200 answer

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 kinds of response listeners with string or JSON object, object, but always there is an error: android volley org.json.JSONException: End of input at character 0 of

Or is there any other kind of request in volley which supports and json object and authentification header in body and response is just a http status code ?

Livy answered 19/11, 2013 at 15:34 Comment(0)
D
7

I know its an old question but i thought i still answer this as my answer might help people in future -

You need to create a custom class that extends Request class and in that class override these methods

    @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 the class.

For full code and explanation check out my blog on the topic -

Getting a server response status 200 from Android Volley library

link 1

Hope it helps, Thanks

Decoder answered 6/6, 2014 at 2:7 Comment(1)
answering old questions ftw!Clubwoman
C
5

Thanks to Varundroid and Itai Hanski. It's correct, you just need to subclass the JsonObjectRequest.

For convenience here's my override:

public class MyJsonObjectRequest extends JsonObjectRequest { 
    @Override
    protected Response<JSONObject> parseNetworkResponse(NetworkResponse response) {
        try {
            String jsonString = new String(response.data, HttpHeaderParser.parseCharset(response.headers));
            // here's 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 that instead of JsonObjectRequest. Not too pretty, not too ugly.

Clubwoman answered 12/12, 2014 at 8:50 Comment(0)
G
2

The JsonObjectRequest is used when you expect the network response to be a JSON object. Since you're not expecting that, you shouldn't be using it.

Volley comes with a few predefined popular types of requests for ease of use, but you can always create your own. I suggest making a custom request based on JsonObjectRequest, but with a different implementation of the parseNetworkResponse() method that does not expect the response to be a JSON object, since you aren't receiving one. You can add any other changes you need there.

Gottfried answered 20/11, 2013 at 8:0 Comment(0)
W
2

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

Following is the code I'm using to overcome the same problem you are facing. I hope this helps you 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);
}
Wester answered 29/9, 2016 at 8:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.