2016-08-06 118 views
1

在Volley中遇到了一個奇怪的問題。
首先我使用Apache Http庫測試了下面的代碼,並且獲得了成功,嘗試了一個Postman客戶端並取得了成功,但是每次在Volley中我都遇到了有關JsonString can't be converted to Json Object的解析錯誤。通過Volley發送數據到服務器通過Volley不起作用

這是我的工作代碼:
使用舊的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; 
    } 

//工作精絕 這是郵差客戶:

enter image description here

//成功

This is Volley(ERROR:String can not be converted to JSON Obj ect)

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) { 

      } 
     }) 

任何幫助將不勝感激。

回答

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>() 
+0

你在暗示不要使用請求主體先生? – notTdar

+0

JsonObject是請求主體。它不需要是一個字符串 –

+0

新的JsonObjectRequest(Request.Method.POST,confirm_url,jsonObject,新的Response.Listener ()這是正確的! – notTdar

相關問題