2016-09-26 92 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。