Volley 发送请求而不编码

Volley send request without encoding it

我正在尝试使用普通字符串向 wifi 控制器发送 HTTP 请求。我的字符串是 API:W/PSS:12345,但当通过我的 Android 应用程序发送时,控制器收到 API=W%2FPSS%3A12345。我知道这是由于 header 值 content-type: application/x-www-form-urlencoded.

但是,在我的请求中,我重写了方法:

public String getBodyContentType() {
    return "text/html;";
}

将内容类型设置为纯文本,但 volley 在发送之前仍然对其进行编码。 (在我的电脑上使用 REST 客户端,将请求发送到控制器而不对其进行编码)

有没有办法将我的字符串作为纯文本发送而无需 volley 编码?控制器是低级别的,所以我不想在两侧添加任何编码,只发送纯字符串。

也覆盖 getHeaders 方法并在 header 中设置您的 content-type,如下所示:

public Map<String, String> getHeaders() throws AuthFailureError {
    HashMap<String, String> headers = new HashMap<String, String>();
    headers.put("Content-Type", "application/text");
    return headers;
}

在深入研究 volley 源代码后,我发现罪魁祸首是调用了 java URLEncoder.encode() 方法,无论如何都会对字符串进行编码...我跳过了这个一种非常骇人听闻的方式。如果你们有更好的方法,请告诉我,因为这很丑陋:

@Override
public String getBodyContentType() {
    //for settings the content=type header, the right way...
    return return "text/html";
}

@Override
public byte[] getBody() throws AuthFailureError {
    Map<String, String> params = getParams();
        if (params != null && params.size() > 0) {
            return encodeParameters(params, getParamsEncoding());
        }
    return null; 
}

//Hax.......
private byte[] encodeParameters(Map<String, String> params, String paramsEncoding){
    StringBuilder encodedParams = new StringBuilder();
        try {
            for (Map.Entry<String, String> entry : params.entrySet()) {
                encodedParams.append(entry.getKey());
                //encodedParams.append(':');
                encodedParams.append(entry.
                //encodedParams.append('&');
            }
            return encodedParams.toString().getBytes(paramsEncoding);
        } catch (UnsupportedEncodingException uee) {
            throw new RuntimeException("Encoding not supported: " + paramsEncoding, uee);
        }
}

volley的源代码在这里,你可以看看它是如何编码项目的:https://android.googlesource.com/platform/frameworks/volley/+/idea133/src/com/android/volley/Request.java