【发布时间】:2023-03-27 08:50:01
【问题描述】:
我遇到了 Volley 请求的问题 - 我想使用 JSONObject 参数(用户有密码和登录名)发送 GET(也是另一个类似的 POST)请求来授权用户并发回完整的用户数据。虽然在调试过程中我可以看到 mRequestBody 是正确的 JSON 但我的控制器无法接收它 -
private void processLogin() throws JSONException {
this.user.setLogin(this.loginText.getText().toString());
this.user.setPassword(this.passwordText.getText().toString());
final JSONObject jsonObject = new JSONObject(new Gson().toJson(this.user));
final JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, UrlHelper.loginUrl, jsonObject, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
user = new Gson().fromJson(response.toString(), User.class);
if (user.getUserId().equals(getResources().getString(R.string.ERROR))) {
onLoginFailed();
} else {
onLoginSuccess(user);
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.d(TAG, error.toString());
}
}) {
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
HashMap<String, String> headers = new HashMap<String, String>();
headers.put("Content-Type", "application/json");
return headers;
}
};
VolleySingleton.getInstance(this).addToRequestQueue(request);
}
控制器方法:
@RequestMapping(value = "/loginTest", method = RequestMethod.GET, produces = {MediaType.APPLICATION_JSON_UTF8_VALUE})
public User someMethod(@RequestBody User user) throws SQLException {
return userService.authenticateUser(user.getLogin(), user.getPassword());
}
没有注释@RequestBody我处理但param中的用户为空,所以我处理用户密码为空并登录。最近我通过StringRequest 做到了,请帮帮我。 :)
【问题讨论】:
标签: java android json controller android-volley