1

My app needs to download a few images from the web and I've been using Volley for the other basic requests for a couple of months now. However, when I try to get image from the web on Android 2.2, this error comes up. I've only tested on Android 4.3 and android 2.2 and it works perfectly on 4.3.

E/Volley(2603): [65] BasicNetwork.performRequest: Unexpected response code 302 for <request URL>

Here's my code :

imageContainer = imageLoader.get(imageUrl, new ImageListener() {

    public void onErrorResponse(VolleyError error) {
        iv.setImageResource(0); 
    }

    public void onResponse(ImageContainer response, boolean arg1){
        bitmap = response.getBitmap();
        if (bitmap != null) {
             iv.startAnimation(AnimationUtils.loadAnimation(getActivity(), android.R.anim.fade_in));
             iv.setImageBitmap(bitmap);

        } 

    }

});

Doesn't Volley handle redirects on it's own? Any ideas on what could be done here?

Thanks!

Rahul Sainani
  • 3,437
  • 1
  • 34
  • 48
  • possible duplicate of [Android volley to handle redirect](http://stackoverflow.com/questions/17481964/android-volley-to-handle-redirect) – rds Oct 13 '14 at 09:00

3 Answers3

3

You can simply call

HttpUrlConnection.setFollowRedirects(true);

for API 9 or later.

But, it doesn't handle HTTP/HTTPS redirects.

19 Lee
  • 108
  • 6
  • Volley must handle this on it's own as it works fine for API 9+. It was API 8, that couldn't handle redirects on its own, so we have to handle it ourselves, still haven't done much about it though. – Rahul Sainani Dec 20 '13 at 11:52
  • 2
    API 9+ still doesn't handle http to https redirects – DallinDyer Jul 03 '14 at 18:39
3

This question is basically a duplicate of Android volley to handle redirect. To solve this, you need to change volley, but someone has already done this for you in this github: github.com/samikirton/android-volley. Hope that helps!

Community
  • 1
  • 1
1

It probably has to do with the HTTP stack Volley uses in 2.2 (apache), as opposed to using HttpUrlConnection for API 9 and up.

I'm not quite sure about this, but perhaps you have to handle URL redirection manually for API 8. That is, get the HTTP response, check the code and create a new request if it's a redirect code according to the header.

EDIT:

A quick look at the code yielded that inside the client provided to HttpClientStack (the one used for API 8), upon creating a new http client instance the following code appears (AndroidHttpClient line 123):

// Don't handle redirects -- return them to the caller.  Our code
// often wants to re-POST after a redirect, which we must do ourselves.
HttpClientParams.setRedirecting(params, false);

You can try changing it to true and see if it solves your problem. I'm not sure what your needs are exactly, but that looks like a good place to start.

Itai Hanski
  • 8,540
  • 5
  • 45
  • 65
  • Thanks for the reply. Where do I have to change this? I'm new to this. How can I change the behavior of Volley so it handles redirects on api 8 as well? – Rahul Sainani Nov 05 '13 at 17:18
  • Just look at the source. It seems you need to create your own version of AndroidHttpClient (this is an Android source, see my answer) and use it in Volley.java (line 59) where a new `HttpClientStack` is created. – Itai Hanski Nov 05 '13 at 17:26
  • Is there another way of doing this? I would have to add a lot of files to the project to get this working. The apache harmony package is missing so I guess I'd have to add the files manually which would have their own dependencies and would have to add those too. Is there a way to get the url and do the redirect stuff on my own? Looking into that. – Rahul Sainani Nov 06 '13 at 09:48