我有一个问题。
当我提交请求时,显示错误“E / Volley:[300] BasicNetwork.performRequest:http://192.168.0.130:1010/api/CRM/check_login的意外响应代码404”。
但我使用浏览器或POSTMAN(http://192.168.0.130:1010/api/CRM/check_login?username=test&password=123)发送相同的请求,它可以正常工作。
您可以查看请求的照片(https://www.dropbox.com/sh/vme97ftuifwb3l0/AADZonU2gZRlLjxV64C2iapFa?dl=0)
btn_volley.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String URL = "http://192.168.0.130:1010/api/CRM/check_login";
Response.Listener<String> listener = new Response.Listener<String>() {
@Override
public void onResponse(String response) {
if (response.isEmpty()) {
Toast.makeText(getApplicationContext(), "test",Toast.LENGTH_LONG).show();
} else {
Toast.makeText(getApplicationContext(),response,Toast.LENGTH_LONG).show();
}
}
};
Response.ErrorListener errorListener = new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(getApplicationContext(),"error",Toast.LENGTH_LONG).show();
}
};
StringRequest request = new StringRequest(Request.Method.POST, URL, listener, errorListener)
{
@Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String, String> params = new HashMap<>();
params.put("username","test");
params.put("password","123");
return params;
}
};
AppController.getInstance().addToRequestQueue(request);
}
});
答案 0 :(得分:1)
您可以直接使用您的网址http://192.168.0.130:1010/api/CRM/check_login?username=test&password=123
因为getParams()
方法用于application/form-data
而不是Query
参数,所以它对您不起作用。
来源:
btn_volley.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String username="test";//you can get username and password from edittexts
String password="123";
String URL = "http://192.168.0.130:1010/api/CRM/check_login?username="+username+"&password="+password;
Response.Listener<String> listener = new Response.Listener<String>() {
@Override
public void onResponse(String response) {
if (response.isEmpty()) {
Toast.makeText(getApplicationContext(), "test",Toast.LENGTH_LONG).show();
} else {
Toast.makeText(getApplicationContext(),response,Toast.LENGTH_LONG).show();
}
}
};
Response.ErrorListener errorListener = new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(getApplicationContext(),"error",Toast.LENGTH_LONG).show();
}
};
StringRequest request = new StringRequest(Request.Method.POST, URL, listener, errorListener);
AppController.getInstance().addToRequestQueue(request);
}
});
答案 1 :(得分:0)
您需要在标题中添加Content-Type。
@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;
}