How do I set the user agent in Volley?
Asked Answered
L

2

7

I have one small strange question: How do I set the user agent in Volley?

I need the full version of some sites (desktop version), not mobile version.

I tried to change variable userAgent from "volley/0" to something like "Mozilla/5.0 (X11; Linux i686) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/27.0.1453.93 Safari/537.36" (my Chrome). No difference.

Any advice?

Longevous answered 4/6, 2013 at 8:26 Comment(2)
here are some tutorials check this.Quarles
developer.android.com/training/volley/request-custom.html shows another way.Dato
I
32

You should override the method getHeaders() in Request and set the "User-agent" header

In your Request class:

@Override
public Map<String, String> getHeaders(){
    Map<String, String> headers = new HashMap<String, String>();
    headers.put("User-agent", "YOUR_USER_AGENT");
    return headers;
}
Inspired answered 4/6, 2013 at 8:34 Comment(0)
T
2

Following on from @alex's answer you need to add this @Override function to your request object when adding it to the queue.

Request request = new Request(
   Method.GET,
   url,
   Listener listener,
   ErrorListener errorListener) {
    @Override
    public Map<String, String> getHeaders(){
        Map<String, String> headers = new HashMap<String, String>();
        headers.put("User-agent", "YOUR_USER_AGENT");
        return headers;
    }
};

To understand more about how to add this to your requests, see this StackOverflow answer about setting headers. - How to set custom header in Volley Request

Tabes answered 13/8, 2015 at 9:13 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.