【问题标题】:Sending request parameters with Google Volley使用 Google Volley 发送请求参数
【发布时间】:2017-06-18 17:36:07
【问题描述】:

我正在尝试使用 Volley 从我的 android 手机向 Spring 上编写的 HTTP 服务器发出 GET 请求。

我提出了一个扩展 Request 并覆盖其 getParams() 方法的自定义请求:

@Override
protected Map<String, String> getParams() throws AuthFailureError {
        Map<String, String> parameters = new HashMap<>();
        parameters.put("email", "xxxx@xxx.com");
        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&

但是有一个错误:

http://192.168.43.15:10000/login 的意外响应代码 400

请求参数好像没有发送。


这是我的服务器代码:

@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 因为您正在返回字符串响应。
  • 看起来你想向服务器发送一些数据但是你使用GET方法,尝试使用POST方法,当数据库没有收到强制参数时出现错误。

标签: android spring-mvc android-volley


【解决方案1】:

经过一番查看,我发现这篇文章解释了为什么不发送参数。

Volley - how to send DELETE request parameters?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-14
    • 1970-01-01
    • 2013-08-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多