Я пытаюсь отправить Json данные через библиотеку Android Volly, но получаю сообщение об ошибке.
Вот мое тело Json, которое отлично работает с Почтальоном.
{
"fullName": "Mr X",
"fatherName": "Mr Y",
"motherName": "Mrs Z",
"nidNo": "34345",
"surveyDate": "2020-03-25",
"birthCertificateNo": "3435355",
"mobileNumber": "01834261758",
"dateOfBirth": "",
"gender": "Male",
"bloodGroup": "A+",
"numOfDaysSick": "23",
"numOfContInftPerson": "0",
"remarks": "Need Rest",
"physicalSymptoms": ",Runny nose,Sore throat",
"status": "User",
"isForeignVisitor": "false",
"isRead": "false",
"presentAddress": {
"village": "Sonapur",
"postOffice": "Noahat",
"postCode": "1219",
"upazila": "Sonaimuri",
"district": "Noakhali",
"division": "Chittagong"
},
"permanentAddress": {
"village": "Sonapur",
"postOffice": "Noahat",
"postCode": "1234",
"upazila": "Sonaimuri",
"district": "Noakhali",
"division": "Chittagong"
}}
Но когда я пытаюсь отправить его через Android Звонок Volly's Post, я получаю сообщение об ошибке ниже:
com.android.volley.ParseError: org.json.JSONException: Value Survey of type java.lang.String cannot be converted to JSONObject
Вот мой Android метод для создания этого Json тела.
Обратите внимание: я могу успешно сгенерировать тело Json с помощью этого метода Android и скопировать вставить (из Logcat) в Postman, все работает нормально. Но Android Volly дает мне такую ошибку. Мой URL-адрес также в порядке, потому что я проверил его вручную.
public void SendSurveyDataToServer(){
customProgressBar.show();
//--
//-------------------------------
JSONObject permanent_address = new JSONObject();
try {
permanent_address.put("village", village_pS);
permanent_address.put("postOffice", post_office_pS);
permanent_address.put("postCode", post_code_pS);
permanent_address.put("upazila", upazilla_pS);
permanent_address.put("district", district_pS);
permanent_address.put("division", division_pS);
} catch (JSONException e) {
e.printStackTrace();
}
//--
JSONObject present_address = new JSONObject();
try {
present_address.put("village", village_tS);
present_address.put("postOffice", post_office_tS);
present_address.put("postCode", post_code_tS);
present_address.put("upazila", upazilla_tS);
present_address.put("district", district_tS);
present_address.put("division", division_tS);
} catch (JSONException e) {
e.printStackTrace();
}
//--
if(numberofContactedPersonS.equals("Yes")){
numberofContactedPersonS = "true";
}else{
numberofContactedPersonS = "false";
}
//--
//--
JSONObject parameters = new JSONObject();
try {
parameters.put("fullName", fullNameS);
parameters.put("fatherName", fatherNameS);
parameters.put("motherName", motherNameS);
parameters.put("nidNo", nidNoS);
parameters.put("surveyDate", "2020-03-25");
parameters.put("birthCertificateNo", birthdayCertificateNoS);
parameters.put("mobileNumber", mobileNoS);
parameters.put("dateOfBirth", "");
parameters.put("gender", gender_selectS);
parameters.put("bloodGroup", blood_group_selectS);
parameters.put("numOfDaysSick", sickDaysNoS);
parameters.put("numOfContInftPerson", "0");
parameters.put("remarks", remarksS);
parameters.put("physicalSymptoms", physicalSymptomsS);
parameters.put("status", statusS);
parameters.put("isForeignVisitor", numberofContactedPersonS );
parameters.put("isRead", "false");
parameters.put("presentAddress", present_address);
parameters.put("permanentAddress", permanent_address);
} catch (JSONException e) {
e.printStackTrace();
}
Log.d(classTag,parameters.toString());
RequestQueue rq = Volley.newRequestQueue(this);
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest
(Request.Method.POST, ApiClass.server_path+ApiClass.saveSurvey, parameters, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
String respo=response.toString();
Log.d(classTag,respo);
//iosDialog.cancel();
//Parse_signup_data(respo);
Log.e(classTag,respo);
if(respo.equalsIgnoreCase("Survey Info save Successfully")){
Toast.makeText(Survey.this,"Your survey submitted successfully. Thank you!",Toast.LENGTH_LONG).show();
//----------------------------------------
}else if(respo.equalsIgnoreCase("Survey Info already exits")){
Toast.makeText(Survey.this,"Survey info already exist with this NID and Birth Certificate Number!",Toast.LENGTH_LONG).show();
}else{
Toast.makeText(Survey.this,"There is an error while submitting your survey, please try again!",Toast.LENGTH_LONG).show();
Log.e(classTag,"There is an error while submitting your survey, please try again!");
}
customProgressBar.hide();
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
// TODO: Handle error
// Toast.makeText(MainActivity.this,"Can't connect with server! Please check your network connection.",Toast.LENGTH_LONG).show();
customProgressBar.hide();
Log.d(classTag,error.toString());
}
});
jsonObjectRequest.setRetryPolicy(new DefaultRetryPolicy(30000,
DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
rq.getCache().clear();
rq.add(jsonObjectRequest);
//--------------
}