2017-10-31 12 views
0

Volleyを使用してTFL(Transport for London)APIからJSONを取得できません。私は別のAPIとすべての作品で自分のコードをテストしました。 API URLは正常に動作しています - 私はPostmanでURLをテストしました。 Logcatが表示されず、赤色のエラーが発生しました。コード& logcatをご覧ください。なぜVolleyはAPIから情報を返さないのですか?

tempTextView = (TextView) findViewById(R.id.tempTextView); 

    String url = "https://api.tfl.gov.uk/Line/Victoria"; 

    JsonObjectRequest jsObjRequest = new JsonObjectRequest 
      (Request.Method.GET, url, null, new Response.Listener<JSONObject>() { 

       @Override 
       public void onResponse(JSONObject response) { 
        // tempTextView.setText("Response: " + response.toString()); 

        Log.e("status", "Response: " + tempTextView.toString()); 

        try { 
         JSONObject statusJSONObject = response.getJSONObject("lineStatuses"); 
         String status = statusJSONObject.getString("statusSeverityDescription"); 
         tempTextView.setText(status); 

        } catch (JSONException e) { 
         e.printStackTrace(); 
        } 
       } 
      }, new Response.ErrorListener() { 

       @Override 
       public void onErrorResponse(VolleyError error) { 
        // TODO Auto-generated method stub 

       } 
      }); 

     // Access the RequestQueue through your singleton class. 
     RequestQueue queue = Volley.newRequestQueue(this); 
     queue.add(jsObjRequest); 
} 

logcat(更新)

10-31 19:24:10.410 18618から18618/com.example.aaron.itubeのE/TAG:エラー応答: com.android.volley.ParseError: org.json.JSONException:Value [{"$ type": "Tfl.Api.Presentation.Entities.Line、Tfl.Api.Presentation.Entities"、 "id": "northern"、 "name": "Northern"、 "modeName": "tube"、 "disruptions":[]、 "created": "2017-10-31T10:48:22.99Z"、 "modified": "2017-10-31T10:48:22.99Z" "、" statusSeverity ":10、" statusSeverityDescription ":"良いサービス ":" {"" $ type ":" Tfl.Api.Presentation.Entities.LineStatus、Tfl.Api.Presentation.Entities "、" id " 、 "created": "0001-01-01T00:00:00"、 "validityPeriods":[]}]、 "routeSections "、"サービスタイプ ":[{" "$ type": "Tfl.Api.Presentation.Entities.LineServiceTypeInfo、Tfl.Api.Presentation.Entities"、 "name": "Regular"、 "uri": "/" ids = Northern & serviceTypes = Regular "}、{" $ type ":" Tfl.Api.Presentation.Entities.LineServiceTypeInfo、Tfl.Api.Presentation.Entities "、" name ":" Night "、" uri ":"/Line/Route?ids = Northern & serviceTypes = Night "}"、 "crowding":{"$ type": "Tfl.Api.Presentation.Entities.Crowding、Tfl.Api.Presentation.Entities"}} ]タイプのorg.json.JSONArrayはJSONObjectに変換できません com.android.volley.toolbox.JsonObjectRequest.parseNetworkResponse(JsonObjectRequest.java:73) com.android.volley.NetworkDispatcher.run(NetworkDispatcher.java:123 ) 原因:org.json.JSONException:値[{"$ type": "Tfl.Api.Presentation.Entities.Line、Tfl.Api.Presentation.Entities"、 "i" d ":"北 "、"名前 ":"北 "、"モード名 ":"チューブ "、"混乱 ":[]、"作成済み ":" 2017-10-31T10:48:22.99Z " : "2017-10-31T10:48:22.99Z"、 "lineStatuses":[{"$ type": "Tfl.Api.Presentation.Entities.LineStatus、Tfl.Api.Presentation.Entities"、 "id":0 、 "statusSeverity":10、 "statusSeverityDescription": "Good Service"、 "created": "0001-01-01T00:00:00"、 "validityPeriods":[]}]、 "routeSections":[]、 "serviceTypes ":" {"" $ type ":" Tfl.Api.Presentation.Entities.LineServiceTypeInfo、Tfl.Api.Presentation.Entities "、" name ":" Regular "、" uri ":"/Line/Route?ids = Northern & serviceTypes = Regular "}、{" $ type ":" Tfl.Api.Presentation.Entities.LineServiceTypeInfo、Tfl.Api.Presentation.Entities "、" name ":" Night "、" uri ":"/Line/Route ?ids = Northern & serviceTypes = Night "}]、" crowding ":{" $ type ":" Tfl.Api.Presentation.Entities.Crowding、Tfl.Api.Presentation.Entities "}}タイプorg.json。 JSONArrayを組織のJSONObject に変換できません.json.JSON.typeMismatch(JSON.java:111) at org.json.JSONObject。(JSONObject.java:160) at org.json.JSONObject。(JSONObject.java:173) at com.android.volley com.android.volley.NetworkDispatcher.run(NetworkDispatcher.java:123)で.toolbox.JsonObjectRequest.parseNetworkResponse(JsonObjectRequest.java:68) 探しのための

感謝。開始エラーをログに記録するonErrorResponse方法でいくつかのコードを記述するために

+0

に変更します。おそらく、「onErrorResponse」を実装しますか?そしてエラーを見なさい???また、開発ツールとしてAndroid Studioを使用していることを願っています。 – EpicPandaForce

+0

アドバイスありがとうございます。提案どおりに完了します。現在のlogcat出力を含めるように質問を編集しました。乾杯。 –

答えて

1

は次のように受け取ら:

@Override 
public void onErrorResponse(VolleyError error) { 
    Log.e("TAG", "Error response:", error); 

} 

そして、あなたの質問にエラーを更新して投稿してください。

UPDATE

ここで更新logcatから手がかりof type org.json.JSONArray cannot be converted to JSONObjectです。受け取った応答はjsonarrayであり、jsonobjectではありません。電話番号を

JsonArrayRequest jsArrayRequest = new JsonArrayRequest 
     (Request.Method.GET, url, null, new Response.Listener<JSONArray>() { 

      @Override 
      public void onResponse(JSONArray response) { 
       // place your code here, mind now this is a `JSONArray` and not a `JSONObject` 

      } 
     }, new Response.ErrorListener() { 

      @Override 
      public void onErrorResponse(VolleyError error) { 
       Log.e("TAG", "Error response:", error); 

      } 
     }); 
    // Access the RequestQueue through your singleton class. 
    RequestQueue queue = Volley.newRequestQueue(this); 
    queue.add(jsArrayRequest); 
+0

アドバイスをいただきありがとうございます。提案どおりに完了します。現在のlogcat出力を含めるように質問を編集しました。乾杯。 –

+0

それに応じて私の答えを更新する – pleft

関連する問題