I am using HttpURLConnection for communication with server and using HttpResponseCache for caching, when server respond with 304 (HTTP_NOT_MODIFIED), i got empty body, but according to documentation of HttpURLConnection and HttpResponseCache, in case 304 HttpUrlConnection will fetch the response for the same URL in the cache. While the request for url is cached and i confirmed it. when url is called first time i get proper response (data) while after cached when i called url, Why i am getting empty body (no data).
I fixed the issue by downloading volley library from official google code repository this is the link
https://android.googlesource.com/platform/frameworks/volley/+/master.
Actually the volley source code available at github is customized and cause many issues (github link: https://github.com/mcxiaoke/android-volley), never download from that link.
Related
I'm sending an http request to a website using Volley (POST and StringRequest). The call is correctly executed. However, I can see that the result is a string codified. When seeing the headers of the answer I can see it is encoded in br, which means brotli. How can I decode the answer to later read it as a JSON?
Should I change to OkHttp or another alternative?
I resolved this error code 307 issue by hitting first on base url and then getting the redirect url from response header and hitting on that redirect url in both volley and retrofit. In volley I used retry policy to hit on redirect url but in retrofit I handle this redirection manually.
String redirectUrl=responseHeaders.get("location");
Is there any best solution to resolve this issue?Please help me out.
I tried to override deliverError method but still facing the problem.
My request is https but on error response location I am receiving an http url.
What is significance of location in error response?
thanks.
Use this version of volley library here: https://github.com/samkirton/android-volley
Add to your gradle :
compile 'com.mcxiaoke.volley:library:1.0.19'
I hope this should resolve your issue
I'm not sure if I understand your question.
For sdk version greater than or equal to 9, volley internally use HttpURLConnection. As other related answers mentioned, HttpURLConnection will not redirect to a schema with different protocol. That means a 30x from https to http or vise versa is not automatically handled in volley. You are going to get an Error in deliverError if such a response is returned.
The version from https://github.com/samkirton/android-volley seems to have dealt with 301, 302, not all 30x. But I'm not sure.
By the way, 30x is generally treated as error in almost all libs that implements http. This should not be a problem. You may consider retrieving the url from location and making a request by your own
I'm trying to get HttpResponseCache to cache responses to requests that include an "Authorization" header. I'm including this header because the API I am calling uses basic authentication.
HttpUrlConnection connection = initialiseConnection();
String usernameAndPasswordString = Base64.encodeToString(String.format("%s:%s", username, password).getBytes(), Base64.NO_WRAP);
connection.setRequestProperty("Authorization", String.format("basic %s", usernameAndPasswordString));`
To test this, I'm making the request with WiFi turned on. I'm then turning off WiFi and data and making the request again. I then get a FileNotFoundException when trying to read the response body.
InputStream inputStream = new BufferedInputStream(connection.getInputStream());
If I do the same thing but without the "Authorization" header (to an app on a different server that doesn't use basic auth), my code is able to read the response from the cache.
I am aware that an HTTP cache is not meant to cache a response that was the result of a request including an "Authorization" header, but does that mean that I just can't cache any responses from this server without writing my own cache? Is there any known way around this or to override this behaviour in HttpUrlConnection / HttpResponseCache?
Thanks in advance!
I managed to get to the bottom of this by going through the source code of HttpResponseCache (via https://github.com/candrews/HttpResponseCache, a custom version of the class by candrews taken from the Android source :) ). Including "public", "must-revalidate" or "s-maxage" directives in the Cache-Control header of the response will allow caching by HttpResponseCache even if the Authorization header was included in the request.
I am using Volley and GSON for networking in my app. I have a news feen in the app. I want to show the previously downloaded and cached result right away while downloading the latest content. I have tried accessing the cache using queue.getCache().get(url) but this is always null.
How to properly show cached result while new content is being downloaded?
If it is always null, that means there is no chache for that url. Make sure you did not turned off caching with setShouldCache. Or in http header of response is cache-control: no-cache or max-age=0