刚刚在Volley遇到了一个奇怪的问题
首先,我使用Apache Http库测试了以下代码,并获得了成功,尝试使用Postman客户端并获得成功,但每次在Volley中我都遇到有关JsonString can't be converted to Json Object
的解析错误。
这是我的工作代码:
使用旧的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;
}
//工作得很好 这是来自POSTMAN CLIENT:
//成功
这是Volley(错误:字符串无法转换为JSON对象)
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) {
}
})
非常感谢任何帮助。
答案 0 :(得分:2)
您使用了错误的参数。如果您阅读文档,则会发现您不需要toString
JsonObject。
您甚至不需要Method参数。
尝试使用POST
new JsonObjectRequest(confirm_url, jsonObject, new Response.Listener<JSONObject>()
这对于GET(JsonObject为null,因为GET请求中没有正文)
new JsonObjectRequest(confirm_url, null, new Response.Listener<JSONObject>()