【问题标题】:Post string request volley?发布字符串请求截击?
【发布时间】:2014-09-20 19:17:55
【问题描述】:

我想POST 将以下字符串发送到服务器并通过android volley 接收JSONObject!文档说对服务器的请求应该采用下面给出的格式,Content-Type"application/x-www-form-urlencoded"。如何使用 volley 提出此请求?

{
    Username=usr&Password=passwd&grant_type=passwd
}

提前致谢!

【问题讨论】:

    标签: json string http escaping android-volley


    【解决方案1】:

    首先,您应该覆盖您的 getbody() 并在该函数中编码您的参数...例如:

        @Override
        public byte[] getBody() {
            Map<String, String> params = new HashMap<String, String>();
            params.put("password", "yourpassword");
    
            if (params != null && params.size() > 0) {
               return encodeParameters(params, getParamsEncoding());
            }
            return null;
        }
    
    
            protected byte[] encodeParameters(Map<String, String> params, String paramsEncoding) {
            StringBuilder encodedParams = new StringBuilder();
            try {
                for (Map.Entry<String, String> entry : params.entrySet()) {
                    encodedParams.append(URLEncoder.encode(entry.getKey(), paramsEncoding));
                    encodedParams.append('=');
                    encodedParams.append(URLEncoder.encode(entry.getValue(), paramsEncoding));
                    encodedParams.append('&');
                }
                return encodedParams.toString().getBytes(paramsEncoding);
            } catch (UnsupportedEncodingException uee) {
                throw new RuntimeException("Encoding not supported: " + paramsEncoding, uee);
            }
        }
    

    这是对参数进行编码的方式.... volley 实际上已经实现了以下功能,并且有效,对我来说有效...希望对您有所帮助。

    【讨论】:

      【解决方案2】:

      所以你的问题令人困惑。 POST 正文参数未在 JSON 正文中编码。

      我还没有测试过,但如果你了解 HTTP,你应该从这里了解其余部分。

      final Map<String, String> postBody = new HashMap<String, String>();
      postBody.put("Username", "usr");
      postBody.put("Password", "passwd");
      postBody.put("grant_type", "passwd");
      
      final Listener<JSONObject> responseListener = ...;
      Request request = new Request<JSONObject>(Method.POST, "http://example.com", errorListener) {
      
         void deliverResponse(JSONObject obj) {
               responseListener.onResponse(obj);
         }
      
         protected Map<String, String> getPostParams() throws AuthFailureError {
               return postBody;
         }
      
         protected Response<T> parseNetworkResponse(NetworkResponse response) {
               if (response.status == 200) {
                   JSONObject responseBody = new JSONObject(new String(response.data));
                   return Response.success(body, getCacheEntry());
               } else {
                   return new Response<JSONObject>(new VolleyError(response);
               }
         }
      
      };
      requestQueue.add(request);
      

      【讨论】:

      • 不知道如何像你一样包含换行符(android studio)-我使用了“{\nUsername=usr&Password=passwd&grant_type=passwd\n}”但我收到了一个错误的请求错误!!
      • 您确定它是 json 正文而不是预期的 www-url-encoded 参数吗?对我来说,这些看起来像 www-url 编码的参数,它们周围有大括号。 json 将每个定义为单独的键值,没有 &'s。
      • 对不起,我想将此字符串作为 x-www-form-urlencoded 参数发送并接收 json 响应,如果这就是您的要求!!
      • 服务器需要一个 www-url-encoded 参数!
      • 抱歉再次混淆,在研究时,我一定是混淆了“编码”这个词!我已经澄清了这个问题 - 如果我们在同一页面上,请告诉我!非常感谢!!
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-07-19
      • 1970-01-01
      • 2021-09-23
      • 1970-01-01
      • 2012-06-14
      • 2017-07-23
      • 1970-01-01
      相关资源
      最近更新 更多