Android Volley error.getMessage() 为空
Android Volley error.getMessage() is blank
所以我正在向我的服务器发出 POST JSonObjectRequest,当它成功时一切正常并发布信息,但如果出现错误并且我尝试在 toast 中显示它,它会显示为空白。这是我的要求:
private void logUserIn() {
final String URL = Globals.BASE_URL +"/auth/login/";
// 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(URL, new JSONObject(params),
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
Log.d("Log In User", response.toString());
//logged in db, changes screens
Intent nextScreen = new Intent(getApplicationContext(), MyProfile.class);
startActivity(nextScreen);
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(getApplicationContext(), error.getMessage(), Toast.LENGTH_SHORT).show();
VolleyLog.e("Error: ", error.getMessage());
}
});
// add the request object to the queue to be executed
Globals.mRequestQueue.add(req);
}
error.getMessage() 始终为空白。这是服务器 returns 错误时我的响应(使用 CURL 测试):
{
"non_field_errors": [
"Unable to login with provided credentials."
]
}
我好像无法打印这条消息。我错过了什么? POST 有效,但错误响应显示为空白...
VolleyError 对象有一个 networkResponse 引用,请尝试检查它以查看是否可以从那里获得一些有用的信息。
@Override
public void onErrorResponse(VolleyError error) {
if (error == null || error.networkResponse == null) {
return;
}
String body;
//get status code here
final String statusCode = String.valueOf(error.networkResponse.statusCode);
//get response body and parse with appropriate encoding
try {
body = new String(error.networkResponse.data,"UTF-8");
} catch (UnsupportedEncodingException e) {
// exception
}
//do stuff with the body...
}
所以我正在向我的服务器发出 POST JSonObjectRequest,当它成功时一切正常并发布信息,但如果出现错误并且我尝试在 toast 中显示它,它会显示为空白。这是我的要求:
private void logUserIn() {
final String URL = Globals.BASE_URL +"/auth/login/";
// 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(URL, new JSONObject(params),
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
Log.d("Log In User", response.toString());
//logged in db, changes screens
Intent nextScreen = new Intent(getApplicationContext(), MyProfile.class);
startActivity(nextScreen);
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(getApplicationContext(), error.getMessage(), Toast.LENGTH_SHORT).show();
VolleyLog.e("Error: ", error.getMessage());
}
});
// add the request object to the queue to be executed
Globals.mRequestQueue.add(req);
}
error.getMessage() 始终为空白。这是服务器 returns 错误时我的响应(使用 CURL 测试):
{ "non_field_errors": [ "Unable to login with provided credentials." ] }
我好像无法打印这条消息。我错过了什么? POST 有效,但错误响应显示为空白...
VolleyError 对象有一个 networkResponse 引用,请尝试检查它以查看是否可以从那里获得一些有用的信息。
@Override
public void onErrorResponse(VolleyError error) {
if (error == null || error.networkResponse == null) {
return;
}
String body;
//get status code here
final String statusCode = String.valueOf(error.networkResponse.statusCode);
//get response body and parse with appropriate encoding
try {
body = new String(error.networkResponse.data,"UTF-8");
} catch (UnsupportedEncodingException e) {
// exception
}
//do stuff with the body...
}