2014-07-19 26 views
0

我試圖找回GeoJSON數據在我的地震警報的應用程序自定義listview顯示,但應用程序意外關閉。這裏是我的代碼:代碼檢索GeoJSON的數據不能正常工作

CODE

public class JSONActivity extends ListActivity {  
ListAdapter adapter; 
String data; 
private ProgressDialog pDialog; 
ListView listview; 

private static final String TAG_FEATURES= "features"; 
private static final String TAG_PROPERTIES= "properties"; 
private static final String TAG_MAG= "mag"; 
private static final String TAG_PLACE= "place"; 
private static final String TAG_TIME= "time"; 
private static final String TAG_TYPE= "type"; 

JSONArray features = null; 

ArrayList<HashMap<String, String>> EarthquakeList; 

public String readJSONFeed(String URL){  

    StringBuilder stringBuilder= new StringBuilder(); 
    HttpClient client= new DefaultHttpClient(); 
    HttpGet httpGet = new HttpGet(URL); 
    try{ 

     HttpResponse response= client.execute(httpGet); 
     StatusLine statusLine= response.getStatusLine(); 
     int statusCode = statusLine.getStatusCode(); 
     if(statusCode == 200){ 

      HttpEntity entity= response.getEntity(); 
      data= EntityUtils.toString(entity); 

      InputStream content = entity.getContent(); 
      BufferedReader reader = new BufferedReader(new InputStreamReader(content)); 
      String line; 
      while((line= reader.readLine())!= null) 
      { 

       stringBuilder.append(line); 
      } 

     }else{ 
      Log.e("JSON", "Failed to download file"); 
     } 

    }catch(ClientProtocolException e){ 
     e.printStackTrace(); 
    }catch(IOException e){ 
     e.printStackTrace(); 
    } 

    return stringBuilder.toString(); 

} 

private class ReadJSONFeedTask extends AsyncTask<String, Void, String>{ 

// @Override 
    protected String doInBackground(String... urls) { 

     return readJSONFeed(urls[0]); 
    } 

    @Override 
    protected void onPreExecute() { 
     super.onPreExecute(); 

     pDialog = new ProgressDialog(JSONActivity.this); 
     pDialog.setMessage("Please wait..."); 
     pDialog.setCancelable(false); 
     pDialog.show(); 
    }  

protected void onPostExecute(String result){ 

    if (pDialog.isShowing()) 
     pDialog.dismiss(); 
    try{    

     JSONObject jsonObj = new JSONObject(data); 
     features = jsonObj.getJSONArray("features"); 
     for (int i = 0; i < features.length(); i++) 
     { 

      JSONObject c = features.getJSONObject(i); 

       // Properties node is JSON Object 
      JSONObject properties = c.getJSONObject(TAG_PROPERTIES); 
      String mag = properties.getString(TAG_MAG); 
      String place = properties.getString(TAG_PLACE); 
      String type = properties.getString(TAG_TYPE); 
      String time = properties.getString(TAG_TIME); 

        Toast.makeText(getBaseContext(),mag,3000).show(); 
      // tmp hash map for single contact 
      HashMap<String, String> earthquake = new HashMap<String, String>(); 

      // adding each child node to HashMap key => value 
      earthquake.put(TAG_MAG, mag); 
      earthquake.put(TAG_PLACE, place); 
      earthquake.put(TAG_TYPE, type); 
      earthquake.put(TAG_TIME,time); 

      EarthquakeList.add(earthquake); 

      /** 
      * Updating parsed JSON data into ListView 
      * */ 


      adapter = new SimpleAdapter(
        JSONActivity.this , EarthquakeList , 
          R.layout.list_item , new String[] { TAG_MAG, TAG_PLACE, 
            TAG_TIME,TAG_TYPE }, new int[] { R.id.mag, 
            R.id.place, R.id.time,R.id.type }); 
      setListAdapter(adapter);    
     }   
    }catch(Exception e) 
    { 
     e.printStackTrace(); 
    }    
} 

} 

/** Called when the activity is first created. */ 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 
    listview= (ListView)findViewById(R.id.listView1); 
    EarthquakeList = new ArrayList<HashMap<String, String>>();  

    listview.setOnItemClickListener(new OnItemClickListener() { 
     public void onItemClick(AdapterView<?> parent, View view, 
      int position, long id) { 

     Intent i = new Intent(getApplicationContext(), SingleListItem.class); 
     startActivity(i); 
     }}); 
    new ReadJSONFeedTask().execute("http://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/4.5_day.geojson"); 

} 
} 

這是main.xml中

CODE

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:orientation="vertical" > 

<TextView 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="@string/hello" /> 

<ListView 
    android:id="@+id/listView1" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" > 
</ListView> 

這是list_item.xml CODE

<TextView 
    android:id="@+id/mag" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:paddingBottom="2dip" 
    android:paddingTop="6dip" 
    android:textColor="#43bd00" 
    android:textSize="16sp" 
    android:textStyle="bold" 
    /> 
<TextView 
    android:id="@+id/place" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:paddingBottom="2dip" 
    android:paddingTop="6dip" 
    android:textColor="#43bd00" 
    android:textSize="16sp" 
    android:textStyle="bold" 
    android:text="Place : " /> 


<TextView 
    android:id="@+id/time" 
    android:text="Time: " 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:paddingBottom="2dip" 
    android:textColor="#acacac" /> 


<TextView 
    android:id="@+id/type" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:gravity="left" 
    android:text="Type: " 
    android:textColor="#5d5d5d" 
    android:textStyle="bold" /> 

logcat的

07-19 12:30:49.302: E/AndroidRuntime(279): FATAL EXCEPTION: main 
07-19 12:30:49.302: E/AndroidRuntime(279): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.JSON/com.JSON.JSONActivity}: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list' 
07-19 12:30:49.302: E/AndroidRuntime(279): at ``android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663) 
07-19 12:30:49.302: E/AndroidRuntime(279): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679) 
07-19 12:30:49.302: E/AndroidRuntime(279): at android.app.ActivityThread.access$2300(ActivityThread.java:125) 
07-19 12:30:49.302: E/AndroidRuntime(279): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033) 
07-19 12:30:49.302: E/AndroidRuntime(279): at android.os.Handler.dispatchMessage(Handler.java:99) 
07-19 12:30:49.302: E/AndroidRuntime(279): at android.os.Looper.loop(Looper.java:123) 
07-19 12:30:49.302: E/AndroidRuntime(279): at android.app.ActivityThread.main(ActivityThread.java:4627) 
07-19 12:30:49.302: E/AndroidRuntime(279): at java.lang.reflect.Method.invokeNative(Native Method) 
07-19 12:30:49.302: E/AndroidRuntime(279): at java.lang.reflect.Method.invoke(Method.java:521) 
07-19 12:30:49.302: E/AndroidRuntime(279): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868) 
07-19 12:30:49.302: E/AndroidRuntime(279): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626) 
07-19 12:30:49.302: E/AndroidRuntime(279): at dalvik.system.NativeStart.main(Native Method) 
07-19 12:30:49.302: E/AndroidRuntime(279): Caused by: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list' 
07-19 12:30:49.302: E/AndroidRuntime(279): at android.app.ListActivity.onContentChanged(ListActivity.java:245) 
07-19 12:30:49.302: E/AndroidRuntime(279): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:201) 
07-19 12:30:49.302: E/AndroidRuntime(279): at android.app.Activity.setContentView(Activity.java:1647) 
07-19 12:30:49.302: E/AndroidRuntime(279): at com.JSON.JSONActivity.onCreate(JSONActivity.java:187) 
07-19 12:30:49.302: E/AndroidRuntime(279): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047) 
07-19 12:30:49.302: E/AndroidRuntime(279): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627) 
07-19 12:30:49.302: E/AndroidRuntime(279): ... 11 more 
+0

解決在你的web服務器部分的GeoJSON的MIME類型? – Wanderer

+0

請加你堆棧跟蹤,並提供進一步的信息.. – lukaswelte

回答

0

是否包含.geojson到您的MIME類型將您的Web服務器上解決?

如果沒有,你可能你可以添加你登錄`需要cat`編輯web.config中通過

<mimeMap fileExtension=".geojson" mimeType="application/json" />