I'm trying to read a JSON string from a API webpage but get the error jsonexception of type org.json.JSONObject cannot be converted to 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 This project me get this in the logcat and when i debug it says: jsonexception of type E/Volley: com.android.volley.ParseError: org.json.JSONException:
Json Object are enclosed within {}
eg: { "name": "Ramu", "age": "21"}
Json Array are enclosed within [] eg: [{ "name": "Ramu", "age": "21"}, { "name": "Arnav", "age": "12"}]
Your Object name is events
so you need to get the JSONArray from JSONObject using events
as id
JSONArray jsonArray = response.getJSONArray("events");
And then iterate over the array of json Objects.
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject Jobj = jsonArray.getJSONObject(i);
\\You can read each object using Jobj now
}
The paramerters passed to Volley request is wrong, you need to specify GET or POST Method of url calls. Check the below code.
The correct code should be
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);
}
}
Hope this helps,
Thank You
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.