2

There is a handy com.android.volley.NetworkImageView widget extending ImageView. According to a Google presentation, to use it one needs only this in the layout xml file:
<com.android.volley.toolbox.NetworkImageView

and this in the Activity source code:
mImageView.setImageUrl(imageUrl, mImageLoader);

But what if I need to put a cookie or parameter into the request? I've seen this question, but I think this requires modifying volley library. Is there an easier way?

Community
  • 1
  • 1
tutejszy
  • 602
  • 7
  • 22

5 Answers5

2

Here you can find a project with different simple examples about using Volley including using cookies and GET/POST parameters.

Ognyan
  • 13,452
  • 5
  • 64
  • 82
1

The proposed solution by Ogre_BGR is not a proper one as he is using Apache's HTTP client. And volley uses Apache's HTTP client only on API 8 and lower as there it is less buggy than URLConnection. But since Gingerbread (2.3) it is recommended to use URLConnection and it is the one that Android's team is maintaining and updating.

I think that a better solution is the one that you've given a link to. It does not modify Volley, it just adds/saves cookies to the ones that Volley already uses. And it is a common thing to extend some of the *Request classes of Volley for quick and easy call of API requests ( see here ). Ogre_BGR's solution completely changes the HttpStack of Volley.

Zheko
  • 673
  • 6
  • 16
1

You need to set you xml layout file to

<com.android.volley.toolbox.NetworkImageView...

You are missing "toolbox" in your class name.

David Shellabarger
  • 1,523
  • 2
  • 15
  • 23
0

As for the cookies, what I did to keep using URLConnetion (which is the default use case for Volley on API > 8) is to implement a custom class that extends HurlStack (Volley's implementation of an HTTP client using URLConnection), and overriding createConnection(URL url), which is the method used to get a connection prior to every request. I just added my user agent (none is send by default, using HurlStack), and a cookie (also, isn't managed automatically in HurlStack).

Here's my code for the class:

public class CustomHurlStack extends HurlStack {

    public CustomHurlStack() {
        super();
    }

    @Override
    protected HttpURLConnection createConnection(URL url) throws IOException {
        // Create a connection with custom attributes
        HttpURLConnection conn = super.createConnection(url);
        conn.addRequestProperty("User-Agent", "myUserAgent/1.0");
        conn.addRequestProperty("cookie", "myCookie");

        return conn; 
    }
}

This should be a viable solution, and I'm not sure why Volley doesn't include such a configurable class to use, but oh well, you can create your own in just a minute or so, as described above.

bengr
  • 316
  • 3
  • 8
0
private static RequestQueue mQueue;

String userAgent = "volley/0";
HttpStack stack = new HttpClientStack(AndroidHttpClient.newInstance(userAgent));
mQueue = Volley.newRequestQueue(this, stack, 100 * 1024 * 1024);
Huang Jinlong
  • 774
  • 8
  • 6
  • Please provide an explanation as to why this code works so the OP can understand it better instead of just copy-pasting things he doesn't understand. – Lawrence Aiello Jul 13 '15 at 15:51