为什么Volley没有从API返回任何信息?

时间:2017-10-31 18:05:35

标签: android json android-volley

我无法使用Volley从TFL(Transport for London)API中检索JSON。我已经使用不同的API测试了我的代码,并且完美无缺。 API URL工作正常 - 我已经使用Postman测试了URL。 Logcat没有显示和红色错误..请参阅代码& logcat下面。

 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”, “名称”: “北”, “modeName”: “管”, “中断”:[], “创建”: “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&amp; serviceTypes = Regular“}, {“$ type”:“Tfl.Api.Presentation.Entities.LineServiceTypeInfo,Tfl.Api.Presentation.Entities”,“name”:“Night”,“uri”:“/ Line / Route?ids = Northern&amp; serviceTypes =夜“}},”拥挤“:{”$ type“:”Tfl.Api.Presentation.Entities.Crowding,Tfl.Api.Presentation.Entities“}}]类型为org.json.JSONArray canno转换为JSONObject                                                                   在com.android.volley.toolbox.JsonObjectRequest.parseNetworkResponse(JsonObjectRequest.java:73)                                                                   在com.android.volley.NetworkDispatcher.run(NetworkDispatcher.java:123)                                                                引起:org.json.JSONException:Value [{“$ type”:“Tfl.Api.Presentation.Entities.Line,Tfl.Api.Presentation.Entities”,“id”:“northern”,“name”:“北部 “ ”modeName“: ”管“, ”中断“:[], ”创建“: ”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“:”良好的服务“,”创建“:”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&amp; serviceTypes = Regular“},{”$ type“ :“Tfl.Api.Presentation.Entities.LineServiceTypeInfo,Tfl.Api.Presentation.Entities”,“name”:“Night”,“uri”:“/ Line / Route?ids = Northern&amp; serviceTypes = Night”}], “拥挤”:类型为org.json.JSONArray的{“$ type”:“Tfl.Api.Presentation.Entities.Crowding,Tfl.Api.Presentation.Entities”}}]无法转换为JS ONObject                                                                   在org.json.JSON.typeMismatch(JSON.java:111)                                                                   在org.json.JSONObject。(JSONObject.java:160)                                                                   在org.json.JSONObject。(JSONObject.java:173)                                                                   在com.android.volley.toolbox.JsonObjectRequest.parseNetworkResponse(JsonObjectRequest.java:68)                                                                   在com.android.volley.NetworkDispatcher.run(NetworkDispatcher.java:123)

感谢您的光临。

1 个答案:

答案 0 :(得分:1)

开始时,在onErrorResponse方法中编写一些代码来记录收到的错误,如下所示:

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

}

然后更新并发布问题中的错误。

<强>更新

从更新的logcat这里是线索of type org.json.JSONArray cannot be converted to JSONObject。您收到的回复类型为json array,而非json object。因此,请将您的电话改为:

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);