【问题标题】:setting query string in volley is not working在 volley 中设置查询字符串不起作用
【发布时间】:2014-06-17 13:13:43
【问题描述】:

我尝试使用继承请求的自定义请求类发送 GET 请求。我尝试通过 getParams() 方法设置参数。但是当我发送请求时,我收到用户名或密码无效错误。我不知道为什么会这样。但是,当我通过包含 http://myurl.com/method?user="username"&password="password" 等参数的 url 发送请求时,它可以工作。有人可以告诉我为什么会这样吗?

CustomRequest request=new CustomRequest(number,url,new MyListener(number),new ErrorListner(number)){

            @Override
            protected Map<String, String> getParams() throws AuthFailureError {
                Map<String, String> query=new HashMap<>();
                query.put("user",data.getString("user"));
                query.put("password",data.getString("password"));
                query.put("sender",data.getString("senderid"));
                query.put("SMSText",data.getString("message"));
                query.put("GSM",number);
                return  query;

            }
        };

【问题讨论】:

  • 您的CustomRequest 扩展了哪个类?如果你扩展 JsonArrayRequestJsonObjectRequest,你应该转向 StringRequest,详情见我的另一个answer

标签: parameters android-volley


【解决方案1】:

阅读 javadocs 您会发现 getParams 仅用于 POST 或 PUT,而不是 GET。查询参数需要自己添加到网址中。

/**
 * Returns a Map of parameters to be used for a POST or PUT request.  Can throw
 * {@link AuthFailureError} as authentication may be required to provide these values.
 *
 * <p>Note that you can directly override {@link #getBody()} for custom data.</p>
 *
 * @throws AuthFailureError in the event of auth failure
 */
protected Map<String, String> getParams() throws AuthFailureError {
    return null;
}

【讨论】:

  • 感谢您的回复。这就是问题:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-07-10
  • 1970-01-01
  • 1970-01-01
  • 2012-06-15
  • 2023-03-11
  • 2012-12-26
  • 1970-01-01
相关资源
最近更新 更多