MainActivity.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); final Button loadButton = (Button) findViewById(R.id.button1); loadButton.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { startActivity(new Intent(MainActivity.this, NetworkingAndroidHttpClientJSONActivity.class)); } }); } } |
NetworkingAndroidHttpClientJSONActivity.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 |
import java.io.IOException; import java.util.ArrayList; import java.util.List; import org.apache.http.HttpResponse; import org.apache.http.client.ClientProtocolException; import org.apache.http.client.ResponseHandler; import org.apache.http.client.methods.HttpGet; import org.apache.http.impl.client.BasicResponseHandler; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; import org.json.JSONTokener; import android.app.ListActivity; import android.net.http.AndroidHttpClient; import android.os.AsyncTask; import android.os.Bundle; import android.widget.ArrayAdapter; public class NetworkingAndroidHttpClientJSONActivity extends ListActivity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); new HttpGetTask().execute(); } private class HttpGetTask extends AsyncTask<Void, Void, List<String>> { // Get your own user name at http://www.geonames.org/login private static final String USER_NAME = "indrawdev"; private static final String URL = "http://api.geonames.org/earthquakesJSON?north=44.1&south=-9.9&east=-22.4&west=55.2&username=" + USER_NAME; AndroidHttpClient mClient = AndroidHttpClient.newInstance(""); @Override protected List<String> doInBackground(Void... params) { HttpGet request = new HttpGet(URL); JSONResponseHandler responseHandler = new JSONResponseHandler(); try { return mClient.execute(request, responseHandler); } catch (ClientProtocolException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return null; } @Override protected void onPostExecute(List<String> result) { if (null != mClient) mClient.close(); setListAdapter(new ArrayAdapter<String>( NetworkingAndroidHttpClientJSONActivity.this, R.layout.list_item, result)); } } private class JSONResponseHandler implements ResponseHandler<List<String>> { private static final String LONGITUDE_TAG = "lng"; private static final String LATITUDE_TAG = "lat"; private static final String MAGNITUDE_TAG = "magnitude"; private static final String EARTHQUAKE_TAG = "earthquakes"; @Override public List<String> handleResponse(HttpResponse response) throws ClientProtocolException, IOException { List<String> result = new ArrayList<String>(); String JSONResponse = new BasicResponseHandler() .handleResponse(response); try { // Get top-level JSON Object - a Map JSONObject responseObject = (JSONObject) new JSONTokener( JSONResponse).nextValue(); // Extract value of "earthquakes" key -- a List JSONArray earthquakes = responseObject .getJSONArray(EARTHQUAKE_TAG); // Iterate over earthquakes list for (int idx = 0; idx < earthquakes.length(); idx++) { // Get single earthquake data - a Map JSONObject earthquake = (JSONObject) earthquakes.get(idx); // Summarize earthquake data as a string and add it to // result result.add(MAGNITUDE_TAG + ":" + earthquake.get(MAGNITUDE_TAG) + "," + LATITUDE_TAG + ":" + earthquake.getString(LATITUDE_TAG) + "," + LONGITUDE_TAG + ":" + earthquake.get(LONGITUDE_TAG)); } } catch (JSONException e) { e.printStackTrace(); } return result; } } } |
activity_main.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity" > <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/load_data_string" > </Button> </RelativeLayout> |
list_item.xml
1 2 3 4 5 6 7 |
<?xml version="1.0" encoding="utf-8"?> <TextView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:padding="10dp" android:textSize="24sp" > </TextView> |