Android: how to know file located in web server is modified? - android

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

Related

How to Use Both HTTPS and HTTP to parse JSON data in Android?

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

Rails RESTful API connection with android

Hi i have been learning rails for a bit , now i need to make some mobile applications android & IOS connecting to my webapp, I know that the API is used to make the connections like the endpoints / url and then the methods for each and everyone of them is used to make it, my problem how can i know or test that my API for the mobile is working properly?
I am currently doing the android par and i don't know where to start with that. can guys also help with pointing out where to learn them, i mean creating user's in my webapp , I used device so i need to make an android application to make user's , insert data into my PG(postgresql) , get some geolocaion (geocoder), from the webapp , but i don't know where to start with the android how to send and get data from that, also how to test if the API that i made is working properly....
any help willbe greatly appreciated specially links to tutorials for rails version 4.+
I'm showing you the simplest way to do this things.
First make a json formatted api using rails. And you can make a URL that can accept GET method value(www.exmp.com?val=asd).
In your android application make a Class name JsonParser.
copy and paste the following code.
private String jsonurl;
public JsonParser(String url) {
this.jsonurl = url;
}
public String fetchJSON(){
try {
URL url = new URL(jsonurl);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setReadTimeout(15000);
conn.setConnectTimeout(25000);
conn.setRequestMethod("GET");
conn.setDoInput(true);
conn.connect();
InputStream stream = conn.getInputStream();
String data = convertStreamToString(stream);
return data;
} catch (Exception e) {
Log.d("Check", e.getMessage());
return null;
}
}
private String convertStreamToString(java.io.InputStream is) {
java.util.Scanner s = new java.util.Scanner(is).useDelimiter("\\A");
return s.hasNext() ? s.next() : "";
}
After that make a Object of that class in a Asynctask thread. and also give the URL as a parameter.you will get the Json formatted data.
To get the actual data you need to make a JSONObject type object and call like this .
JSONObject reader = new JSONObject(jsonData); // returning json formatted data
this.status = reader.getString("status");
Now you can make java Bean to get or set the value for further uses.

When is it necessary to specific application/json Content-Type explicitly

Currently, I'm building a Android mobile app & Python restful server services.
I found that, it makes no different, whether or not I'm using
self.response.headers['Content-Type'] = "application/json"
The following code (which doesn't specific Content-Type explicitly) works fine for me. I was wondering, in what situation, I should specific Content-Type explicitly?
Python restful server services code
class DebugHandler(webapp2.RequestHandler):
def get(self):
response = {}
response["key"] = "value"
self.response.out.write(json.dumps(response))
application = webapp2.WSGIApplication([
('/debug', DebugHandler),
], debug = True)
Android mobile app client code
public static String getResponseBodyAsString(String request) {
BufferedReader bufferedReader = null;
try {
URL url = new URL(request);
HttpURLConnection httpURLConnection = (HttpURLConnection)url.openConnection();
initHttpURLConnection(httpURLConnection);
InputStream inputStream = httpURLConnection.getInputStream();
bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
int charRead = 0;
char[] buffer = new char[8*1024];
// Use StringBuilder instead of StringBuffer. We do not concern
// on thread safety. stringBuffer = new StringBuffer();
StringBuilder stringBuilder = new StringBuilder();
while ((charRead = bufferedReader.read(buffer)) > 0) {
stringBuilder.append(buffer, 0, charRead);
}
return stringBuilder.toString();
} catch (MalformedURLException e) {
Log.e(TAG, "", e);
} catch (IOException e) {
Log.e(TAG, "", e);
} finally {
close(bufferedReader);
}
return null;
}
Content-Type specifies what's inside the response (i.e. how to interpret the body of the response). Is it JSON, a HTML document, a JPEG, etc? It is useful when you have different representations of your resources and together with Accept it's a header involved in doing content negotiation between client and server.
Different clients might need different formats. A C# client might prefer XML, a Javascript client might prefer JSON, another client could work with multiple representations but try to request the most efficient one first and then settle for others if the server can't serve the preferred one, etc.
Content-Type is very important in the browser so that the user agent knows how to display the response. If you don't specify one the browser will try to guess, usually based on the extension and maybe fallback to some Save as... dialog if that fails also. In a browser, the lack of a Content-Type might cause some HTML to open a Save as... dialog, or a PDF file to be rendered as gibberish in the page.
In an application client, not having a Content-Type might cause a parsing error or might be ignored. If you server only serves JSON and your client only expects JSON then you can ignore the Content-Type, the client will just assume it's JSON because that's how it was built.
But what if at some point you want to add XML as a representation, or YAML or whatever? Then you have a problem because the client assumed it's always JSON and ignored the Content-Type. Now when it receives XML it will try to parse as JSON and fail. If instead the client was built with content types in mind and you always specify a Content-Type then your client will then take it into account and select an appropriate parser instead of blindly making assumptions.

FileNotFoundException when using the offline cache of HttpResponsecache

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.

Send JSON request - JQuery analog

I need to send a JSON request similar to jQuery's ajax method.
The official documentation quote on the data parameter says:
If value is an Array, jQuery serializes multiple values with same key based on the value of the traditional setting
So I have the same situation - a key that maps to an array "parameters":[123123, {"category":"123"}]
The complete data parameter looks like
$.ajax({
url: "/api/",
data: {"parameters":[123123, {"category":"123"}], "anotherParameter":"anotherValue"}
Would you mind telling how to achieve the same functionality in Java ?
UPD:
I've made it to work with the use of gson + collections + post request.
Here're some guide lines:
Sending POST requests in JAVA.
You might also want to consider performance issues related to different kinds of json parsers
What have you tried so far and what exactly is the problem? Is it that you don't know how to create a connection, that you don't know how to serialize your objects or that you don't know how to make the thing run asynchronously?
I don't think Java has any asynchronous HTTP call methods, you need to use a regular URLConnection and run it in a separate thread. The connection itself is formed like this:
URL url = null;
URLConnection urlConnection = null;
String myURL = "http://example.com/ajax.php?foo=bar";
try {
url = new URL(myURL);
urlConnection = url.openConnection();
} catch (Exception e) {
e.printStackTrace();
return;
}
InputStream responseStream = null;
try {
responseStream = urlConnection.getInputStream();
} catch (IOException e) {
e.printStackTrace();
return;
}
Then you must use something like the AsyncTask class to make it a background job.
For converting your objects to JSON you might want to use the GSON library.

Categories

Resources