2016-09-26 90 views
-8

問候!在我刪除硬編碼的json數據並移動到從url請求數據之後。我有異常錯誤。代碼與最終的官方git非常相似,但是我收到了錯誤。解析json結果時出錯?

我從json中提取數據的代碼是: private static final String USGS_REQUEST_URL = 「http://earthquake.usgs.gov/fdsnws/event/1/query?format=geojson&orderby=time&minmag=5&limit=10」;

// 
    public static List<Earthquake> extractFeaturesfromJson(String earthquakeJSON) { 
     /*if the json is null then return earlu*/ 
     if (TextUtils.isEmpty(earthquakeJSON)) { 
      return null; 
     } 

     // Create an empty ArrayList that we can start adding earthquakes to 
     List<Earthquake> earthquakes = new ArrayList<>(); 

     // Try to parse the JsonResponseString. If there's a problem with the way the JSON 
     // is formatted, a JSONException exception object will be thrown. 
     // Catch the exception so the app doesn't crash, and print the error message to the logs. 
     try { 
      // create an object form jsonString 
      JSONObject root = new JSONObject(earthquakeJSON); 
      JSONArray features = root.getJSONArray("features"); 

      for (int i = 0; i < features.length(); i++) { 
       // Get a single earthquake at position i within the list of earthquakes 
       JSONObject currentEarthquake = features.getJSONObject(i); 
       // For a given earthquake, extract the JSONObject associated with the 
       // key called "properties", which represents a list of all properties 
       // for that earthquake. 
       JSONObject properties = currentEarthquake.getJSONObject("properties"); 

       double mag = properties.getDouble("mag"); 
       String location = properties.getString("place"); 
       long time = properties.getLong("time"); 

       //extract the value of key url 
       String url = properties.getString("url"); 
       //create new object with magnitude, location ane time and url from json response 
       Earthquake earthquake = new Earthquake(mag, location, time, url); 
       earthquakes.add(earthquake); 


      } 


     } catch (JSONException e) { 
      // If an error is thrown when executing any of the above statements in the "try" block, 
      // catch the exception here, so the app doesn't crash. Print a log message 
      // with the message from the exception. 
      Log.e("QueryUtils", "Problem parsing the earthquake JSON results", e); 
     } 

     // Return the list of earthquakes 
     return earthquakes; 
    } 

的logcat中顯示:

09-26 14:49:23.628 2551-2584/com.example.android.quakereport E/com.example.android.quakereport.QueryUtils: Problem retrieving the earthquake JSON results. 
+0

請提供您得到的異常 – anddevmanu

+1

請張貼錯誤的logcat日誌中,JSON數據你正在解析和你用來解析JSON的代碼。 –

+0

我看到你使用'索引10超出範圍[0..10]'也許你用循環獲取對象?如果是這樣,你應該檢查你的循環索引。 – Numb

回答

0

從您的網址JSON數據格式不正確。將您從url中收到的內容與硬編碼數據進行比較。

+0

url和硬編碼的json數據是相同的http://earthquake.usgs.gov/fdsnws/event/1/query?format=geojson&orderby=time&minmag=5&limit=10「。上面的json提取器代碼中是否存在錯誤? –

0

完成後臺工作後,此方法在主UI線程上運行。該方法接收來自doInBackground()方法的返回值作爲輸入。

首先我們清除適配器,以擺脫以前向USGS詢問的 地震數據。

然後我們用新的地震列表更新適配器,這將觸發ListView重新填充它的列表項。 但是在將數據填充到適配器時出錯。

 @Override 
     protected void onPostExecute(List<Earthquake> data) { 
      //clear the adapter of previdous earthquake data 
      mAdapter.clear(); 
      if (data != null && data.isEmpty()) { //====?????shourld be !data.isEmpty()) 
       mAdapter.addAll(data); 
      } 
     } 

真正的問題是onPostExecute方法來填充mainthread在後臺方法做後的數據。

+1

您的日誌顯示:「09-26 14:49:23.628 2551-2584/com.example.android.quakereport E/com.example.android.quakereport.QueryUtils:**檢索地震JSON結果的問題**」 - 當然它與解析無關。 –

0

如果你正在參加Udacity android課程,並遇到quakereport/DidUfeelIt應用程序的這個錯誤,那麼更改URL並嘗試使用其他URL解決您的問題。 例如: - 過程中所提供的網址是 「http://earthquake.usgs.gov/fdsnws/event/1/query?format=geojson&starttime=2012-01-01&endtime=2012-12-01&minmagnitude=6

然後我得到了相同的是「問題解析JSON」 所以我嘗試了不同的URL錯誤: https://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/4.5_day.geojson

它工作.. !! 在課程期間,務必嘗試從USGS網站獲取最新的URL。