【发布时间】: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