Cyborg91 Cyborg91 - 3 months ago 16
Android Question

Sending data to server with POST through Volley not Working

just encountered a weird problem in volley.Firstly I tested below code with Apache Http Libraries,got Success,Tried with Postman Client Got success but every time in volley I got a parsing error regarding JsonString can't be converted to Json Object.
Below this is my working code:

using old Apache HTTP LIB:

httpClient=new DefaultHttpClient();
StringBuilder stringBuilder=new StringBuilder(confirm_url);
httpPost=new HttpPost(stringBuilder.toString());
new webLogin().execute();
try {
jsonObject=new JSONObject();

try {

jsonObject.put("customerID",long_customerID);//long
jsonObject.put("restaurantId",rcv_details_rcv_restaurantId);//long
jsonObject.put("subscriptionPlan",rcv_details_subscriptionPlan);
jsonObject.put("subscriptionDays",rcv_details_rcv_subscriptionDays);//int
jsonObject.put("subscriptionAmount",rcv_details_subscriptionAmount);//int

jsonObject.put("kidName",kid_name);
jsonObject.put("clas23",rcv_class);
jsonObject.put("section",rcv_section);
jsonObject.put("gender",rcv_gender);
DateTime obj=new DateTime();
DateTime dateTime=new DateTime();
jsonObject.put("startDate",dateTime);
jsonObject.put("schoolName",rcv_school);
jsonObject.put("address",rcv_delivery);

jsonObject.put("paymentType",paymentType);
jsonObject.put("restaurantSubscriptionId",rcv_details_rcv_restaurantSubscriptionId);//long
jsonObject.put("subscriptionId",0);//long


} catch (JSONException e) {
e.printStackTrace();
}
stringEntity=new StringEntity(jsonObject.toString());
httpPost.setEntity(stringEntity);
httpPost.setHeader("Content-type", "application/json");

httpResponse=httpClient.execute(httpPost);
int statusCode=httpResponse.getStatusLine().getStatusCode();


if(statusCode==200){

entity = httpResponse.getEntity();
System.out.println("Entity post is: "
+ EntityUtils.toString(entity));
mre = "200";
Log.d("SUCCESS","YES FINALLY");
Log.d("Ok",entity.toString());

}else if (statusCode==412){
Log.d("412", "412 WE MEET AGAIN)");
mre = "412";

}else
Log.i("Unknown","Unknown Server Error");
mre="unknown";

} catch (IOException e) {
e.printStackTrace();
}

return mre;
}


//working absolutely fine
This is from POSTMAN CLIENT:

enter image description here

//success

This is Volley (ERROR :String can't be converted to JSON Object)

jsonObject=new JSONObject();
try {

//I put it manually not through SHARED PREF

jsonObject.put("customerID",long_customerID);//long
jsonObject.put("restaurantId",rcv_details_rcv_restaurantId);//long
jsonObject.put("subscriptionPlan",rcv_details_subscriptionPlan);
jsonObject.put("subscriptionDays",rcv_details_rcv_subscriptionDays);//int
jsonObject.put("subscriptionAmount",rcv_details_subscriptionAmount);//int


jsonObject.put("kidName",kid_name);
jsonObject.put("clas23",rcv_class);
jsonObject.put("section",rcv_section);
jsonObject.put("gender",rcv_gender);
*//*jsonObject.put("startDate",String.valueOf(rcv_date));*//*
*//* DateTime obj=new DateTime();*//*
DateTime dateTime=new DateTime();
Log.i("ffds",dateTime.toString());

jsonObject.put("startDate",dateTime.toString());
jsonObject.put("schoolName",rcv_school);
jsonObject.put("address",rcv_delivery);

jsonObject.put("paymentType",paymentType);
jsonObject.put("restaurantSubscriptionId",rcv_details_rcv_restaurantSubscriptionId);//long
jsonObject.put("subscriptionId",subscriptionId);//long



requestBody=jsonObject.toString();
Log.i("Daa",jsonObject.toString());

JsonObjectRequest jsonObjectRequest=new JsonObjectRequest(Request.Method.POST, confirm_url, requestBody, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response_jsonObject) {
Log.i("Login Response",response_jsonObject.toString());

}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError volleyError) {

}
})


Any help would be deeply appreciated.

Answer

You're using the wrong parameters. If you read the documentation, you'll see that you don't need to toString the JsonObject.

You don't really even need the Method parameter.

Try this for a POST

new JsonObjectRequest(confirm_url, jsonObject, new Response.Listener<JSONObject>()

And this for a GET (the JsonObject is null because there is no body in a GET request)

new JsonObjectRequest(confirm_url, null, new Response.Listener<JSONObject>()
Comments