StringRequest stringRequest=new StringRequest(Request.Method.POST,url, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
Toast.makeText(LoginActivity.this, "Success", Toast.LENGTH_SHORT).show();
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(LoginActivity.this, "Check Again", Toast.LENGTH_SHORT).show();
//Toast.makeText(LoginActivity.this, error.toString(), Toast.LENGTH_SHORT).show();
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
Toast.makeText(LoginActivity.this, res, Toast.LENGTH_SHORT).show();
Log.i("Error Code",res);
} catch (UnsupportedEncodingException e1) {
// Couldn't properly decode data to string
e1.printStackTrace();
}
}
}
}){
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
HashMap<String, String> headers = new HashMap<String, String>();
headers.put("X-Api-Key",APIKey);
//headers.put("Content-Type", "application/json; charset=utf-8");
return headers;
}
@Override
protected Map<String,String> getParams() throws AuthFailureError{
Map<String, String> params = new HashMap<String, String>();
params.put("email",email);
params.put("password",password);
//params.put("grant_type", "password");
return params;
}
};
MySingleton.getmInstance(LoginActivity.this).addToRequestQueue(stringRequest);
我尝试在postman中使用相同的标题和参数传递相同的url,url在那里工作得很好。如您所见,我已应用了类似问题中提供的所有更正。生成的错误代码为:
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>400 Bad Request</title>
</head><body>
<h1>Bad Request</h1>
<p>Your browser sent a request that this server could not understand.<br />
</p>
</body></html>
我也尝试使用相同的代码命中其他api,它们工作得很好。但这个api我不能打,但正如我在Postman中所说,我可以正确地击中它。