Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I set the user agent in Volley?

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?

like image 424
none Avatar asked Jun 04 '13 08:06

none


People also ask

How do I add a header in volley?

If you need to add custom headers to your volley requests, you can't do this after initialisation, as the headers are saved in a private variable. Instead, you need to override the getHeaders() method of Request.

What is Android volley ClientError?

It means the server returned a 4xx error code. volley/src/main/java/com/android/volley/toolbox/BasicNetwork.java. Line 199 in d1a3d53. throw new ClientError(networkResponse); All reactions.

How does volley library work?

Although Volley is a part of the Android Open Source Project(AOSP), Google announced in January 2017 that Volley will move to a standalone library. It manages the processing and caching of network requests and it saves developers valuable time from writing the same network call/cache code again and again.

What is retry policy in volley?

By default, Volley sets all socket and connection timeouts to 5 seconds for all requests. RetryPolicy is an interface where you need to implement your logic of how you want to retry a particular request when a timeout occurs.


1 Answers

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;
}
like image 65
alex Avatar answered Oct 16 '22 05:10

alex