I have json that looks likes this:
{"type":"FeatureCollection","metadata":{"generated":1428783374000,"url":"http://earthquake.usgs.gov/fdsnws/event/1/query?format=geojson&starttime=2015-04-04&endtime=2015-04-05","title":"USGS Earthquakes","status":200,"api":"1.0.17","count":328},"features":[{"type":"Feature","properties":{"mag":1.3,"place":"8km SSE of Talmage, California","time":1428191575500,"updated":1428359465102,"tz":-420,"url":"http://earthquake.usgs.gov/earthquakes/eventpage/nc72426686","detail":"http://earthquake.usgs.gov/fdsnws/event/1/query?eventid=nc72426686&format=geojson","felt":null,"cdi":null,"mmi":null,"alert":null,"status":"reviewed","tsunami":0,"sig":26,"net":"nc","code":"72426686","ids":",nc72426686,","sources":",nc,","types":",cap,general-link,geoserve,nearby-cities,origin,phase-data,scitech-link,tectonic-summary,","nst":12,"dmin":0.08508,"rms":0.06,"gap":154,"magType":"md","type":"earthquake","title":"M 1.3 - 8km SSE of Talmage, California"},"geometry":{"type":"Point","coordinates":[-123.1233333,39.0688333,7.27]},"id":"nc72426686"},
My Main Activity code looks like this:
public class MainActivity extends ActionBarActivity {
private static final String LOG_TAG ="JSON STRING";
TextView httpData;
HttpClient client;
JSONObject json;
final static String URL = "http://earthquake.usgs.gov/fdsnws/event/1/query?format=geojson&starttime=2015-04-04&endtime=2015-04-05";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
httpData = (TextView) findViewById(R.id.tvHttp);
client = new DefaultHttpClient();
new Read().execute("features");
}
public JSONObject lastEvent(String event) throws ClientProtocolException, IOException, JSONException {
StringBuilder url = new StringBuilder(URL);
HttpGet get = new HttpGet(url.toString());
HttpResponse response = client.execute(get);
int status = response.getStatusLine().getStatusCode();
if ( status == 200 ) {
HttpEntity entity = response.getEntity();
String data = EntityUtils.toString(entity);
JSONArray timeline = new JSONArray(data);
JSONObject last = timeline.getJSONObject(0);
return last;
}
else {
Toast.makeText(MainActivity.this, "error", Toast.LENGTH_SHORT);
return null;
}
}
public class Read extends AsyncTask<String, Integer, String> {
@Override
protected String doInBackground(String... params) {
try {
json = lastEvent("title");
return json.getString(params[0]);
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(String result ) {
httpData.setText(result);
}
}
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
My activity_main.xml looks like this:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="wrap_content"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">
<ScrollView android:layout_height="fill_parent" android:layout_width="fill_parent">
<TextView android:text="Loading Data" android:id="@+id/tvHttp" android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</ScrollView>
I am trying to get the value of 'title' from the first element of the JSONArray, but instead I just get a blank Text View screen. I am somewhat new to Java and Android in general, so if a different format (such as XML) is easier for me to work with, I can try that too. Any insight or hints is greatly appreciated.
JSONObjectand then you can usegetString("title"). You however create aJSONArray.featuresarray?