2016-09-26 16 views
-8

挨拶!ハードコードされたjsonデータを削除した後、urlからデータを要求するように移動しました。例外エラーが発生しています。コードは最終的な公式なgitとほとんど同じですが、エラーが発生しています。json結果の解析中にエラーが発生しましたか?

私はjsonからデータを抽出しているコードです: プライベート静的最終文字列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

私はあなたがオブジェクトを取得するためにループのために使用する 'Index 10 of range [0..10]'を使用するのを見ますか?本当の場合は、ループのインデックスをチェックする必要があります。 – Numb

答えて

0

あなたのURLからJSONデータが正しくフォーマットされていません。あなたが受け取ったURLとハードコードされたデータを比較してください。

+0

URLとハードコードされたjsonデータは同じですhttp://earthquake.usgs.gov/fdsnws/event/1/query?format=geojson&orderby=time&minmag=5&limit=10 "上記のjson抽出プログラムコードにバグがありますか? –

-1

オンラインのJson Validatorを使用してください。 http://jsonlint.com/ 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); 
      } 
     } 

本当の問題は、バックグラウンドの方法で行った後mainthread内のデータを移入するonPostExecute方法でした。

+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を試してください。問題が解決されます。 、例えば: - 途中で提供された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を入手しようとします。

関連する問題