I'm using HttpResponseCache to enable the response caching (for web requests) in my android app, and the offline cache isn't working.
I'm doing the offline cache as the documentation tells me to do.
In my Application class, at the onCreate method, I'm turning on the the cache with:
try {
long httpCacheSize = 10 * 1024 * 1024; // 10 MiB
File httpCacheDir = new File(getCacheDir(), "http");
Class.forName("android.net.http.HttpResponseCache")
.getMethod("install", File.class, long.class)
.invoke(null, httpCacheDir, httpCacheSize);
} catch (Exception httpResponseCacheNotAvailable) {}
At my HttpConnection class I'm getting the JSON with the method:
private String sendHttpGet(boolean cacheOnly) throws Exception {
URL url = new URL(getUrlCompleta());
HttpURLConnection urlConnection = null;
String retorno = null;
try {
urlConnection = (HttpURLConnection) url.openConnection();
if(urlConnection == null)
throw new Exception("Conn obj is null");
fillHeaders(urlConnection, cacheOnly);
InputStream in = new BufferedInputStream(urlConnection.getInputStream(), 8192);
retorno = convertStream(in);
in.close();
urlConnection.disconnect();
if(retorno != null)
return retorno;
} catch(IOException e) {
throw e;
} finally {
if(urlConnection != null)
urlConnection.disconnect();
}
throw new Exception();
}
Where the convertStream method just parse a InputStream into a String.
The method fillHeaders put an token on the request (for security reasons) and if the parameter cacheOnly is true, then the header "Cache-Control", "only-if-cached" is added to the request header ( with the code: connection.addRequestProperty("Cache-Control", "only-if-cached");)
The cache works 'fine' (with minor strange behaviors) when there is connectivity and the app hit the web server just to see if there is a newer version of the JSON. When the web server answers "nothing changed", the cache works.
The problem is when I have no connectivity and use the header "Cache-Control", "only-if-cached". In this case, I receive a java.io.FileNotFoundException: https://api.example.com/movies.json. That is awkward, because the implementation code of the cache probably stores the response in a file named using a hash function on the request url, and not the url itself.
Does anyone knows what can I do or what is wrong with my implementation?
ps: Above, I said "probably using a hash function", because I was not able to found the implementation of the com.android.okhttp.HttpResponseCache object (the class that android.net.http.HttpResponseCache delegates cache calls). If someone found it, please tell me where to look at :)
ps2: Even when I add a max-stale parameter in the Cache-Control header, it still doesn't work.
ps3: I obviously tested it on api 14+.
ps4: Although I'm accessing an "https://" URL address, the same behavior occurs when the URL is just a normal "http://" address.
It turns out that the problem was with the max-age value of the Cache-control directive in the response given by my web server. It had the following value: Cache-Control: max-age=0, private, must-revalidate. With this directive, my server was saying to the cache that the response could be used from the cache even if it was 0 seconds old. So, my connection wasn't using any cached response.
Knowing that max-age is specified in seconds, all I had to do was change the value to: Cache-Control: max-age=600, private, must-revalidate! There it is, now I have a 10 minute cache.
Edit: If you want to use a stale response, with the max-stale directive of the request, you shouldn't use the must-revalidate directive in the response, as I did in my webserver.
Related
I am trying to develop a part of my app in which i want to get video details for a keyword. I am using the search list API. I think I have an issue with the authorization. I am getting a 401. I have tried passing my authorization details.
I have tried the following code after going through a few resources online and I am current getting the java.io.FileNotFoundException at the line at which i get the Input Stream. The connection code that I am getting is a 401.
The given code is in the doInBackground of an AsyncTask.
String ytres="";
URL url;
HttpURLConnection urlConnection = null;
try {
Log.d("youtubedata","a1");
url = new URL("https://www.googleapis.com/youtube/v3/search?part=id&q=queen%20bohemian");
Log.d("youtubedata","a2");
Log.d("youtubedata","a");
urlConnection = (HttpURLConnection) url
.openConnection();
urlConnection.setRequestMethod("GET");
urlConnection.setRequestProperty ("Authorization", "Bearer MYAPIKEY");
urlConnection.setRequestProperty ("Accept", "application/json");
Log.d("youtubedatanum",Integer.toString(urlConnection.getResponseCode()));
InputStream in = urlConnection.getInputStream();
InputStreamReader isw = new InputStreamReader(in);
int data = isw.read();
while (data != -1) {
char current = (char) data;
data = isw.read();
ytres=ytres+current;
System.out.print(current);
}
Log.d("youtubedata",ytres);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (urlConnection != null) {
urlConnection.disconnect();
}
}
return ytres;
I do suspect the issue is in the way i have passed my API Key.
Please go through the API docs at Calling the API and at Search Endpoint.
You'll see that your URL does not contain the needed API key passed on as the key parameter.
It may also worth it to dug up Google's own sample Java implementation at Search.java. However, that code, due to the layer of library abstraction it uses, is not of immediate help since it obscures the way a client should work directly with the API.
For You-tube Data API error 401
Possible issue for unauthorized (401) can be
authorizationRequired
youtubeSignupRequired
Check You-tube Data API error doc Here.
Also confirm that you have follow THIS steps to integrate API.
Sample API Code you can find Here
Hope this can help you.
I followed this to Parse Json In Android
I have Successfully Done it with HttpData handler..
Here I am Successfully Posting Data to server and Getting Response..
Now I want to Use this same in the Part of HTTPS..
Can Any one suggest me How to do this Without Major Changes in my code.. Because In my application I am doing this for more activities.. Please Suggest me to Use HTTPs in my code..
I will provide Additional Info... Depending Responses...
Update
In my code I have Changed HttpURLConnection to HttpsURLConnection
Please suggest me How to through this error In my code..
Update 1
I have Changed Certificate on server side.. Now its working On Https..
But Now,
I want to Use HTTP and HTTPS Both in one app Depending on Client Requirement So here now its worked with Https....
But I also need to work with Http
In my Code Can any any one suggest me...I want I should Work with Https and Http Both In one App.
to use both HTTP and HTTPS, you need to have the 2 methods (i think you already have them)
GetHTTPData(String urlString)
GetHTTPSData(String urlString)
now in HTTPDataHandler class (where you have both methods above)
you need to create a 3rd method GetDataFromUrl(), that will check URL and decide which method to use (http or https)
public String GetDataFromUrl(String url){
if(url.toLowerCase().startsWith("https")){
//HTTPS:
return GetHTTPSData(url);
}else{
//HTTP:
return GetHTTPData(url);
}
}
now in the AsyncTask class ProcessJSON
replace this line stream = hh.GetHTTPData(urlString);
with this one stream = hh.GetDataFromUrl(urlString);
if you don't want to add that 3rd method in HTTPDataHandler, just use the if-statement in ProcessJSON at doInBackground() to call either one of the 2 methods (http or https)
You can use HttpsURLConnection, replace HttpURLConnection by HttpsURLConnection .
public String GetHTTPData(String urlString){
try{
URL url = new URL(urlString);
HttpsURLConnection urlConnection =(HttpsURLConnection)url.openConnection();
// Check the connection status
if(urlConnection.getResponseCode() == 200)
{
// if response code = 200 ok
InputStream in = new BufferedInputStream(urlConnection.getInputStream());
// Read the BufferedInputStream
BufferedReader r = new BufferedReader(new InputStreamReader(in));
StringBuilder sb = new StringBuilder();
String line;
while ((line = r.readLine()) != null) {
sb.append(line);
}
stream = sb.toString();
// End reading...............
// Disconnect the HttpURLConnection
urlConnection.disconnect();
}
else
{
// Do something
}
}catch (MalformedURLException e){
e.printStackTrace();
}catch(IOException e){
e.printStackTrace();
}finally {
}
// Return the data from specified url
return stream;
}
What I understand is in your server side, they used self signed SSL certificate. So you have to install that certificate in your android device also. Settings > Security > install form storage.But for production build you have to buy ssl certificate from CA Authorities.
Hope this will solve your problem.
Remove HttpDataHandler lines in doInBackground use HttpUrlConnection directly in doInBackground or use HttpUrlConnection in JSONparse class to post params to server follow this tutorial to post params Website
I've got an app targeting ICS and above, and I'm trying to use HttpsURLConnection and HttpResponseCache to cache a web service response. It makes the request with an etag, and the server will return 304 if not modified. It appears to be returning stuff from the cache, however, I'm not receiving any indication that the response is from cache. The response code I'm getting from connection.getResponseCode() is 200, not 304. I need to know if it's cached because I want to know if it's changed so I can mark it as read (it's a fairly long string, and I would not like to have to compare the two). Is there something I can set or something I can check so I know that it is from the cache instead of freshly fetched?
You can try using the response header information. Compare the dates, if you are getting the same date for the same request, then it's the cache.
HttpURLConnection conn;
conn = (HttpURLConnection)url.openConnection();
...
conn.connect();
System.out.println(conn.getHeaderField("Date"));
if you want to print the whole header:
Map headerfields = conn.getHeaderFields();
Set headers = headerfields.entrySet();
for(Iterator i = headers.iterator(); i.hasNext();){
Map.Entry map = (Map.Entry)i.next();
System.out.println(map.getKey() + " : " + map.getValue() + "");
}
After print the whole response header on Android 9. I found the following fields useful.
HttpURLConnection conn;
...
conn.getHeaderField("X-Android-Response-Source"); // "NETWORK 200" or "CACHE 200"
conn.getHeaderField("Warning"); // null or "110 HttpURLConnection "Response is stale""
It should have other status messages that I have not tested.
i hope you can help me with my problem. I have to instantiate an http connection and do a get call to the server.
After this call i have to check periodically if the server sent me some data (the connection must not be closed after the first data received). If it does, then i have to parse this data and pass it to an activity and wait again.
My problem is to understand if i'm doing correctly this thing. Here my code
try {
URL url = getUrl();
URLConnection urlConn = url.openConnection();
HttpURLConnection httpConnection = (HttpURLConnection) urlConn;
httpConnection.setRequestMethod("GET");
httpConnection.connect();
while(true) {
sleep(5000);
int responseCode = httpConnection.getResponseCode();
if(responseCode == HttpURLConnection.HTTP_OK) {
InputStream inputStream = httpConnection.getInputStream();
String response = null;
if(inputStream.available() > 0) {
long length = inputStream.available();
byte[] bytes = new byte[(int) length];
inputStream.read(bytes);
response = new String(bytes, "UTF-8");
}
}
}
} catch(Exception e) {
e.printStackTrace();
}
getUrl() function gives me the complete url that i have to call.
Then i connect through httpConnection.connect();.
while i'm iterating is the connection already opend and capable of receiving incoming data?
I apologize for my english. Thanks a lot
Francesco
From the official android documentation
Uses of this class follow a pattern:
Obtain a new HttpURLConnection by calling URL.openConnection() and
casting the result to HttpURLConnection.
Prepare the request. The
primary property of a request is its URI. Request headers may also
include metadata such as credentials, preferred content types, and
session cookies.
Optionally upload a request body. Instances must be
configured with setDoOutput(true) if they include a request body.
Transmit data by writing to the stream returned by getOutputStream().
Read the response. Response headers typically include metadata such as
the response body's content type and length, modified dates and
session cookies. The response body may be read from the stream
returned by getInputStream(). If the response has no body, that method
returns an empty stream.
Disconnect. Once the response body has been
read, the HttpURLConnection should be closed by calling disconnect().
Disconnecting releases the resources held by a connection so they may
be closed or reused.
As you see from this quote, if you need to send some data to a host you just need to call setDoOutput(true) and use OutputStream of your connection to send data to the host. In this case you use "POST" request method after you called setDoOutput(true). If you don't need to send data, but just to connect to the host and retrieve data, you also don't need to call setRequestMethod("GET"), because it's default request method.
When you connect to the host, just use InputStream to get data from it.
I am parsing Xml file located in web server and storing parsed data in to database. for my app I am using data from database. I need to parse the xml file only if the file is modified otherwise no need to parse. So how can I know the file is modified? I know I can use "if-modified-since" header. But
I need some examples of "if-modified-since" header
please help me.......
Since you are retrieving your .xml file from a web server, this should be relatively easy without having to do a server side MD5 sum.
If you are doing a HTTP request for the xml file you can simply perform a HEAD request from the web server and this will return if the file has changed/modified or if it doesn't exist. This is also lightweight and the best part is that the server should already do this for you.
Edit: re-reading your question, looks like you had the same idea. Here's the code.
import java.net.*;
import java.io.*;
// Using HTTP_NOT_MODIFIED
public static boolean Changed(String url){
try {
HttpURLConnection.setFollowRedirects(false);
HttpURLConnection con = (HttpURLConnection) new URL(url).openConnection();
con.setRequestMethod("HEAD");
return (con.getResponseCode() == HttpURLConnection.HTTP_NOT_MODIFIED);
}
catch (Exception e) {
e.printStackTrace();
return false;
}
}
// GET THE LAST MODIFIED TIME
public static long LastModified(String url)
{
HttpURLConnection.setFollowRedirects(false);
HttpURLConnection con = (HttpURLConnection) new URL(url).openConnection();
long date = con.getLastModified();
if (date == 0)
System.out.println("No last-modified information.");
else
System.out.println("Last-Modified: " + new Date(date));
return date;
}
See:
HttpURLConnection
HyperText_Transfer_Protocol HttpStatus 304 (Not Modified)
Alternatively if your server supports them you can use ETags to find out if your file has been modified.
http://www.xpertdeveloper.com/2011/03/last-modified-header-vs-expire-header-vs-etag/
Calculate the MD5 of the file. You can save the old one and compare it?
If you don't know how, check out this for example: Getting a File's MD5 Checksum in Java