【发布时间】:2016-08-07 01:52:41
【问题描述】:
刚刚在 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;
}
//工作正常 这是来自邮递员客户:
//成功
这是 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) {
}
})
任何帮助将不胜感激。
【问题讨论】:
标签: android json android-volley