Android Volley-BasicNetwork.performRequest:意外的响应代码400 [英] Android Volley - BasicNetwork.performRequest: Unexpected response code 400
问题描述
问题陈述:
我正在尝试访问REST API,该API将使用Volley返回用于各种HTTP状态代码(400、403、200等)的JSON对象.
对于200以外的任何HTTP状态,看来意外响应代码400"是一个问题.有谁有办法绕过这个错误"?
代码:
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);
}
在不更改Volley
源代码的情况下执行此操作的一种方法是检查VolleyError
中的响应数据并自行解析./p>
从 f605da3 commit
开始,Volley
引发 ServerError
异常,其中包含原始网络响应.
因此您可以在错误侦听器中执行类似的操作:
/* 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();
}
}
}
将来,如果Volley
发生更改,则可以采用上述方法,其中您需要检查VolleyError
中服务器已发送的原始数据并进行解析.
我希望他们实现
One way of doing this without changing As of So you can do something similar to this in your error listener: For future, if I hope that they implement that 这篇关于Android Volley-BasicNetwork.performRequest:意外的响应代码400的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!Volley
's source code is to check for the response data in the VolleyError
and parse it your self.f605da3 commit
, Volley
throws a ServerError
exception that contains the raw network response./* 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();
}
}
}
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.TODO
mentioned in the source file.