可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
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 params = new HashMap(); params.put("username", username.getText().toString()); params.put("password", password.getText().toString()); JsonObjectRequest req = new JsonObjectRequest(mURL, new JSONObject( params), new Response.Listener() { @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); }
回答1:
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.
回答2:
@Override public Map getHeaders() throws AuthFailureError { HashMap headers = new HashMap(); headers.put("Content-Type", "application/json; charset=utf-8"); return headers; }
You need to add Content-Type to the header.
回答3:
Try this ...
StringRequest sr = new StringRequest(type,url, new Response.Listener() { @Override public void onResponse(String response) { // valid response } }, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError error) { // error } }){ @Override protected Map getParams(){ Map params = new HashMap(); params.put("username", username); params.put("password", password); params.put("grant_type", "password"); return params; } @Override public Map getHeaders() throws AuthFailureError { Map params = new HashMap(); // Removed this line if you dont need it or Use application/json // params.put("Content-Type", "application/x-www-form-urlencoded"); return params; }
回答4:
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() { @Override public void onResponse(String response) { ... ... ...
回答5:
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(); } } }); }
回答6:
in my case, I was not writing reg_url with :8080 . String reg_url = "http://192.168.29.163:8080/register.php";
回答7:
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.