2016-04-11 64 views
1

我想通過retrofit2傳遞一個自定義對象,而我的問題是服務器是否自動將自定義對象寫入json或者是否必須爲此編寫一個php文件。與此同時,我成功發佈到服務器,但無法寫入json。在改造中傳遞自定義對象2

我的目標是將自定義對象寫入服務器,並將自定義對象的內容寫入json文件。

這是我改造阿比

public interface ApsaraCatalogAPI { 

    @GET("/apsaratrendz/data/apsara_json_document_v2.json") 
    Call<List<ApsaraCatalogModel>> getFeed(); 

    @POST("/apsaratrendz/data/apsara_json_orders_document.json") 
    Call<Void> setOrder(@Body OrderModel orderModel); 
} 

這裏是我的調用API函數

@Override 
    public void onClick(View v) { 
     int total = 0; 
     if(v.getId()==R.id.fabButtonCart && cartlist.size()!=0) 
     { 
      // get details from shared preferences 
      OrderModel orderModel = new OrderModel(); 
      orderModel.setDate(getDate()); 
      orderModel.setName("ssdfs"); 
      orderModel.setEmail("[email protected]"); 
      orderModel.setNoofitems(String.valueOf(cartlist.size())); 
      orderModel.setOrderno("32335"); 
      orderModel.setPhone("9896566444"); 
      for(int i=0; i<cartlist.size();i++){ 
       Productdetail pd = new Productdetail(); 
       pd.getSellingprice(String.valueOf(cartlist.get(i).getSellingPrice())); 
       pd.getPid(cartlist.get(i).getEANCode()); 
       total += cartlist.get(i).getSellingPrice(); 
       orderModel.getProductdetails().add(pd); 
      } 
      // 
      // now go for insertion using retrofit 
      requestData(orderModel); 
      Toast.makeText(getApplicationContext(), "Total Price : Rs."+total+"/-", Toast.LENGTH_LONG).show(); 
     }else{ 
      Toast.makeText(getApplicationContext(), "Cart is Empty", Toast.LENGTH_LONG).show(); 
     } 

    } 

這裏是改造的API,我傳遞新創建的POJO類OrderModel我的服務請求。

import java.util.Map; 
     import java.util.ArrayList; 
     import java.util.HashMap; 
     import java.util.List; 
public class OrderModel { 

    private String name; 
    private String email; 
    private String phone; 
    private String orderno; 
    private String date; 
    private String noofitems; 
    private List<Productdetail> productdetails = new ArrayList<Productdetail>(); 
    private Map<String, Object> additionalProperties = new HashMap<String, Object>(); 

    /** 
    * 
    * @return 
    * The name 
    */ 
    public String getName() { 
     return name; 
    } 

    /** 
    * 
    * @param name 
    * The name 
    */ 
    public void setName(String name) { 
     this.name = name; 
    } 

    /** 
    * 
    * @return 
    * The email 
    */ 
    public String getEmail() { 
     return email; 
    } 

    /** 
    * 
    * @param email 
    * The email 
    */ 
    public void setEmail(String email) { 
     this.email = email; 
    } 

    /** 
    * 
    * @return 
    * The phone 
    */ 
    public String getPhone() { 
     return phone; 
    } 

    /** 
    * 
    * @param phone 
    * The phone 
    */ 
    public void setPhone(String phone) { 
     this.phone = phone; 
    } 

    /** 
    * 
    * @return 
    * The orderno 
    */ 
    public String getOrderno() { 
     return orderno; 
    } 

    /** 
    * 
    * @param orderno 
    * The orderno 
    */ 
    public void setOrderno(String orderno) { 
     this.orderno = orderno; 
    } 

    /** 
    * 
    * @return 
    * The date 
    */ 
    public String getDate() { 
     return date; 
    } 

    /** 
    * 
    * @param date 
    * The date 
    */ 
    public void setDate(String date) { 
     this.date = date; 
    } 

    /** 
    * 
    * @return 
    * The noofitems 
    */ 
    public String getNoofitems() { 
     return noofitems; 
    } 

    /** 
    * 
    * @param noofitems 
    * The noofitems 
    */ 
    public void setNoofitems(String noofitems) { 
     this.noofitems = noofitems; 
    } 

    /** 
    * 
    * @return 
    * The productdetails 
    */ 
    public List<Productdetail> getProductdetails() { 
     return productdetails; 
    } 

    /** 
    * 
    * @param productdetails 
    * The productdetails 
    */ 
    public void setProductdetails(List<Productdetail> productdetails) { 
     this.productdetails = productdetails; 
    } 

    public Map<String, Object> getAdditionalProperties() { 
     return this.additionalProperties; 
    } 

    public void setAdditionalProperty(String name, Object value) { 
     this.additionalProperties.put(name, value); 
    } 

} 

產品詳細類:

private void requestData(OrderModel orderModel) { 

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

     ApsaraCatalogAPI service = retrofit.create(ApsaraCatalogAPI.class); 
     Call<Void> call = service.setOrder(orderModel); 

     call.enqueue(new Callback<Void>() { 
     @Override 
     public void onResponse(Call<Void> call, Response<Void> response) { 
      if(response.isSuccessful()){ 
       Log.d("InApi","Yipppie"); 
      } 
     } 

     @Override 
     public void onFailure(Call<Void> call, Throwable t) { 
      Log.d("InApi","Kaboom"); 
     } 
    }); 

    } 

我的POJO類下面給出

import java.util.HashMap; 
import java.util.Map; 

public class Productdetail { 

    private String pid; 
    private String sellingprice; 
    private Map<String, Object> additionalProperties = new HashMap<String, Object>(); 

    /** 
    * 
    * @return 
    * The pid 
    * @param s 
    */ 
    public String getPid(String s) { 
     return pid; 
    } 

    /** 
    * 
    * @param pid 
    * The pid 
    */ 
    public void setPid(String pid) { 
     this.pid = pid; 
    } 

    /** 
    * 
    * @return 
    * The sellingprice 
    * @param s 
    */ 
    public String getSellingprice(String s) { 
     return sellingprice; 
    } 

    /** 
    * 
    * @param sellingprice 
    * The sellingprice 
    */ 
    public void setSellingprice(String sellingprice) { 
     this.sellingprice = sellingprice; 
    } 

    public Map<String, Object> getAdditionalProperties() { 
     return this.additionalProperties; 
    } 

    public void setAdditionalProperty(String name, Object value) { 
     this.additionalProperties.put(name, value); 
    } 

} 
+0

什麼是服務器的JSON響應?根據錯誤消息,您的API會返回一個對象列表,但您指定了一個對象。 – peitek

+0

我不希望我的API返回任何東西(更新的問題),只是想寫我的POJO到JSON,所以我需要PHP文件來做到這一點,或者服務器是否處理這個?我對改造和PHP非常陌生.. –

+0

從改造方面看,一切看起來都不錯。 – peitek

回答

0

改造基於POJO的json的,放在後載荷。 所以,在這種情況下:

@POST("/apsaratrendz/data/apsara_json_orders_document.json") 
Call<Void> setOrder(@Body OrderModel orderModel); 

後的身體會orderModel的JSON表示。

如果需要更改JSON,您可以使用SerializedName註釋,它是非常有用的產生atributtes的名字:

public class OrderModel { 
    @SerializedName("other_name_for_json") 
    private String name; 

希望它能幫助。

相關問題