我在向服务器发送排球请求时遇到一些错误......任何人都可以帮助我
I have some errors in sending volley request to the server ...Anyone can help me
我想将对象列表转换成json数组,然后将此数组传递到服务器上进行提交
我尝试了很多次但失败了
我认为主要问题在于我可以将对象列表转换为 json 数组的部分。
请查看代码和错误并指导我
谢谢
尝试{
final ProgressDialog progress = new ProgressDialog(getContext());
progress.setTitle("Submitting Leave");
progress.setMessage("Loading...");
progress.setCancelable(false); // disable dismiss by tapping outside of the dialog
progress.show();
// jsonObject.put("dayList",leaveDayListArrayList);
// JSONArray jsonArray55 = new JSONArray(utils.leaveStaEndDTOList);
JSONArray jsonArray33 = new JSONArray();
for (int j=0; j<=utils.leaveStaEndDTOList.size() ; j++ ){
JSONObject jsonObject1 = new JSONObject();
jsonObject1.put("afternoonValue",utils.leaveStaEndDTOList.get(j).getAfternoonValue());
jsonObject1.put("date",utils.leaveStaEndDTOList.get(j).getDate());
jsonObject1.put("day",utils.leaveStaEndDTOList.get(j).getDay());
jsonObject1.put("dayCode",utils.leaveStaEndDTOList.get(j).getDayCode());
jsonObject1.put("holyday",utils.leaveStaEndDTOList.get(j).getHolyday());
jsonObject1.put("month",utils.leaveStaEndDTOList.get(j).getMonth());
jsonObject1.put("morningValue",utils.leaveStaEndDTOList.get(j).getMorningValue());
jsonObject1.put("weekend",utils.leaveStaEndDTOList.get(j).getWeekend());
jsonObject1.put("year",utils.leaveStaEndDTOList.get(j).getYear());
jsonArray33.put(jsonObject1);
}
jsonObject.put("dayList",jsonArray33);
jsonObject.put("endDate",utils.creLeaveEndDate);
jsonObject.put("fileData","data:image/jpeg;base64,"+utils.creLeaveFile);
jsonObject.put("resourceId",utils.creLeaveResourceId);
jsonObject.put("startDate",utils.creLeaveStartDate);
jsonObject.put("statusId",utils.creLeaveStatusId);
jsonObject.put("title",utils.creLeaveTitle);
jsonObject.put("total",utils.creLeaveTotal);
jsonObject.put("typeId",utils.creLeaveTypeId);
jsonObject.put("year",utils.creLeaveYear);
// jsonObject.put("leaveId",utils.creLeaveId);
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(
Request.Method.POST, "url", jsonObject, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
Toast.makeText(getContext(), "Successfully added expense", Toast.LENGTH_SHORT).show();
progress.dismiss();
final Fragment expenseFragmentList=new ExpenseFragmentList();
getFragmentManager().beginTransaction().replace(R.id.fragmentContainerEx,
expenseFragmentList,expenseFragmentList.getClass().getSimpleName())
.addToBackStack(null).commit();
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
progress.dismiss();
Toast.makeText(getContext(), error.toString(), Toast.LENGTH_SHORT).show();
}
}
){
@Override
public Map<String, String> getHeaders() {
Map<String, String> params = new HashMap<String, String>();
params.put("Accept", "application/json");
params.put("Authorization",utils.bear + user_token);
return params;
}
};
queue.add(jsonObjectRequest);
}catch (JSONException e){
Toast.makeText(getContext(), e.toString(), Toast.LENGTH_SHORT).show();
}
}
这是我的代码错误,在我将对象列表传递到 json数组
时显示
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.sprintsols.Pajita, PID: 22480
java.lang.IndexOutOfBoundsException: Invalid index 4, size is 4
at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:255)
at java.util.ArrayList.get(ArrayList.java:308)
at com.sprintsols.Pajita.Fragments.LeavesFragment.onClick(LeavesFragment.java:304)
at android.view.View.performClick(View.java:5184)
at android.view.View$PerformClick.run(View.java:20910)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:5942)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1400)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1195)
Process 22480 terminated.
这是错误
{
"dayList": [
{
"afternoonValue": 0,
"date": "yyyy-MM-dd",
"day": 0,
"dayCode": "string",
"holyday": true,
"id": 0,
"month": 0,
"morningValue": 0,
"weekend": true,
"year": 0
}
],
"endDate": "yyyy-MM-dd",
"fileData": "string",
"leaveId": 0,
"resourceId": 0,
"startDate": "yyyy-MM-dd",
"statusId": 0,
"title": "string",
"total": 0,
"typeId": 0,
"year": 0
}
这是我要发送到服务器的json正文
每当遇到错误时,请先仔细阅读错误日志。大多数情况下,该问题已在此处明确提及。
java.lang.IndexOutOfBoundsException: Invalid index 4, size is 4
例如,这里的错误清楚地表明您正在越界,即试图访问数组中不存在的索引。
现在,在查看所有数组迭代之后,我们可以看到您在从 0 迭代到长度时向 JSONArray "jsonArray33" 添加元素。但是,我们知道,列表的索引从 0 开始,直到长度为 1(不是长度)。
所以,你必须更正这个:
for (int j = 0; j < utils.leaveStaEndDTOList.size(); j++)
{
// code...
}
希望对您有所帮助。
for (int j=0; j<=utils.leaveStaEndDTOList.size() ; j++ ) 这一行应该是 for (int j=0; j
我想将对象列表转换成json数组,然后将此数组传递到服务器上进行提交 我尝试了很多次但失败了 我认为主要问题在于我可以将对象列表转换为 json 数组的部分。 请查看代码和错误并指导我 谢谢
尝试{
final ProgressDialog progress = new ProgressDialog(getContext());
progress.setTitle("Submitting Leave");
progress.setMessage("Loading...");
progress.setCancelable(false); // disable dismiss by tapping outside of the dialog
progress.show();
// jsonObject.put("dayList",leaveDayListArrayList);
// JSONArray jsonArray55 = new JSONArray(utils.leaveStaEndDTOList);
JSONArray jsonArray33 = new JSONArray();
for (int j=0; j<=utils.leaveStaEndDTOList.size() ; j++ ){
JSONObject jsonObject1 = new JSONObject();
jsonObject1.put("afternoonValue",utils.leaveStaEndDTOList.get(j).getAfternoonValue());
jsonObject1.put("date",utils.leaveStaEndDTOList.get(j).getDate());
jsonObject1.put("day",utils.leaveStaEndDTOList.get(j).getDay());
jsonObject1.put("dayCode",utils.leaveStaEndDTOList.get(j).getDayCode());
jsonObject1.put("holyday",utils.leaveStaEndDTOList.get(j).getHolyday());
jsonObject1.put("month",utils.leaveStaEndDTOList.get(j).getMonth());
jsonObject1.put("morningValue",utils.leaveStaEndDTOList.get(j).getMorningValue());
jsonObject1.put("weekend",utils.leaveStaEndDTOList.get(j).getWeekend());
jsonObject1.put("year",utils.leaveStaEndDTOList.get(j).getYear());
jsonArray33.put(jsonObject1);
}
jsonObject.put("dayList",jsonArray33);
jsonObject.put("endDate",utils.creLeaveEndDate);
jsonObject.put("fileData","data:image/jpeg;base64,"+utils.creLeaveFile);
jsonObject.put("resourceId",utils.creLeaveResourceId);
jsonObject.put("startDate",utils.creLeaveStartDate);
jsonObject.put("statusId",utils.creLeaveStatusId);
jsonObject.put("title",utils.creLeaveTitle);
jsonObject.put("total",utils.creLeaveTotal);
jsonObject.put("typeId",utils.creLeaveTypeId);
jsonObject.put("year",utils.creLeaveYear);
// jsonObject.put("leaveId",utils.creLeaveId);
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(
Request.Method.POST, "url", jsonObject, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
Toast.makeText(getContext(), "Successfully added expense", Toast.LENGTH_SHORT).show();
progress.dismiss();
final Fragment expenseFragmentList=new ExpenseFragmentList();
getFragmentManager().beginTransaction().replace(R.id.fragmentContainerEx,
expenseFragmentList,expenseFragmentList.getClass().getSimpleName())
.addToBackStack(null).commit();
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
progress.dismiss();
Toast.makeText(getContext(), error.toString(), Toast.LENGTH_SHORT).show();
}
}
){
@Override
public Map<String, String> getHeaders() {
Map<String, String> params = new HashMap<String, String>();
params.put("Accept", "application/json");
params.put("Authorization",utils.bear + user_token);
return params;
}
};
queue.add(jsonObjectRequest);
}catch (JSONException e){
Toast.makeText(getContext(), e.toString(), Toast.LENGTH_SHORT).show();
}
}
这是我的代码错误,在我将对象列表传递到 json数组
时显示E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.sprintsols.Pajita, PID: 22480
java.lang.IndexOutOfBoundsException: Invalid index 4, size is 4
at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:255)
at java.util.ArrayList.get(ArrayList.java:308)
at com.sprintsols.Pajita.Fragments.LeavesFragment.onClick(LeavesFragment.java:304)
at android.view.View.performClick(View.java:5184)
at android.view.View$PerformClick.run(View.java:20910)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:5942)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1400)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1195)
Process 22480 terminated.
这是错误
{
"dayList": [
{
"afternoonValue": 0,
"date": "yyyy-MM-dd",
"day": 0,
"dayCode": "string",
"holyday": true,
"id": 0,
"month": 0,
"morningValue": 0,
"weekend": true,
"year": 0
}
],
"endDate": "yyyy-MM-dd",
"fileData": "string",
"leaveId": 0,
"resourceId": 0,
"startDate": "yyyy-MM-dd",
"statusId": 0,
"title": "string",
"total": 0,
"typeId": 0,
"year": 0
}
这是我要发送到服务器的json正文
每当遇到错误时,请先仔细阅读错误日志。大多数情况下,该问题已在此处明确提及。
java.lang.IndexOutOfBoundsException: Invalid index 4, size is 4
例如,这里的错误清楚地表明您正在越界,即试图访问数组中不存在的索引。
现在,在查看所有数组迭代之后,我们可以看到您在从 0 迭代到长度时向 JSONArray "jsonArray33" 添加元素。但是,我们知道,列表的索引从 0 开始,直到长度为 1(不是长度)。
所以,你必须更正这个:
for (int j = 0; j < utils.leaveStaEndDTOList.size(); j++)
{
// code...
}
希望对您有所帮助。
for (int j=0; j<=utils.leaveStaEndDTOList.size() ; j++ ) 这一行应该是 for (int j=0; j