0

I have setup Volley in my app but I am stuck at trying to send request headers (cookies) in each request. I have created a custom request and have overriden getHeaders like below:

@Override
public Map<String, String> getHeaders() throws AuthFailureError {
    HashMap<String, String> headers = new HashMap<>();

    String sessionRequest = "source=Android-" + AppUtils.getDeviceUUID(visnetawrap);
    if (visnetawrap.visnetaUser != null) {
        if (visnetawrap.visnetaUser.getUserIdString() != null) {
            //headers.put("UserSession", "id=" + visnetawrap.visnetaUser.getUserIdString());
            sessionRequest += ",id=" + visnetawrap.visnetaUser.getUserIdString();
        }
        if (visnetawrap.visnetaUser.userToken != null) {
            //headers.put("UserSession", "token=" + visnetawrap.visnetaUser.userToken);
            sessionRequest += ",token=" + visnetawrap.visnetaUser.userToken;
        }
    }
    //headers.put("UserSession", "source=Android-" + AppUtils.getDeviceUUID(visnetawrap));

    headers.put("UserSession", sessionRequest);

    Log.d("RequestHeaders", headers.toString());

    return headers;
}

However, when I create the request I get an error back from the server saying they are not set. It is not a server side issue because it used to work with retrofit. Any suggestions on why this might be?

Joe Ginley
  • 301
  • 3
  • 15
  • Have you read [this question](http://stackoverflow.com/questions/16680701/using-cookies-with-android-volley-library)? – BNK Sep 10 '15 at 01:06
  • I have and I tried it, didn't work for me unless I am doing it wrong...which answer is best? I tried the top one. I am using OkHttpClient, maybe that is the issue? Also all of those values are set in my function above so when it returns the headers it should be sending them? – Joe Ginley Sep 10 '15 at 01:11
  • Post the response message from your server pls – BNK Sep 10 '15 at 01:18
  • It was just returning unknown error but i did a print stack trace and found out it was because of a timeout. – Joe Ginley Sep 10 '15 at 01:28
  • Timeout? Check network connection or setRetryPolicy like `request.setRetryPolicy(new DefaultRetryPolicy(DefaultRetryPolicy.DEFAULT_TIMEOUT_MS * 5, DefaultRetryPolicy.DEFAULT_MAX_RETRIES, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));` – BNK Sep 10 '15 at 01:31
  • What is the value of DEFAULT_TIMEOUT_MS? I will try setting a retry policy. Nevermind it is 2500, let me see if this works ill get back to you... – Joe Ginley Sep 10 '15 at 01:32
  • 1
    That seemed to work, thank you! – Joe Ginley Sep 10 '15 at 02:02
  • Glad to hear that. I add it as my answer, if it help you solve your issue, you can accept :) – BNK Sep 10 '15 at 02:04

1 Answers1

0

As I have commented, for Timeout problem, you should check network connection or setRetryPolicy like the following code:

request.setRetryPolicy(new DefaultRetryPolicy(DefaultRetryPolicy.DEFAULT_TIMEOUT_MS * 5, DefaultRetryPolicy.DEFAULT_MAX_RETRIES, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
BNK
  • 23,994
  • 8
  • 77
  • 87