java - E/Volley:com.android.volley.ParseError:org.json.JSONException
问题描述
我正在尝试从 API 网页读取 JSON 字符串,但收到错误 org.json.JSONObject 类型的错误 jsonexception 无法转换为 JSONArray。
package com.kedaiit.dev.jadwalin;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonArrayRequest;
import com.android.volley.toolbox.Volley;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.ArrayList;
import java.util.List;
public class UpcomingFragment extends Fragment {
private RecyclerView recyclerView;
private LinearLayoutManager linearLayoutManager;
private DividerItemDecoration dividerItemDecoration;
private List<Upcoming> list;
private RecyclerView.Adapter adapter;
private String url = "https://www.thesportsdb.com/api/v1/json/1/eventsnextleague.php?id=4328";
public UpcomingFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_upcoming, container, false);
recyclerView = view.findViewById(R.id.recyclerViewUpcoming);
list = new ArrayList<>();
adapter = new UpcomingAdapter(getContext(), list);
linearLayoutManager = new LinearLayoutManager(getContext());
linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
dividerItemDecoration = new DividerItemDecoration(recyclerView.getContext(), linearLayoutManager.getOrientation());
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(linearLayoutManager);
recyclerView.addItemDecoration(dividerItemDecoration);
recyclerView.setAdapter(adapter);
getData();
return view;
}
private void getData() {
final ProgressDialog progressDialog = new ProgressDialog(getContext());
progressDialog.setMessage("Loading...");
progressDialog.show();
JsonArrayRequest jsonArrayRequest = new JsonArrayRequest(url, new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
for (int i = 0; i < response.length(); i++) {
try {
JSONObject jsonObject = response.getJSONObject(i);
Upcoming obj = new Upcoming();
obj.setIdEvent(jsonObject.getString("idEvent"));
obj.setStrEvent(jsonObject.getString("strEvent"));
obj.setDateEvent(jsonObject.getString("dateEvent"));
list.add(obj);
} catch (JSONException e) {
e.printStackTrace();
progressDialog.dismiss();
}
}
adapter.notifyDataSetChanged();
progressDialog.dismiss();
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.e("Volley", error.toString());
progressDialog.dismiss();
}
});
RequestQueue requestQueue = Volley.newRequestQueue(getContext());
requestQueue.add(jsonArrayRequest);
}
}
https://github.com/bellabeen/Android_Jadwalin这个项目我在 logcat 中得到了这个,当我调试时它说: jsonexception of type E/Volley: com.android.volley.ParseError: org.json.JSONException:
解决方案
JSONObject 和 JSONArray 的区别
Json 对象包含在 {} 中
eg: { "name" : "Ramu", "age" : "21"}
Json 数组包含在 [] 内
eg: [{ "name" : "Ramu", "age" : "21"}, { "name" : "Arnav", "age" : "12"}]
你的情况
您的对象名称是events
,因此您需要使用events
作为 id从 JSONObject 获取 JSONArray
JSONArray jsonArray = response.getJSONArray("事件");
然后遍历 json 对象数组。
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject Jobj = jsonArray.getJSONObject(i);
\\You can read each object using Jobj now
}
传递给 Volley 请求的参数错误,需要指定 GET 或 POST 方法的 url 调用。检查下面的代码。
正确的代码应该是
package com.kedaiit.dev.jadwalin;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonArrayRequest;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.ArrayList;
import java.util.List;
public class UpcomingFragment extends Fragment {
private RecyclerView recyclerView;
private LinearLayoutManager linearLayoutManager;
private DividerItemDecoration dividerItemDecoration;
private List<Upcoming> list;
private RecyclerView.Adapter adapter;
private String url = "https://www.thesportsdb.com/api/v1/json/1/eventsnextleague.php?id=4328";
public UpcomingFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_upcoming, container, false);
recyclerView = view.findViewById(R.id.recyclerViewUpcoming);
list = new ArrayList<>();
adapter = new UpcomingAdapter(getContext(), list);
linearLayoutManager = new LinearLayoutManager(getContext());
linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
dividerItemDecoration = new DividerItemDecoration(recyclerView.getContext(), linearLayoutManager.getOrientation());
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(linearLayoutManager);
recyclerView.addItemDecoration(dividerItemDecoration);
recyclerView.setAdapter(adapter);
getData();
return view;
}
private void getData() {
final ProgressDialog progressDialog = new ProgressDialog(getContext());
progressDialog.setMessage("Loading...");
progressDialog.show();
JsonObjectRequest my_request = new JsonObjectRequest(Request.Method.GET, url, null, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try{
JSONArray jsonArray = response.getJSONArray("events");
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject Jobj = jsonArray.getJSONObject(i);
Upcoming obj = new Upcoming();
obj.setIdEvent(Jobj.getString("idEvent"));
obj.setStrEvent(Jobj.getString("strEvent"));
obj.setDateEvent(Jobj.getString("dateEvent"));
list.add(obj);
}
} catch (JSONException e) {
e.printStackTrace();
progressDialog.dismiss();
}
adapter.notifyDataSetChanged();
progressDialog.dismiss();
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.e("Volley", "Error: " + error.getMessage());
progressDialog.dismiss();
}
});
RequestQueue requestQueue = Volley.newRequestQueue(getContext());
requestQueue.add(my_request);
}
}
当前代码的输出
希望这会有所帮助,
谢谢
推荐阅读
- python - How to access a dataclass docstring and comments
- java - Java 生态系统中是否有类似于 Rust 中的 cargo build 的替代方案?
- excel - 返回评估值满足 VBA 标准的范围内的最大值
- aws-lambda - 在 Azure 管道中安装 AWS lambda 工具时选择 nuget.org 作为源
- awk - 在文件中查找行并将其替换为另一个文件中的行
- sql - 每个部门的平均课程规模
- r - 根据R中的键重新编码多列
- linux - 输出 3 点和 2 点,从右到左读取
- ruby-on-rails - Ruby On Rails + PostgreSQL:如何从表 A 中获取表 B 中没有关联的所有记录?
- matlab - 在 Matlab 中禁用坐标轴比例