0
我正在尝试使用Volley从我的android手机发送到Spring上编写的HTTP服务器的GET请求。使用Google Volley发送请求参数
我做了延伸Request
并覆盖其getParams()
梅托德自定义请求:
@Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String, String> parameters = new HashMap<>();
parameters.put("email", "[email protected]");
parameters.put("password", "xxxx");
return parameters;
}
:我将我在这样的请求:
GsonRequest = new GsonRequest<String>(Request.Method.GET, "http://192.168.43.15:10000/login",
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
....
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
......
}
});
Log.i(TAG, new String(request.getBody()));
requestQueue.add(request);
日志显示请求有这个参数:
email = xxxx%40xxx.com & password = xxxx &
但有一个错误:
Unexpected response code 400 for http://192.168.43.15:10000/login
它看起来像请求参数不发送。
这是我的服务器代码:
@RequestMapping(path = "/login", method = RequestMethod.GET)
public ResponseEntity<String> login(@RequestParam(name = "email") String email,
@RequestParam(name = "password") String password) {
User user = repository.findByEmailAndPassword(email, password);
if (user != null) {
return new ResponseEntity("Found", HttpStatus.OK);
}
return new ResponseEntity<>("Not found", HttpStatus.OK);
}
我觉得这事不对我的Android设备上的代码,因为当我提出从浏览器的一切请求确定。
有人能告诉我问题在哪里吗?
愿你可以使用** ** StringRequest而不是** ** JSONRequest因为你正在返回一个字符串响应。 – Frank
它看起来像你想发送一些数据到服务器,但你使用GET方法,尝试使用POST方法,当数据库没有收到强制参数时出现错误。 –