我是新来改装的。我已经在大多数Web服务中使用了volley,决定继续改进。但是我被Json解析所困,我在POJO类中得到了空数据。我尝试通过调用
检查响应,我的响应是正确的,当我在模型类中解析时,我得到的是空白数据。请指导我,任何帮助都将不胜感激。
注意:POJO类是可parcelable,还包含cunstructor和gette/setter。我从这里删除了它,因为文本限制。
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(PRODUCT_URL)
.addConverterFactory(GsonConverterFactory.create())
.build();
ApiInterface apiService = retrofit.create(ApiInterface.class);
JsonObject ob = new JsonObject();
ob.addProperty("pro_id", model.getId());
Call<Product> call = apiService.getProducts(ob);
call.enqueue(new Callback<Product>() {
@Override
public void onResponse(Call<Product> call, Response<Product> response) {
Product p = response.body();
Toast.makeText(getActivity(), response.body().getTitle(), Toast.LENGTH_SHORT).show();
}
@Override
public void onFailure(Call<Product> call, Throwable t) {
Log.e("ERROR",call.toString());
}
});
public interface ApiInterface {
@POST("app/product.php")
Call<Product> getProducts(@Body JsonObject jsonObject);
}
public class Product {
@SerializedName("title")
@Expose
public String title;
@SerializedName("id")
@Expose
public int id;
@SerializedName("created_at")
@Expose
public String createdAt;
@SerializedName("updated_at")
@Expose
public String updatedAt;
@SerializedName("type")
@Expose
public String type;
@SerializedName("status")
@Expose
public String status;
@SerializedName("downloadable")
@Expose
public boolean downloadable;
@SerializedName("virtual")
@Expose
public boolean virtual;
@SerializedName("permalink")
@Expose
public String permalink;
@SerializedName("sku")
@Expose
public String sku;
@SerializedName("price")
@Expose
public String price;
@SerializedName("regular_price")
@Expose
public String regularPrice;
@SerializedName("sale_price")
@Expose
public Object salePrice;
@SerializedName("price_html")
@Expose
public String priceHtml;
@SerializedName("taxable")
@Expose
public boolean taxable;
@SerializedName("tax_status")
@Expose
public String taxStatus;
@SerializedName("tax_class")
@Expose
public String taxClass;
@SerializedName("managing_stock")
@Expose
public boolean managingStock;
@SerializedName("stock_quantity")
@Expose
public int stockQuantity;
@SerializedName("in_stock")
@Expose
public boolean inStock;
@SerializedName("backorders_allowed")
@Expose
public boolean backordersAllowed;
@SerializedName("backordered")
@Expose
public boolean backordered;
@SerializedName("sold_individually")
@Expose
public boolean soldIndividually;
@SerializedName("purchaseable")
@Expose
public boolean purchaseable;
@SerializedName("featured")
@Expose
public boolean featured;
@SerializedName("visible")
@Expose
public boolean visible;
@SerializedName("catalog_visibility")
@Expose
public String catalogVisibility;
@SerializedName("on_sale")
@Expose
public boolean onSale;
@SerializedName("product_url")
@Expose
public String productUrl;
@SerializedName("button_text")
@Expose
public String buttonText;
@SerializedName("weight")
@Expose
public Object weight;
@SerializedName("dimensions")
@Expose
public Dimensions dimensions;
@SerializedName("shipping_required")
@Expose
public boolean shippingRequired;
@SerializedName("shipping_taxable")
@Expose
public boolean shippingTaxable;
@SerializedName("shipping_class")
@Expose
public String shippingClass;
@SerializedName("shipping_class_id")
@Expose
public Object shippingClassId;
@SerializedName("description")
@Expose
public String description;
@SerializedName("short_description")
@Expose
public String shortDescription;
@SerializedName("reviews_allowed")
@Expose
public boolean reviewsAllowed;
@SerializedName("average_rating")
@Expose
public String averageRating;
@SerializedName("rating_count")
@Expose
public int ratingCount;
@SerializedName("related_ids")
@Expose
public List<Integer> relatedIds = new ArrayList<Integer>();
@SerializedName("upsell_ids")
@Expose
public List<Object> upsellIds = new ArrayList<Object>();
@SerializedName("cross_sell_ids")
@Expose
public List<Integer> crossSellIds = new ArrayList<Integer>();
@SerializedName("parent_id")
@Expose
public int parentId;
@SerializedName("categories")
@Expose
public List<String> categories = new ArrayList<String>();
@SerializedName("tags")
@Expose
public List<Object> tags = new ArrayList<Object>();
@SerializedName("images")
@Expose
public List<Image> images = new ArrayList<Image>();
@SerializedName("featured_src")
@Expose
public String featuredSrc;
@SerializedName("attributes")
@Expose
public List<Attribute> attributes = new ArrayList<Attribute>();
@SerializedName("downloads")
@Expose
public List<Object> downloads = new ArrayList<Object>();
@SerializedName("download_limit")
@Expose
public int downloadLimit;
@SerializedName("download_expiry")
@Expose
public int downloadExpiry;
@SerializedName("download_type")
@Expose
public String downloadType;
@SerializedName("purchase_note")
@Expose
public String purchaseNote;
@SerializedName("total_sales")
@Expose
public int totalSales;
@SerializedName("variations")
@Expose
public List<Variation> variations = new ArrayList<Variation>();
@SerializedName("parent")
@Expose
public List<Object> parent = new ArrayList<Object>();
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
}
JSON响应
{
"product": {
"title": "Flying Ninja",
"id": 70,
"created_at": "2013-06-07T11:25:01Z",
"updated_at": "2017-09-26T05:55:35Z",
"type": "simple",
"status": "publish",
"downloadable": false,
"virtual": false,
"permalink": "http:\/\/codiixx.com\/product\/flying-ninja\/",
"sku": "",
"price": "12.00",
"regular_price": "15.00",
"sale_price": "12.00",
"price_html": "<del><span class=\"woocommerce-Price-amount amount\"><span class=\"woocommerce-Price-currencySymbol\">$<\/span>15.00<\/span><\/del> <ins><span class=\"woocommerce-Price-amount amount\"><span class=\"woocommerce-Price-currencySymbol\">$<\/span>12.00<\/span><\/ins>",
"taxable": true,
"tax_status": "taxable",
"tax_class": "",
"managing_stock": false,
"stock_quantity": null,
"in_stock": true,
"backorders_allowed": false,
"backordered": false,
"sold_individually": false,
"purchaseable": true,
"featured": false,
"visible": true,
"catalog_visibility": "visible",
"on_sale": true,
"product_url": "",
"button_text": "",
"weight": null,
"dimensions": {
"length": "",
"width": "",
"height": "",
"unit": "cm"
},
"shipping_required": true,
"shipping_taxable": true,
"shipping_class": "",
"shipping_class_id": null,
"description": "<p>Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Vestibulum tortor quam, feugiat vitae, ultricies eget, tempor sit amet, ante. Donec eu libero sit amet quam egestas semper. Aenean ultricies mi vitae est. Mauris placerat eleifend leo.<\/p>\n",
"short_description": "<p>Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Vestibulum tortor quam, feugiat vitae, ultricies eget, tempor sit amet, ante. Donec eu libero sit amet quam egestas semper. Aenean ultricies mi vitae est. Mauris placerat eleifend leo.<\/p>\n",
"reviews_allowed": true,
"average_rating": "4.00",
"rating_count": 4,
"related_ids": [
76,
79,
73,
67
],
"upsell_ids": [
],
"cross_sell_ids": [
],
"parent_id": 0,
"categories": [
"Posters"
],
"tags": [
],
"images": [
{
"id": 71,
"created_at": "2013-06-07T11:24:19Z",
"updated_at": "2013-06-07T11:24:19Z",
"src": "http:\/\/codiixx.com\/wp-content\/uploads\/2013\/06\/poster_2_up.jpg",
"title": "poster_2_up",
"alt": "",
"position": 0
},
{
"id": 72,
"created_at": "2013-06-07T11:24:47Z",
"updated_at": "2013-06-07T11:24:47Z",
"src": "http:\/\/codiixx.com\/wp-content\/uploads\/2013\/06\/Poster_2_flat.jpg",
"title": "Poster_2_flat",
"alt": "",
"position": 1
},
{
"id": 10260,
"created_at": "2017-07-10T06:51:22Z",
"updated_at": "2017-07-10T06:51:22Z",
"src": "http:\/\/codiixx.com\/wp-content\/uploads\/2017\/07\/images.jpeg",
"title": "images",
"alt": "",
"position": 2
},
{
"id": 10261,
"created_at": "2017-07-10T06:52:18Z",
"updated_at": "2017-07-10T06:52:18Z",
"src": "http:\/\/codiixx.com\/wp-content\/uploads\/2017\/07\/Jazbaa-Movie-Full-Audio-Album-Free-Download-Mp3-Song-2015.jpg",
"title": "Jazbaa-Movie-Full-Audio-Album-Free-Download-Mp3-Song-2015",
"alt": "",
"position": 3
},
{
"id": 10264,
"created_at": "2017-07-10T09:34:49Z",
"updated_at": "2017-07-10T09:34:49Z",
"src": "http:\/\/codiixx.com\/wp-content\/uploads\/2017\/07\/entrepreneur-593371_640.jpg",
"title": "entrepreneur-593371_640",
"alt": "",
"position": 4
},
{
"id": 10234,
"created_at": "2017-04-06T06:01:10Z",
"updated_at": "2017-04-06T06:01:10Z",
"src": "http:\/\/codiixx.com\/wp-content\/uploads\/2017\/04\/img1.jpg",
"title": "img1",
"alt": "",
"position": 5
},
{
"id": 469,
"created_at": "2016-11-04T06:26:58Z",
"updated_at": "2017-04-06T05:03:30Z",
"src": "http:\/\/codiixx.com\/wp-content\/uploads\/2016\/11\/test1-1.jpg",
"title": "test1",
"alt": "",
"position": 6
},
{
"id": 10215,
"created_at": "2017-03-28T06:17:00Z",
"updated_at": "2017-04-06T05:03:29Z",
"src": "http:\/\/codiixx.com\/wp-content\/uploads\/2017\/03\/download.jpg",
"title": "download",
"alt": "",
"position": 7
},
{
"id": 327,
"created_at": "2016-06-24T10:42:33Z",
"updated_at": "2016-06-24T10:42:33Z",
"src": "http:\/\/codiixx.com\/wp-content\/uploads\/2016\/06\/logo-phoeniixx.jpg",
"title": "logo-phoeniixx",
"alt": "",
"position": 8
},
{
"id": 98,
"created_at": "2013-06-07T11:37:17Z",
"updated_at": "2013-06-07T11:37:17Z",
"src": "http:\/\/codiixx.com\/wp-content\/uploads\/2013\/06\/cd_5_flat.jpg",
"title": "cd_5_flat",
"alt": "",
"position": 9
}
],
"featured_src": "http:\/\/codiixx.com\/wp-content\/uploads\/2013\/06\/poster_2_up.jpg",
"attributes": [
],
"downloads": [
],
"download_limit": -1,
"download_expiry": -1,
"download_type": "standard",
"purchase_note": "",
"total_sales": 91,
"variations": [
],
"parent": [
]
}
}
虽然使用json对象将json数据发送到URL,但主要目的是获取数据,因此注释应该是get而不是post
public interface ApiInterface {
@GET("app/product.php")
Call<Product> getProducts(@Body JsonObject jsonObject);
}
但是我的DetailModel是一个基类,我的user.class和organization.class是DetailModel的子类。但是我不知道如何解析对我的用户或组织模型的响应。
其答复如下: 我相信问题出在响应前面的jsonFlickrApi上。 执行以下代码时: }
我正在使用凌空OkHttp从服务器获取一些数据。 响应是一个包含JSON的字符串,我想使用GSON/POJO解析它。 我得到错误: 预期BEGIN_OBJECT,但在第1行第1列路径$上是STRING 尝试解析时。 原因:java.lang.IllegalStateException:预期BEGIN_OBJECT但在第1行第1列路径$ com.google.gson.stream.JsonRead
问题内容: 我已经尝试了所有方法,但是解决方案非常丑陋,我真的想要一个直接的答案,是否可以改进(这意味着是否需要使用其他实现)。 问题在于带有GSON的Map of Maps: 根据Firebug给我以下回应: 大约有200个,但是只有两个。到目前为止,这是我在课堂上要解析的内容: 所以我这样访问数据: 但是正如您所看到的,这将是很长的时间。另一个问题是某些“值”中包含“&”号,这意味着我不能在此