英文:
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 -> {
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;
}
};
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 <stdio.h>
int main()
{
int foo = 0;
printf("Hello world");
return foo;
}
java.lang.IllegalArgumentException: Unexpected char 0x0a at 18 in header value: #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)
答案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("MIME Type", "application/x-www-form-urlencoded; charset=UTF-8");
• Step 2: replaced params.put("code", code);
with params.put("code", 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("code", code);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论