【问题标题】:Why isn't Volley isn't returning any information from an API?为什么 Volley 没有从 API 返回任何信息?
【发布时间】:2017-10-31 18:05:34
【问题描述】:

我无法使用 Volley 从 TFL(伦敦交通)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", “名称”:“北方”,“模式名称”:“管”,“中断”:[],“创建”:“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":"良好服务","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"}] org.json.JSONArray 类型的 "crowding":{"$type":"Tfl.Api.Presentation.Entities.Crowding, Tfl.Api.Presentation.Entities"}}] 不能是 con转为 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":" Northern","modeName":"tube","disruptions":[],"created":"2017-10-31T10:48:22.99Z","modified":"2017-10-31T10:48:22.99Z ","lineStatuses":[{"$type":"Tfl.Api.Presentation.Entities.LineStatus, Tfl.Api.Presentation.Entities","id":0,"statusSeverity":10,"statusSeverityDescription":"良好的服务","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": org.json.JSONArray 类型的 {"$type":"Tfl.Api.Presentation.Entities.Crowding, Tfl.Api.Presentation.Entities"}}] 无法转换为 JSONObject 在 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)

感谢收看。

【问题讨论】:

  • 也许实现onErrorResponse???并看到错误???另外我希望你使用 Android Studio 作为开发工具
  • 感谢您的建议。按照建议完成。我已经编辑了问题以包含当前的 logcat 输出。干杯。

标签: android json android-volley


【解决方案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);

【讨论】:

  • 感谢您的建议。按照建议完成。我已经编辑了问题以包含当前的 logcat 输出。干杯。
猜你喜欢
  • 2023-01-25
  • 2014-10-30
  • 2019-07-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-12-17
  • 2016-06-19
  • 1970-01-01
相关资源
最近更新 更多