“Volley在我尝试设置头部时抛出错误。”

huangapple go评论81阅读模式
英文:

Volley throwing error when I try to set a header

问题

我在给我的 Volley String 请求添加自定义头部时遇到了错误,错误信息为 在头部值的第18个字符处出现意外字符0x0a

以下是我的代码。

stringRequest = new StringRequest(Request.Method.GET, URL, string -> {
    try {
        JSONObject jsonObject = new JSONObject(string);
        Log.d("TAG", jsonObject.toString());
    } catch (Exception e) {

    }
}, error -> {

}){
    @Override
    public Map<String, String> getHeaders() {
        Map<String, String> params = new HashMap<>();
        params.put("MIME Type", "application/x-www-form-urlencoded; charset=UTF-8");
        params.put("compiler", "c");
        params.put("code", code);
        params.put("input", "");
        return params;
    }
};

我遇到的错误信息为。

2020-10-07 10:52:06.869 32119-32232/com.my.app E/Volley: [2415] NetworkDispatcher.processRequest: Unhandled exception java.lang.IllegalArgumentException: 在头部值的第18个字符处出现意外字符0x0a: #include <stdio.h>

int main()
{
int foo = 0;

printf("Hello world");

return foo;
}
java.lang.IllegalArgumentException: 在头部值的第18个字符处出现意外字符0x0a: #include <stdio.h>

int main()
{
int foo = 0;

printf("Hello world");

return foo;
}
    at com.android.okhttp.Headers$Builder.checkNameAndValue(Headers.java:319)
    at com.android.okhttp.Headers$Builder.add(Headers.java:250)
    at com.android.okhttp.internal.huc.HttpURLConnectionImpl.addRequestProperty(HttpURLConnectionImpl.java:600)
    at com.android.okhttp.internal.huc.DelegatingHttpsURLConnection.addRequestProperty(DelegatingHttpsURLConnection.java:187)
    at com.android.okhttp.internal.huc.HttpsURLConnectionImpl.addRequestProperty(HttpsURLConnectionImpl.java:30)
    at com.android.volley.toolbox.HurlStack.executeRequest(HurlStack.java:95)
    at com.android.volley.toolbox.BasicNetwork.performRequest(BasicNetwork.java:131)
    at com.android.volley.NetworkDispatcher.processRequest(NetworkDispatcher.java:120)
    at com.android.volley.NetworkDispatcher.run(NetworkDispatcher.java:87)
英文:

I'm getting an error says Unexpected char 0x0a at 18 in header value when I add a custom header to my Volley String request.

Here is my code.

stringRequest = new StringRequest(Request.Method.GET, URL, string -&gt; {
            try {
                JSONObject jsonObject = new JSONObject(string);
                Log.d(&quot;TAG&quot;, jsonObject.toString());
            } catch (Exception e) {

            }
        }, error -&gt; {

        }){
            @Override
            public Map&lt;String, String&gt; getHeaders() {
                Map&lt;String, String&gt;  params = new HashMap&lt;&gt;();
                params.put(&quot;MIME Type&quot;, &quot;application/x-www-form-urlencoded; charset=UTF-8&quot;);
                params.put(&quot;compiler&quot;, &quot;c&quot;);
                params.put(&quot;code&quot;, code);
                params.put(&quot;input&quot;, &quot;&quot;);
                return params;
            }
        };

the error I'm getting.

2020-10-07 10:52:06.869 32119-32232/com.my.app E/Volley: [2415] NetworkDispatcher.processRequest: Unhandled exception java.lang.IllegalArgumentException: Unexpected char 0x0a at 18 in header value: #include &lt;stdio.h&gt;
    
    int main()
    {
int foo = 0;
    
printf(&quot;Hello world&quot;);
    
return foo;
    }
    java.lang.IllegalArgumentException: Unexpected char 0x0a at 18 in header value: #include &lt;stdio.h&gt;
    
    int main()
    {
int foo = 0;
    
printf(&quot;Hello world&quot;);
    
return foo;
    }
        at com.android.okhttp.Headers$Builder.checkNameAndValue(Headers.java:319)
        at com.android.okhttp.Headers$Builder.add(Headers.java:250)
        at com.android.okhttp.internal.huc.HttpURLConnectionImpl.addRequestProperty(HttpURLConnectionImpl.java:600)
        at com.android.okhttp.internal.huc.DelegatingHttpsURLConnection.addRequestProperty(DelegatingHttpsURLConnection.java:187)
        at com.android.okhttp.internal.huc.HttpsURLConnectionImpl.addRequestProperty(HttpsURLConnectionImpl.java:30)
        at com.android.volley.toolbox.HurlStack.executeRequest(HurlStack.java:95)
        at com.android.volley.toolbox.BasicNetwork.performRequest(BasicNetwork.java:131)
        at com.android.volley.NetworkDispatcher.processRequest(NetworkDispatcher.java:120)
        at com.android.volley.NetworkDispatcher.run(NetworkDispatcher.java:87)

答案1

得分: 0

0x0A是换行字符。请确保在您的头文件中没有"\n"。

我认为在您的头文件中有几行!
并且您可以在正文中发送代码!

英文:

0x0A is \n newline character. make sure in your header don't have "\n".

I think in your header you have some line!
and you can send code with body!

答案2

得分: 0

这个答案中解决了这个问题:

• 第一步:删除了 params.put("MIME Type", "application/x-www-form-urlencoded; charset=UTF-8");

• 第二步:将 params.put("code", code); 替换为 params.put("code", Base64.encodeToString(code.getBytes(), Base64.NO_WRAP));

更新:

• 我已从我的代码中删除了第二步,因为它给我造成了问题。所以现在它看起来是 params.put("code", code);

英文:

Solved it from this answer

• Step 1: removed params.put(&quot;MIME Type&quot;, &quot;application/x-www-form-urlencoded; charset=UTF-8&quot;);

• Step 2: replaced params.put(&quot;code&quot;, code); with params.put(&quot;code&quot;, Base64.encodeToString(code.getBytes(), Base64.NO_WRAP));

UPDATE

• I have removed step 2 from my code because it was causing me a problem. So it now looks like params.put(&quot;code&quot;, code);

huangapple
  • 本文由 发表于 2020年10月7日 13:32:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/64237775.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定