Problem statement:
I am trying to access an REST API that will return a JSON object for various HTTP status codes (400, 403, 200 etc) using Volley.
For any HTTP status other than 200, it seems the 'Unexpected response code 400' is a problem. Does anyone have a way to bypass this 'error'?
Code:
protected void getLogin() { final String mURL = "https://somesite.com/api/login"; EditText username = (EditText) findViewById(R.id.username); EditText password = (EditText) findViewById(R.id.password); // Post params to be sent to the server HashMap<String, String> params = new HashMap<String, String>(); params.put("username", username.getText().toString()); params.put("password", password.getText().toString()); JsonObjectRequest req = new JsonObjectRequest(mURL, new JSONObject( params), new Response.Listener<JSONObject>() { @Override public void onResponse(JSONObject response) { try { JSONObject obj = response .getJSONObject("some_json_obj"); Log.w("myApp", "status code..." + obj.getString("name")); // VolleyLog.v("Response:%n %s", response.toString(4)); } catch (JSONException e) { e.printStackTrace(); } } }, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError error) { Log.w("error in response", "Error: " + error.getMessage()); } }); // add the request object to the queue to be executed AppController.getInstance().addToRequestQueue(req); }
One way of doing this without changing Volley
's source code is to check for the response data in the VolleyError
and parse it your self.
As of f605da3 commit
, Volley
throws a ServerError
exception that contains the raw network response.
So you can do something similar to this in your error listener:
/* import com.android.volley.toolbox.HttpHeaderParser; */ public void onErrorResponse(VolleyError error) { // As of f605da3 the following should work NetworkResponse response = error.networkResponse; if (error instanceof ServerError && response != null) { try { String res = new String(response.data, HttpHeaderParser.parseCharset(response.headers, "utf-8")); // Now you can use any deserializer to make sense of data JSONObject obj = new JSONObject(res); } catch (UnsupportedEncodingException e1) { // Couldn't properly decode data to string e1.printStackTrace(); } catch (JSONException e2) { // returned data is not JSONObject? e2.printStackTrace(); } } }
For future, if Volley
changes, one can follow the above approach where you need to check the VolleyError
for raw data that has been sent by the server and parse it.
I hope that they implement that TODO
mentioned in the source file.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With