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?
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.
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.
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.
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.
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;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With