Using GeoNames WebServices, we can get time zone of any location. For example, the query:
http://api.geonames.org/timezoneJSON?formatted=true&lat=40.75649&lng=-73.98626&username=demo
return the time zone of the location of lat:40.75649 lng:-73.98626 (Nex York).
{
"time": "2011-08-10 10:07",
"countryName": "United States",
"sunset": "2011-08-10 20:01",
"rawOffset": -5,
"dstOffset": -4,
"countryCode": "US",
"gmtOffset": -5,
"lng": -73.98626,
"sunrise": "2011-08-10 06:00",
"timezoneId": "America/New_York",
"lat": 40.75649
}
Please note that the parameter 'username' needs to be passed with each request. The username for your application can be registered here.
We can modify the exercise of "Parse JSON returned from Flickr Services" to parse the result to get the time zone.
package com.exercise.AndroidTimeZone;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import org.apache.http.HttpEntity;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class AndroidTimeZoneActivity extends Activity {
TextView textTimeZone;
EditText inputLat, inputLon;
Button buttonGetTimeZone;
final static String DEFAULT_LAT = "40.75649";
final static String DEFAULT_LON = "-73.98626";
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
textTimeZone = (TextView)findViewById(R.id.timezone);
inputLat = (EditText)findViewById(R.id.lat);
inputLat.setText(DEFAULT_LAT);
inputLon = (EditText)findViewById(R.id.lon);
inputLon.setText(DEFAULT_LON);
buttonGetTimeZone = (Button)findViewById(R.id.getTimeZone);
buttonGetTimeZone.setOnClickListener(buttonGetTimeZoneOnClickistener);
}
Button.OnClickListener buttonGetTimeZoneOnClickistener
= new Button.OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
String lat = inputLat.getText().toString();
String lon = inputLon.getText().toString();
String rqsurl = "http://api.geonames.org/timezoneJSON?formatted=true"
+ "&lat=" + lat
+ "&lng=" + lon
+ "&username=demo";
String jsonResult = QueryGeonames(rqsurl);
String parsedResult = ParseJSON(jsonResult);
textTimeZone.setText(parsedResult);
}};
private String QueryGeonames(String q){
String qResult = null;
HttpClient httpClient = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(q);
try {
HttpEntity httpEntity = httpClient.execute(httpGet).getEntity();
if (httpEntity != null){
InputStream inputStream = httpEntity.getContent();
Reader in = new InputStreamReader(inputStream);
BufferedReader bufferedreader = new BufferedReader(in);
StringBuilder stringBuilder = new StringBuilder();
String stringReadLine = null;
while ((stringReadLine = bufferedreader.readLine()) != null) {
stringBuilder.append(stringReadLine + "\n");
}
qResult = stringBuilder.toString();
}
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return qResult;
}
private String ParseJSON(String json){
String jResult = null;
try {
JSONObject JsonObject = new JSONObject(json);
jResult = "\n"
+ "lat: " + JsonObject.getString("lat") + "\n"
+ "lng: " + JsonObject.getString("lng") + "\n"
+ "countryName: " + JsonObject.getString("countryName") + "\n"
+ "countryCode: " + JsonObject.getString("countryCode") + "\n"
+ "timezoneId: " + JsonObject.getString("timezoneId") + "\n"
+ "rawOffset: " + JsonObject.getString("rawOffset") + "\n"
+ "dstOffset: " + JsonObject.getString("dstOffset") + "\n"
+ "gmtOffset: " + JsonObject.getString("gmtOffset") + "\n";
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return jResult;
}
}
Layout, main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="lat"
/>
<EditText
android:id="@+id/lat"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="lon"
/>
<EditText
android:id="@+id/lon"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<Button
android:id="@+id/getTimeZone"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Get Time Zone from geonames.org"
/>
<TextView
android:id="@+id/timezone"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
Please remember you you have to modify AndroidManifest.xml to grant permission of "android.permission.INTERNET".
Download the files.
No comments:
Post a Comment