Bended
Bended

Reputation: 504

Use HttpUrlConnection with volley

I read everywhere that volley will use either HttpUrlConnection for newer versions api or HttpClient for older version and I'm trying to tell volley to only use the HttpUrlConnection.

My main goal is to setup volley to execute requests using a cookie I have in store, and to do that I know I need to set the HttpUrlConnection with the cookie and then pass it to volley to use as the default implementation.

So far so good but I have no idea how to start HttpUrlConnection and add the cookie to it.

Can someone please give me a small example of how to initialise HttpUrlConnection and add a cookie to it and then pass it on to volley?

I was able to perform such a request on HttpUrlConnection itself and it worked but how do I set it up to use with volley?

URL urlLink = new URL(url2);
HttpURLConnection conenction = (HttpURLConnection)urlLink.openConnection();
conenction.setRequestProperty("Cookie", cookie);

Upvotes: 0

Views: 5102

Answers (1)

Bryan Herbst
Bryan Herbst

Reputation: 67259

I read everywhere that volley will use either HttpUrlConnection for newer versions api or HttpClient for older version and I'm trying to tell volley to only use the HttpUrlConnection.

This is correct. See lines 54-60 of the Volley source. If your app is running on a device using Gingerbread (API level 9) or higher, it is already using HttpUrlConnection for all requests.

If you really want to use your own HttpUrlConnection instance for your requests, you will need to implement your own HttpStack (see Volley's HurlStack for an example). You can use Volley# newRequestQueue(Context, HttpStack) to tell Volley to use your custom stack.

There are a number of alternatives for sending cookies however. I recommend checking out this question for some of those alternatives.

Upvotes: 5

Related Questions