我正在尝试从API提取数据并将其保存在本地数据库中,但问题是我的代码有时运行正常,有时却无法正常工作。它引发迭代器异常。我正在尝试调试它,但找不到任何令人满意的结果。请帮忙。我不明白为什么我的代码有时运行,有时却不运行。
private void callEventsApi() {
String tag_json_obj = "json_obj_req";
String url = Constants.GET_EVENTS;
final SessionManager session = new SessionManager(getActivity());
final HashMap<String, String> loggedDetail = session.getUserDetail();
pBar.setVisibility(View.VISIBLE);
JsonObjectRequest jsonObjReq = new JsonObjectRequest(Request.Method.POST,
url, new JSONObject(),
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
Log.e("event_response", response.toString());
Gson gson = new Gson();
try {
databaseHelper.deleteData();
ModelEvent modelEvent;
modelEvent = gson.fromJson(response.toString(), ModelEvent.class);
if (modelEvent.getStatus() == 200) {
String status = response.getString("status");
JSONArray jsonArray = response.getJSONArray("data");
Log.e("tag", "instatus: " + jsonArray);
List<EventData> datumArrayList = new ArrayList<>();
datumArrayList = Arrays.asList(gson.fromJson(jsonArray.toString(), EventData[].class));
Log.e("TAG", "onResponse:size " + datumArrayList.size());
for (EventData eventData : datumArrayList) {
databaseHelper.addEventsDetails(eventData);
Log.e("tag", "onResponse56: " + datumArrayList.size());
}
for (int i = 0; i < datumArrayList.size(); i++) {
for (EventProduct eventProduct : datumArrayList.get(i).getProducts()) {
Integer event_pro_id = datumArrayList.get(i).getEventId();
Integer product_id = eventProduct.getId();
String product_name = eventProduct.getProductName();
EventProduct eventProduct1 = new EventProduct();
eventProduct1.setEventId(event_pro_id);
eventProduct1.setId(product_id);
eventProduct1.setProductName(product_name);
databaseHelper.addEventProducts(eventProduct1);
}
}
for (int i = 0; i < datumArrayList.size(); i++) {
for (Subevent subevent : datumArrayList.get(i).getSubevent()) {
Log.e("tag", "onResponse13: " + subevent.getDate() + "&&" + subevent.getId() + "**" +
datumArrayList.get(i).getEventId() + "))" + datumArrayList.get(i).getEventName());
Integer event_sub_id = datumArrayList.get(i).getEventId();
Integer sub_event_id = subevent.getId();
String sub_event_name = subevent.getName();
String sub_event_date = subevent.getDate();
Subevent subevent1 = new Subevent();
subevent1.setEventId(event_sub_id);
subevent1.setId(sub_event_id);
subevent1.setName(sub_event_name);
subevent1.setDate(sub_event_date);
databaseHelper.addSubEvents(subevent1);
}
}
for (int i = 0; i < datumArrayList.size(); i++) {
for (Subevent subevent : datumArrayList.get(i).getSubevent()) {
for (EventBar eventBar : subevent.getBars()) {
Log.e("tag", "onResponse14: " + eventBar.getName() + "&&" + eventBar.getId() + "**" +
subevent.getId() + "))");
Integer subEvent_bar_id = subevent.getId();
Integer bar_id = eventBar.getId();
String bar_name = eventBar.getName();
EventBar eventBar1 = new EventBar();
eventBar1.setSubEventId(subEvent_bar_id);
eventBar1.setId(bar_id);
eventBar1.setName(bar_name);
databaseHelper.addSeBars(eventBar1);
}
}
}
netStockApi();
Intent in = new Intent(getActivity(), ContainerActivity.class);
startActivity(in);
} else {
Toast.makeText(getActivity(), R.string.some_error_occured, Toast.LENGTH_SHORT).show();
}
} catch (Exception e) {
e.printStackTrace();
}
pBar.setVisibility(View.GONE);
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
VolleyLog.e("Error: " + error.getMessage());
// handle error
pBar.setVisibility(View.GONE);
}
}) {
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
HashMap<String, String> headers = new HashMap<String, String>();
headers.put("Content-Type", "application/json; charset=UTF-8");
headers.put("Authorization", "Bearer " + loggedDetail.get("api_token"));
return headers;
}
};
jsonObjReq.setRetryPolicy(new DefaultRetryPolicy(
100000,
DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
AppController.getInstance().addToRequestQueue(jsonObjReq, tag_json_obj);
}