Android Volley - BasicNetwork.performRequest: Unexpected response code 400
Asked Answered
G

9

28

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);
}
Geosynclinal answered 7/11, 2014 at 8:20 Comment(1)
I had the character "é" in my query string. Worked after I encoded all my parameters.Drummond
R
50

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.

Rosmunda answered 16/2, 2016 at 7:51 Comment(0)
C
15
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
    HashMap<String, String> headers = new HashMap<String, String>();
    headers.put("Content-Type", "application/json; charset=utf-8");
    return headers;
}

You need to add Content-Type to the header.

Conserve answered 24/11, 2014 at 13:19 Comment(3)
sending get request. api working fine but getting BasicNetwork.performRequest: Unexpected response code 500.Cocytus
@HemsLodha 500 - Internal Server Error "A generic error message, given when an unexpected condition was encountered and no more specific message is suitable" It's problem on server.Contort
Encountered similar error, but mine was "Unexpected response code 415". I had Content-Type application/json, but after adding charset=utf-8, the problem is gone.Andromede
F
11

Me too got the same error but in my case I was calling url with blank spaces.

Then, I fixed it by parsing like below.

String url = "Your URL Link";

url = url.replaceAll(" ", "%20");

StringRequest stringRequest = new StringRequest(Request.Method.GET, url,
                            new com.android.volley.Response.Listener<String>() {
                                @Override
                                public void onResponse(String response) {
                                ...
                                ...
                                ...
Fleshpots answered 24/12, 2017 at 16:54 Comment(2)
Thank you very much!! This solve my issue in Android 4.4.2Inference
This is a great answer. In new versions, the code was working ok, but older versions this has taken me for a big rollercoaster drive. Never expected that Android being based on Linux / Unix was not taking care of this earlier. Thank you @Rasool MohamedYingling
C
9

Try this ...

 StringRequest sr = new StringRequest(type,url, new Response.Listener<String>() {
        @Override
        public void onResponse(String response) {

            // valid response
        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            // error
        }
    }){

@Override
    protected Map<String,String> getParams(){
        Map<String,String> params = new HashMap<String, String>();
            params.put("username", username);
            params.put("password", password);
            params.put("grant_type", "password");
        return params;
    }

    @Override
    public Map<String, String> getHeaders() throws AuthFailureError {
        Map<String,String> params = new HashMap<String, String>();
        // Removed this line if you dont need it or Use application/json
        // params.put("Content-Type", "application/x-www-form-urlencoded");
        return params;
    }
Cuneiform answered 18/11, 2014 at 16:37 Comment(0)
K
2

You mean that want to get status codes?

VolleyError has a member variable type of NetworkResponse and it is public.

You can access error.networkResponse.statusCode for http error code.

I hope it is helpful for you.

Kismet answered 12/11, 2014 at 5:12 Comment(0)
D
2

What I did was append an extra '/' to my url, e.g.:

String url = "http://www.google.com" 

to

String url = "http://www.google.com/"
Dozier answered 7/6, 2018 at 20:35 Comment(1)
It is easy to miss the forward slash, and this helped direct me to find my mistake. Incorrect url being passed was issue.Indigestible
G
0

in my case, I was not writing reg_url with :8080 . String reg_url = "http://192.168.29.163:8080/register.php";

Gestate answered 2/8, 2016 at 16:2 Comment(1)
then what is solution for thisPentateuch
R
0

change

public static final String URL = "http://api-Location";

to

public static final String URL = "https://api-Location"

it's happen because i'm using 000webhostapp app

Risa answered 22/5, 2019 at 0:33 Comment(0)
G
-2

Just to update all, after some deliberations, I have decided to use Async Http Client instead to solve my earlier problem. The library allows a cleaner approach (to me) to manipulate HTTP responses especially in cases where JSON objects are returned in all scenarios/HTTP statuses.

protected void getLogin() {

    EditText username = (EditText) findViewById(R.id.username); 
    EditText password = (EditText) findViewById(R.id.password); 

    RequestParams params = new RequestParams();
    params.put("username", username.getText().toString());
    params.put("password", password.getText().toString());

    RestClient.post(getHost() + "api/v1/auth/login", params,
            new JsonHttpResponseHandler() {

        @Override
        public void onSuccess(int statusCode, Header[] headers,
                JSONObject response) {

            try {

                //process JSONObject obj 
                Log.w("myapp","success status code..." + statusCode);

            } catch (JSONException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

        @Override
        public void onFailure(int statusCode, Header[] headers,
                Throwable throwable, JSONObject errorResponse) {
            Log.w("myapp", "failure status code..." + statusCode);


            try {
                //process JSONObject obj
                Log.w("myapp", "error ..."  + errorResponse.getString("message").toString());
            } catch (JSONException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    });
}
Geosynclinal answered 19/1, 2015 at 6:11 Comment(1)
You should consider selecting answer of listboss as correct. It is the answer to your question, therefore with a reason it has highest points. Using another library is not a valid answer.Mudslinger

© 2022 - 2024 — McMap. All rights reserved.