json - 发布字符串请求 Volley ?

标签 json string http escaping android-volley

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

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

提前致谢!

最佳答案

首先,您应该重写 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实际上已经实现了以下功能,并且它有效,对我来说它有效......希望这对你有帮助。

关于json - 发布字符串请求 Volley ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25952424/

相关文章:

javascript - 在 json 中使用 id 查找对象索引

Python3 : List of strings, 其中字符串具有可变字母(不是所有位置的所有字母)

java - 是否可以在不使用 hasNextLine 的情况下检查输入是否为整数,如果为 0 则退出循环?

angularjs - 如何在更改选择时更改 http?

c# - 如何使用与使用网络浏览器相同的行为在 C# 中下载网页?

javascript - 为什么我的代码没有获取 JSON 值?

javascript - 发送 JSON 数据进行调试

c# - JSON post 在 IE 中有效,在 FF 中无效

c++ - 如何在 C++ 中轻松管理 Unicode 字符串

http - 在一个结构化的日志行中跟踪 Go 中的 HTTP 请求