Android Volley Error标头值

时间:2017-04-10 15:19:56

标签: java android rest android-volley

我使用Android排球发送GET请求,我在base64中传递标头。每当标题文本的总数超过57个字符时,我就会一直收到此错误:

04-10 11:04:47.593 24274-24516 / com.graypeakworkforce.graypeak E / Volley:[722] NetworkDispatcher.run:未处理的异常java.lang.IllegalArgumentException:标头值为76的意外字符0x0a:InRlc3QiOiIxMjM0NTY3ODkwMTIzNDU2Nzg5MDEyMzQ1Njc4OTAxMjM0NTY3ODkwMTIzNDU2Nzg5                                                                         MDEyMzQ1Njc4OTAifQ ==                                                                         java.lang.IllegalArgumentException:标头值为76时出现意外的字符0x0a:InRlc3QiOiIxMjM0NTY3ODkwMTIzNDU2Nzg5MDEyMzQ1Njc4OTAxMjM0NTY3ODkwMTIzNDU2Nzg5                                                                         MDEyMzQ1Njc4OTAifQ ==                                                                             在com.android.okhttp.Headers $ Builder.checkNameAndValue(Headers.java:313)                                                                             在com.android.okhttp.Headers $ Builder.add(Headers.java:245)                                                                             在com.android.okhttp.internal.huc.HttpURLConnectionImpl.addRequestProperty(HttpURLConnectionImpl.java:579)                                                                             在com.android.okhttp.internal.huc.DelegatingHttpsURLConnection.addRequestProperty(DelegatingHttpsURLConnection.java:186)                                                                             在com.android.okhttp.internal.huc.HttpsURLConnectionImpl.addRequestProperty(HttpsURLConnectionImpl.java)                                                                             在com.android.volley.toolbox.HurlStack.performRequest(HurlStack.java:105)                                                                             在com.android.volley.toolbox.BasicNetwork.performRequest(BasicNetwork.java:96)                                                                             在com.android.volley.NetworkDispatcher.run(NetworkDispatcher.java:112)

如果我没有base64编码没有问题,如果base64字符串少于58个字符就没有问题。我仔细检查了base64编码是否正确。

我正在使用Volley 1.0.0版

这是代码:

    Map<String, String> headers = new HashMap<String, String>();

String str = "\"test\":\"123456789012345678901234567890123456789012345678901234567890\"}";

//tried this, doesnt help
//headers.put("Content-Type", "application/json; charset=utf-8");
//tried this, doesnt help
//headers.put("Content-Type", "application/x-www-form-urlencoded");
headers.put("json", stringToBase64(str));


CustomRequest request = new CustomRequest(Request.Method.GET, endpoint+"jobs",new HashMap<String, String>(),headers,
        new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject response)
            {
                standardResponse(response, callback);
            }
        }, new Response.ErrorListener()
{
    @Override
    public void onErrorResponse(VolleyError error)
    {
        NetworkResponse response = error.networkResponse;
        if (error instanceof ServerError && response != null)
        {
            String res = null;
            try
            {
                res = new String(response.data, HttpHeaderParser.parseCharset(response.headers, "utf-8"));
            } catch (UnsupportedEncodingException e)
            {
                e.printStackTrace();
            }
            Log.d("debug",res);
        }
        Log.d("debug",error.toString());
        callback.doCallback(false, error.toString(), null);
    }
});

request.token = app.dataMgr.currentUser.token;

RequestQueue queue = Volley.newRequestQueue(activity);
    queue.add(request);

2 个答案:

答案 0 :(得分:5)

而不是:

Base64.encodeToString(data, Base64.DEFAULT)

我需要这样做:

Base64.encodeToString(data, Base64.NO_WRAP); 

答案 1 :(得分:0)

如果您仍然想使用

String encodedKey = Base64.encodeToString(data, Base64.DEFAULT)

用空字符串替换\ n

String result = encodedKey.replace("\n", "")