2015-06-16 47 views
2

改裝使得像我這樣的簡易操作變得容易。但是,我爲當前項目請求的API響應結構並不遵循之前使用的相同格式。我不確定是否需要重寫我的POJO或在GSON中製作自定義的反序列化器。我無法更改JSON結構,並且自定義的反序列化器對我來說似乎令人望而生畏。定製解串器或不同的類設計改裝

這裏是JSON:

 { 
    "Green Shirt": [ 
     { 
      "id": "740", 
      "name": 「Nice Green Shirt", 
      "quantity": "0", 
      "make": "", 
      "model": "", 
      "price": 「15.00", 
      "size": "XXS", 
      "sku": null, 
      "image": "https:\/\/google.com\/green_shirt.jpg", 
      "new_record": false, 
      "category_name": "", 
      "bar_code": "", 
     }, 
     { 
      "id": "743", 
      "name": "Green Shirt", 
      "quantity": 「68", 
      "make": "", 
      "model": "", 
      "price": 「20.00", 
      "size": "XS", 
      "sku": null, 
      "image": "https:\/\/google.com\/green_shirt.jpg", 
      "new_record": false, 
      "category_name": "", 
      "bar_code": "", 
     } 
    ], 
    "Dark Blue Jeans": [ 
     { 
      "id": "1588", 
      "name": "Dark Blue Jeans", 
      "quantity": "0", 
      "make": "", 
      "model": "", 
      "price": "0.00", 
      "size": 「S", 
      "sku": null, 
      "image": "https:\/\/google.com\/dark_blue_jeans.jpg", 
      "new_record": false, 
      "category_name": "", 
      "bar_code": "", 
      "category": null 
     }, 
     { 
      "id": "1559", 
      "name": "Dark Blue Jeans", 
      "quantity": "4", 
      "make": "", 
      "model": "", 
      "price": "0.00", 
      "size": 「XL", 
      "sku": null, 
      "image": "https:\/\/google.com\/dark_blue_jeans.jpg", 
      "new_record": false, 
      "category_name": "", 
      "bar_code": "", 
      "category": null 
     } 
    ], 
    "White Belt": [ 
     { 
      "id": "1536", 
      "name": "White Belt", 
      "quantity": "37", 
      "make": "", 
      "model": "", 
      "price": "0.00", 
      "size": "One Size", 
      "sku": null, 
      "image": "https:\/\/google.com\/white_belt.jpg", 
      "new_record": false, 
      "category_name": "", 
      "bar_code": "", 
      "category": null 
     } 
    ] 
} 

這裏是POJO:

public class Product 
{ 
    private String model; 

    private String bar_code; 

    private String image; 

    private null sku; 

    private String new_record; 

    private String size; 

    private String id; 

    private null category; 

    private String price; 

    private String category_name; 

    private String name; 

    private String quantity; 

    private String make; 

    public String getModel() 
    { 
     return model; 
    } 

    public void setModel (String model) 
    { 
     this.model = model; 
    } 

    public String getBar_code() 
    { 
     return bar_code; 
    } 

    public void setBar_code (String bar_code) 
    { 
     this.bar_code = bar_code; 
    } 

    public String getImage() 
    { 
     return image; 
    } 

    public void setImage (String image) 
    { 
     this.image = image; 
    } 

    public null getSku() 
    { 
     return sku; 
    } 

    public void setSku (null sku) 
    { 
     this.sku = sku; 
    } 

    public String getNew_record() 
    { 
     return new_record; 
    } 

    public void setNew_record (String new_record) 
    { 
     this.new_record = new_record; 
    } 

    public String getSize() 
    { 
     return size; 
    } 

    public void setSize (String size) 
    { 
     this.size = size; 
    } 

    public String getId() 
    { 
     return id; 
    } 

    public void setId (String id) 
    { 
     this.id = id; 
    } 

    public null getCategory() 
    { 
     return category; 
    } 

    public void setCategory (null category) 
    { 
     this.category = category; 
    } 

    public String getPrice() 
    { 
     return price; 
    } 

    public void setPrice (String price) 
    { 
     this.price = price; 
    } 

    public String getCategory_name() 
    { 
     return category_name; 
    } 

    public void setCategory_name (String category_name) 
    { 
     this.category_name = category_name; 
    } 

    public String getName() 
    { 
     return name; 
    } 

    public void setName (String name) 
    { 
     this.name = name; 
    } 

    public String getQuantity() 
    { 
     return quantity; 
    } 

    public void setQuantity (String quantity) 
    { 
     this.quantity = quantity; 
    } 

    public String getMake() 
    { 
     return make; 
    } 

    public void setMake (String make) 
    { 
     this.make = make; 
    } 

    @Override 
    public String toString() 
    { 
     return "ClassPojo [model = "+model+", bar_code = "+bar_code+", image = "+image+", sku = "+sku+", new_record = "+new_record+", size = "+size+", id = "+id+", category = "+category+", price = "+price+", category_name = "+category_name+", name = "+name+", quantity = "+quantity+", make = "+make+"]"; 
    } 
} 

這裏是請求和更新接口:

public static void requestData(String username,String password) { 

     RestAdapter.Builder builder = new RestAdapter.Builder() 
       .setClient(new OkClient(new OkHttpClient())) 
       .setEndpoint(ENDPOINT); 

     if (username != null && password != null) { 
      // concatenate username and password with colon for authentication 
      final String credentials = username + ":" + password; 

      builder.setRequestInterceptor(new RequestInterceptor() { 
       @Override 
       public void intercept(RequestFacade request) { 
        // create Base64 encodet string 
        String string = "Basic " + Base64.encodeToString(credentials.getBytes(), Base64.NO_WRAP); 
        request.addHeader("Accept", "application/json"); 
        request.addHeader("Authorization", string); 
       } 
      }); 
     } 
     RestAdapter adapter = builder.build(); 


     ProductAPI api = adapter.create(ProductAPI.class); 

     api.getInventory(new Callback<List<Product>>() { 
      @Override 
      public void success(List<Product> products, Response response) { 
       Log.d(TAG, response.getUrl()); 
       Log.d(TAG, response.getReason()); 
       mInventory = product; 
      } 
      @Override 
      public void failure(RetrofitError error) { 
       Log.d(TAG,error.getMessage()); 
      } 
     }); 
    } 

public interface ProductAPI { 
    @GET("/v2/get-inventory") 
    public void getInventory(Callback<List<Product>> response); 
} 

這是我得到的,因爲錯誤JSON以'{'而不是'[' com.google.gson.JsonSyntaxException:java.lang.Il legalStateException:期望BEGIN_ARRAY,但是BEGIN_OBJECT在第1行第2列路徑$

回答

0

我告訴改造(GSON?)找一個Map<String,List<Product>>,而不是僅僅一個List<Product>,它想通它出來,多麼方便。 :

 api.getInventory(new Callback<Map<String,List<Product>>>() { 
      @Override 
      public void success(Map<String,List<Product>> products, Response response) { 

       mInventory = products; 
      } 
      @Override 
      public void failure(RetrofitError error) { 
       Log.d(TAG,error.getMessage()); 
      } 
     }); 

public interface ProductAPI { 
    @GET("/v2/get-inventory") 
    public void getInventory(Callback<Map<String,List<Product>>> response); 
} 
0

問題看起來完全是這樣說的。答案是:有一個數組的對象,而不是直接的數組:

{ [{},{},{}] } 

如果回答是:

[{},{},{}] 

它的工作,但你不能改變的響應格式,你應該改變你java的。而不是期待產品列表,你應該有一個產品列表裏面有一個對象。這是改造將如何映射它。

編輯:
響應實際上如下:

{[],[],[]} 

不是列表,是對象產品的三個列表

到對象的映射必須是由物體:

public class Wrapper 
{ 
    List<Product> WhiteBelt; 
    List<Product> DarkBlueJeans; 
    List<Product> GreenShirt; 

} 

類似的東西就是你的結構。我不相信是正確的,因爲你通常需要一個列表,而不是一個包含「硬編碼」內的三個列表的對象。

讓我知道這是否有助於

編輯:

所以你需要財產以後這樣的:

{ 
    "products": 
    [{ "name" : "Green Shirt" 
     "items": 
     [{"id": "740", 
      "name": 「Nice Green Shirt", 
      "quantity": "0", 
      "make": "", 
      "model": "", 
      "price": 「15.00", 
      "size": "XXS", 
      "sku": null, 
      "image": "https:\/\/google.com\/green_shirt.jpg", 
      "new_record": false, 
      "category_name": "", 
      "bar_code": "" 
     }] 
     }] 
} 

這將是可以接受的產品和每個prodcut內部列表的結構,你有物品清單(你已經擁有的物品清單)。

在Java:

public class ProductList 
{ 
    List <ProductList> productList; 
} 

public class ProductList 
{ 
    String name; 
    List<Product> items; 
} 

使用原來的產品類別

+0

這似乎是對我來說。 – sray

+0

該列表未在我的新對象中填充出現問題:「public class Inventory { public List getProducts(){ return products; } public void setProducts(List products){ this.products = products; } private listing products; }' – sray

+0

更新了答案 – Franklin

0

你JSON是無效的,嘗試做任何事情之前先正確格式化。

正確格式化:

{ 
    "Green Shirt": [ 
    { 
     "id": "740", 
     "name": "Nice Green Shirt", 
     "quantity": "0", 
     "make": "", 
     "model": "", 
     "price": "15.00", 
     "size": "XXS", 
     "sku": null, 
     "image": "https:\\/\\/google.com\\/green_shirt.jpg", 
     "new_record": false, 
     "category_name": "", 
     "bar_code": "" 
    }, 
    { 
     "id": "743", 
     "name": "Green Shirt", 
     "quantity": "68", 
     "make": "", 
     "model": "", 
     "price": "20.00", 
     "size": "XS", 
     "sku": null, 
     "image": "https:\\/\\/google.com\\/green_shirt.jpg", 
     "new_record": false, 
     "category_name": "", 
     "bar_code": "" 
    } 
    ], 
    "Dark Blue Jeans": [ 
    { 
     "id": "1588", 
     "name": "Dark Blue Jeans", 
     "quantity": "0", 
     "make": "", 
     "model": "", 
     "price": "0.00", 
     "size": "S", 
     "sku": null, 
     "image": "https:\\/\\/google.com\\/dark_blue_jeans.jpg", 
     "new_record": false, 
     "category_name": "", 
     "bar_code": "", 
     "category": null 
    }, 
    { 
     "id": "1559", 
     "name": "Dark Blue Jeans", 
     "quantity": "4", 
     "make": "", 
     "model": "", 
     "price": "0.00", 
     "size": "XL", 
     "sku": null, 
     "image": "https:\\/\\/google.com\\/dark_blue_jeans.jpg", 
     "new_record": false, 
     "category_name": "", 
     "bar_code": "", 
     "category": null 
    } 
    ], 
    "White Belt": [ 
    { 
     "id": "1536", 
     "name": "White Belt", 
     "quantity": "37", 
     "make": "", 
     "model": "", 
     "price": "0.00", 
     "size": "One Size", 
     "sku": null, 
     "image": "https:\\/\\/google.com\\/white_belt.jpg", 
     "new_record": false, 
     "category_name": "", 
     "bar_code": "", 
     "category": null 
    } 
    ] 
} 

一些它有錯誤的:

Error:Strings should be wrapped in double quotes.[Code 17, Structure 12] 
Error:Strings should be wrapped in double quotes.[Code 17, Structure 28] 
Error:Invalid comma, expecting }.[Code 141, Structure 53] 
Error:Expecting string, not }.[Code 8, Structure 54] 
Error:Strings should be wrapped in double quotes.[Code 17, Structure 67] 
Error:Strings should be wrapped in double quotes.[Code 17, Structure 79] 
Error:Invalid comma, expecting }.[Code 141, Structure 104] 
Error:Expecting string, not }.[Code 8, Structure 105] 
Error:Strings should be wrapped in double quotes.[Code 17, Structure 138] 
Error:Strings should be wrapped in double quotes.[Code 17, Structure 192] 
+0

我不好,我必須複製粘貼它不正確 – sray