我正在尝试从http://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/4.5_day.geojson解析地震坐标并将其显示在文本视图中。但它不起作用......文本视图没有显示任何内容并保持空白..这是我的代码:
打包com.googlemaps;
import java.io.IOException;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
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.apache.http.util.EntityUtils;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.widget.TextView;
import android.widget.Toast;
public class Json extends Activity {
TextView txtJson;
HttpClient client;
JSONObject json;
String data;
final static String URL = "http://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/";
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.json);
txtJson = (TextView) findViewById(R.id.txtJson);
client = new DefaultHttpClient();
new Read().execute("features");
}
public JSONObject earthquake(String magnitude) throws ClientProtocolException, IOException, JSONException{
StringBuilder url = new StringBuilder(URL);
url.append(magnitude);
HttpGet get = new HttpGet(url.toString());
HttpResponse response = client.execute(get);
int statusCode = response.getStatusLine().getStatusCode();
if (statusCode == 200){
HttpEntity ent = response.getEntity();
String data= EntityUtils.toString(ent);
JSONObject main_obj = new JSONObject(data);
JSONArray list = main_obj.getJSONArray("features");
JSONObject latest = list.getJSONObject(0);
JSONObject geometry_obj = latest.getJSONObject("geometry");
return geometry_obj;
}
Toast.makeText(this,"failed" , Toast.LENGTH_SHORT).show();
return null;
}
public class Read extends AsyncTask<String, Integer, String>{
@Override
protected String doInBackground(String... arg0) {
// TODO Auto-generated method stub
String re = "";
try {
StringBuilder stringBuilder = new StringBuilder();
json = earthquake("4.5_day.geojson");
JSONArray coordinates_array = json.getJSONArray("coordinates");
for(int i = 0; i < coordinates_array.length(); i++){
stringBuilder.append("Some text"coordinates_array.getString(i));
}
re = stringBuilder.toString();
return re;
}
finally{}
}
@Override
protected void onPostExecute(String result) {
// TODO Auto-generated method stub
txtJson.setText(result);
}
}
}
这是XML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/txtJson"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Loading Data..."
android:textAppearance="?android:attr/textAppearanceLarge" />
</LinearLayout>
这是我的logcat数据:
02-08 04:23:58.908: I/Choreographer(2559): Skipped 91 frames! The application may be doing too much work on its main thread.
02-08 04:23:59.228: I/Choreographer(2559): Skipped 301 frames! The application may be doing too much work on its main thread.
02-08 04:24:24.608: I/Choreographer(2559): Skipped 77 frames! The application may be doing too much work on its main thread.
02-08 04:24:26.398: D/dalvikvm(2559): GC_FOR_ALLOC freed 375K, 14% free 3060K/3536K, paused 92ms, total 92ms
02-08 04:24:26.498: W/System.err(2559): org.json.JSONException: Value {"features": [{"id":"usc000mkv5","type":"Feature","geometry":{"type":"Point","coordinates":[159.5419,-9.7067,41.51]},"properties":{"detail":"http:\/\/earthquake.usgs.gov\/earthquakes\/feed\/v1.0\/detail\/usc000mkv5.geojson","type":"earthquake","net":"us","tsunami":null,"sources":",us,","title":"M 5.1 - 54km SW of Honiara, Solomon Islands","time":1391838013570,"updated":1391843853000,"mag":5.1,"types":",cap,dyfi,geoserve,nearby-cities,origin,phase-data,tectonic-summary,","place":"54km SW of Honiara, Solomon Islands","status":"reviewed","ids":",usc000mkv5,","alert":null,"rms":0.59,"code":"c000mkv5","url":"http:\/\/earthquake.usgs.gov\/earthquakes\/eventpage\/usc000mkv5","magType":"mb","mmi":null,"cdi":1,"tz":660,"felt":0,"nst":null,"dmin":9.404,"gap":94,"sig":400}},{"id":"usc000mktr","type":"Feature","geometry":{"type":"Point","coordinates":[141.0819,37.092,54.23]},"properties":{"detail":"http:\/\/earthquake.usgs.gov\/earthquakes\/feed\/v1.0\/detail\/usc000mktr.geojson","type":"earthquake","net":"us","tsunami":null,"sources":",us,","title":"M 5.0 - 18km ENE of Iwaki, Japan","time":1391826863590,"updated":1391828049000,"mag":5,"types":",cap,dyfi,general-link,geoserve,nearby-cities,origin,phase-data,tectonic-summary,","place":"18km ENE of Iwaki, Japan","status":"reviewed","ids":",usc000mktr,","alert":null,"rms":0.72,"code":"c000mktr","url":"http:\/\/earthquake.usgs.gov\/earthquakes\/eventpage\/usc000mktr","magType":"mb","mmi":null,"cdi":1,"tz":540,"felt":0,"nst":null,"dmin":2.369,"gap":123,"sig":385}},{"id":"usc000mksz","type":"Feature","geometry":{"type":"Point","coordinates":[54.2342,39.8122,10.69]},"properties":{"detail":"http:\/\/earthquake.usgs.gov\/earthquakes\/feed\/v1.0\/detail\/usc000mksz.geojson","type":"earthquake","net":"us","tsunami":null,"sources":",us,","title":"M 4.6 - 35km NNW of Balkanabat, Turkmenistan","time":1391821880490,"updated":1391850827048,"mag":4.6,"types":",cap,dyfi,geoserve,nearby-cities,origin,phase-data,tectonic-summary,","place":"35km NNW of Balkanabat, Turkmenistan","status":"reviewed","ids":",usc000mksz,","alert":null,"rms":1.36,"code":"c000mksz","url":"http:\/\/earthquake.usgs.gov\/earthquakes\/eventpage\/usc000mksz","magType":"mb","mmi":null,"cdi":null,"tz":300,"felt":null,"nst":null,"dmin":3.566,"gap":53,"sig":326}},{"id":"usc000mkir","type":"Feature","geometry":{"type":"Point","coordinates":[-72.9583,6.7107,157.61]},"properties":{"detail":"http:\/\/earthquake.usgs.gov\/earthquakes\/feed\/v1.0\/detail\/usc000mkir.geojson","type":"earthquake","net":"us","tsunami":null,"sources":",us,","title":"M 5.4 - 7km ENE of Aratoca, Colombia","time":1391805430940,"updated":1391822811919,"mag":5.4,"types":",cap,dyfi,general-link,geoserve,losspager,nearby-cities,origin,phase-data,shakemap,tectonic-summary,","place":"7km ENE of Aratoca, Colombia","status":"reviewed","ids":",usc000mkir,","alert":"green","rms":0.86,"code":"c000mkir","url":"http:\/\/earthquake.usgs.gov\/earthquakes\/eventpage\/usc000mkir","magType":"mb","mmi":4.02,"cdi":5,"tz":-300,"felt":151,"nst":null,"dmin":2.292,"gap":23,"sig":524}},{"id":"usc000mkes","type":"Feature","geometry":{"type":"Point","coordinates":[-179.784,-23.2075,544.06]},"properties":{"detail":"http:\/\/earthquake.usgs.gov\/earthquakes\/feed\/v1.0\/detail\/usc000mkes.geojson","type":"earthquake","net":"us","tsunami":null,"sources":",us,","title":"M 4.8 - South of the Fiji Islands","time":1391797565060,"updated":1391826517121,"mag":4.8,"types":",cap,dyfi,geoserve,nearby-cities,origin,phase-data,tectonic-summary,","place":"South of the Fiji Islands","status":"reviewed","ids":",usc000mkes,","alert":null,"rms":0.94,"code":"c000mkes","url":"http:\/\/earthquake.usgs.gov\/earthquakes\/eventpage\/usc000mkes","magType":"mb","mmi":null,"cdi":null,"tz":-720,"felt":null,"nst":null,"dmin":5.8,"gap":111,"sig":354}},{"id":"usc000mkcq","type":"Feature","geometry":{"type":"Point","coordinates":[141.3575,37.607,63.17]},"properties":{"detail":"http:\/\/earthquake.usgs.gov\/earthquakes\/feed\/v1.0\/detail\/usc000mkcq.geojson","ty
02-08 04:24:26.508: W/System.err(2559): at org.json.JSON.typeMismatch(JSON.java:111)
02-08 04:24:26.518: W/System.err(2559): at org.json.JSONArray.<init>(JSONArray.java:96)
02-08 04:24:26.518: W/System.err(2559): at org.json.JSONArray.<init>(JSONArray.java:108)
02-08 04:24:26.528: W/System.err(2559): at com.googlemaps.Json.earthquake(Json.java:50)
02-08 04:24:26.528: W/System.err(2559): at com.googlemaps.Json$Read.doInBackground(Json.java:66)
02-08 04:24:26.528: W/System.err(2559): at com.googlemaps.Json$Read.doInBackground(Json.java:1)
02-08 04:24:26.548: W/System.err(2559): at android.os.AsyncTask$2.call(AsyncTask.java:288)
02-08 04:24:26.548: W/System.err(2559): at java.util.concurrent.FutureTask.run(FutureTask.java:237)
02-08 04:24:26.548: W/System.err(2559): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
02-08 04:24:26.548: W/System.err(2559): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
02-08 04:24:26.558: W/System.err(2559): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
02-08 04:24:26.558: W/System.err(2559): at java.lang.Thread.run(Thread.java:841)
答案 0 :(得分:0)
试试这个..
if (statusCode == 200){
HttpEntity ent = response.getEntity();
String data= EntityUtils.toString(ent);
JSONObject main_obj = new JSONObject(data);
JSONArray list = main_obj.getJSONArray("features");
JSONObject latest = list.getJSONObject(0);
JSONObject geometry_obj = latest.getJSONObject("geometry");
return geometry_obj;
}
在 doInBackground
中String re = "";
try {
StringBuilder stringBuilder = new StringBuilder();
json = earthquake("4.5_day.geojson");
JSONArray coordinates_array = json.getJSONArray("coordinates");
for(int i = 0; i < coordinates_array.length(); i++){
stringBuilder.append(coordinates_array.getString(i)+",");
}
re = stringBuilder.toString();
return re;
}