【发布时间】:2019-05-05 16:09:22
【问题描述】:
当我调用我的服务网站时,我遇到了这个异常:org.json.JSONArray 类型的 org.json.JSONException:Value[{}] 无法转换为 JSONObject
private void jsonParse(){
String url="http://192.168.56.1:8095/rest/workouts/all";
JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, url, null,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
JSONArray jsonArray = response.getJSONArray("");
for(int i = 0; i < jsonArray.length(); i++) {
JSONObject w = jsonArray.getJSONObject(i);
String title = w.getString("title");
String goal = w.getString("goal");
txtv.append(title + ", " + goal +"\n\n");
}
...
顺便说一下,这是JSON 格式
[{"id":"1","title":"title","goal":"goal","exercice":"Exercice
1","difficulty":"Beginner","duration":"3","image":"..","description":"..."},]
【问题讨论】:
-
在响应监听器中将 JSONObject 更改为 JSONArray
-
你必须使用
JsonArrayRequest而不是JsonObjectRequest
标签: android json android-studio android-volley android-json