2015-09-25 28 views
3

我想發佈服務器上的JSON數組下。如何發送身體使用JSON數據改造android

{ 
    "order": [{ 
      "orderid": "39", 
      "dishid": "54", 
      "quantity": "4", 
      "userid":"2" 
     },{ 
      "orderid": "39", 
      "dishid": "54", 
      "quantity": "4", 
      "userid":"2" 
     }] 

} 

我使用這下面:

private void updateOreder() { 

    M.showLoadingDialog(GetDishies.this); 
    UpdateAPI mCommentsAPI = APIService.createService(UpdateAPI.class); 

    mCommentsAPI.updateorder(jsonObject, new Callback<String>() { 
     @Override 
     public void success(String s, Response response) { 
      M.hideLoadingDialog(); 
      Log.e("ssss",s.toString()); 
      Log.e("ssss", response.getReason()); 
     } 

     @Override 
     public void failure(RetrofitError error) { 
      M.hideLoadingDialog(); 
      Log.e("error",error.toString()); 
     } 

    }); 

} 

我得到以下錯誤:

retrofit.RetrofitError: com.google.gson.JsonSyntaxException: com.google.gson.stream.MalformedJsonException: Use JsonReader.setLenient(true) to accept malformed JSON at line 2 column 6 path $ 

updateApi代碼:

@POST("updateorder.php") 
    void updateorder(@Body JSONObject object,Callback<String>()); 

任何一個可以請你告訴我,我的錯誤?

在此先感謝

+0

嘗試激活軌跡以查看結果。機會是,問題在於服務器的答案。 – Henry

+0

@Henry謝謝你的回答。這裏沒有什麼可追溯的。它不會將數據發送到服務器。 –

+0

請發佈您的UpdateAPI代碼。 –

回答

0

使用改裝版2

compile 'com.squareup.retrofit2:retrofit:2.0.0' 
compile 'com.squareup.retrofit:converter-gson:2.0.0-beta2' 
  1. 創建對應於JSON結構POJO類,你將發送到服務器:

    public class RequestBody { 
         private List<InnerClass> order; 
    
         public RequestBody(List<InnerClass> order) { 
         this.order = order; 
         } 
    
         public class InnerClass{ 
          private int orderid, dishid, quantity, userid; 
    
          public InnerClass(int orderid, int dishid, int quantity, int userid) { 
          this.orderid = orderid; 
          this.dishid = dishid; 
          this.quantity = quantity; 
          this.userid = userid; 
         } 
         public int getOrderId() { return orderid; } 
         public int getDishId() { return dishid; } 
         public int getQuantity() { return quantity; } 
         public int getUserId() { return userid; } 
         } 
    } 
    
  2. 將servicegenerator類創建爲in itialize您Retrofic對象實例:

    public class ServiceGenerator { 
    
        private static OkHttpClient okHttpClient = new OkHttpClient(); 
    
        public static <S> S createService(Class<S> serviceClass) { 
        Retrofit.Builder builder = new Retrofit.Builder() 
              .baseUrl(AppConfig.BASE_URL) 
              .addConverterFactory(GsonConverterFactory.create()); 
        Retrofit retrofit = builder.client(okHttpClient).build(); 
    
        return retrofit.create(serviceClass); 
    } 
    
  • 創建它應該包含 「updateorder」 方法的API服務接口:

    public interface ApiService { 
        @POST("updateorder.php") 
        Call<your POJO class that corresponds to your server response data> updateorder(@Body RequestBody object); 
    } 
    
  • 4.inside您的活動或片段,你會喜歡使請求填充您的Json數據並初始化ApiService:

    ArrayList<RequestBody.InnerClass> list = new List<>(); 
    list.add(new RequestBody.InnerClass(39, 54, 4, 2)); 
    list.add(new RequestBody.InnerClass(39, 54, 4, 2)); 
    RequestBody requestBody = new RequestBody(list); 
    
        ApiService apiService = ServiceGenerator.createService(ApiService.class); 
        Call<your POJO class that corresponds to your server response data> call = apiService.updateorder(requestBody); 
    //use enqueue for asynchronous requests 
    call.enqueue(new Callback<your POJO class that corresponds to your server response data>() { 
    
        public void onResponse(Response<> response, Retrofit retrofit) { 
         M.hideLoadingDialog(); 
         Log.e("ssss",s.toString()); 
         Log.e("ssss", response.getReason()); 
        } 
    
        public void onFailure(Throwable t) { 
          M.hideLoadingDialog(); 
          Log.e("error",t.toString()); 
         } 
    } 
    
    3

    嘗試修改代碼以改造2

    compile 'com.squareup.retrofit2:retrofit:2.0.0' 
    compile 'com.squareup.retrofit2:converter-gson:2.0.0' 
    

    您的服務:

    @POST("updateorder.php") 
    Call<String> updateorder(@Body JsonObject object); 
    

    呼叫改造

    Retrofit retrofit = new Retrofit.Builder() 
         .baseUrl(RetrofitService.baseUrl) 
         .addConverterFactory(GsonConverterFactory.create()) 
         .build(); 
    

    使用傳遞您的JSON GSON:

    JsonObject postParam = new JsonObject 
    postParam.addProperty("order",yourArray) ; 
    

    最後:

    Call<String> call = retrofitService.updateorder(postParam); 
    
    
        call.enqueue(new Callback<String>() { 
         @Override 
         public void onResponse(Call<String>callback,Response<String>response) { 
          String res = response.body(); 
         } 
          @Override 
          public void onFailure(Call<String> call, Throwable t) { 
    
          } 
        }); 
    

    我希望能對你有所幫助。

    3

    創建OrderRequest類

    public class OrderRequest { 
    
    @SerializedName("order") 
    public List<Order> orders; 
    } 
    

    創建Order類

    public class Order { 
    
        @SerializedName("orderid") 
        public String Id; 
    } 
    

    端點

    public interface ApiEndpoint{ 
        @POST("order") 
        Call<String> createOrder(@Body OrderRequest order, @HeaderMap HashMap<String,String> headerMap); 
    } 
    

    的用途,其中調用服務

    在MainActivity這種類型的實現

    在createOrder方法中,我們不需要將對象轉換爲Json。因爲當我們進行改造時,我們將轉換器工廠添加爲GsonConverterFactory。它會自動將該對象轉換爲JSON

    retrofit = new Retrofit.Builder() 
           .baseUrl(BASE_URL) 
           .addConverterFactory(GsonConverterFactory.create()) 
           .build();