简体   繁体   中英

Android Volley Error Unexpected char 0x0a in header value

I am using Android volley to send a GET request and I am passing headers in base64. I keep getting this error whenever the total of the header text is more than 57 characters:

04-10 11:04:47.593 24274-24516/com.graypeakworkforce.graypeak E/Volley: [722] NetworkDispatcher.run: Unhandled exception java.lang.IllegalArgumentException: Unexpected char 0x0a at 76 in header value: InRlc3QiOiIxMjM0NTY3ODkwMTIzNDU2Nzg5MDEyMzQ1Njc4OTAxMjM0NTY3ODkwMTIzNDU2Nzg5 MDEyMzQ1Njc4OTAifQ== java.lang.IllegalArgumentException: Unexpected char 0x0a at 76 in header value: InRlc3QiOiIxMjM0NTY3ODkwMTIzNDU2Nzg5MDEyMzQ1Njc4OTAxMjM0NTY3ODkwMTIzNDU2Nzg5 MDEyMzQ1Njc4OTAifQ== at com.android.okhttp.Headers$Builder.checkNameAndValue(Headers.java:313) at com.android.okhttp.Headers$Builder.add(Headers.java:245) at com.android.okhttp.internal.huc.HttpURLConnectionImpl.addRequestProperty(HttpURLConnectionImpl.java:579) at com.android.okhttp.internal.huc.DelegatingHttpsURLConnection.addRequestProperty(DelegatingHttpsURLConnection.java:186) at com.android.okhttp.internal.huc.HttpsURLConnectionImpl.addRequestProperty(HttpsURLConnectionImpl.java) at com.android.volley.toolbox.HurlStack.performReques t(HurlStack.java:105) at com.android.volley.toolbox.BasicNetwork.performRequest(BasicNetwork.java:96) at com.android.volley.NetworkDispatcher.run(NetworkDispatcher.java:112)

If I do not base64 encode there is no problem, if the base64 string is less than 58 characters there is no problem. I double checked that the base64 encoding is correct.

I am using Volley version 1.0.0

This is the code:

    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);

Instead of:

Base64.encodeToString(data, Base64.DEFAULT)

I need to do:

Base64.encodeToString(data, Base64.NO_WRAP); 

If you still want to use

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

Replace \\n with Empty String

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM