使用GSON改进解析问题

时间:2017-07-28 07:15:19

标签: android json web-services retrofit2

我知道很多时候都会问这个问题。但是我无法理解这个问题。

我收到以下错误。

java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 1 column 1 path $

以下是我的代码。

  PriceInterface priceInterface = ApiClient.getClient().create(PriceInterface.class);

    Map<String, String> params = new HashMap<String, String>();
    params.put("date", "today");
    params.put("circle", "Chandigarh");

    Call<PriceListBean> call = priceInterface.getMyThing(params);

    call.enqueue(new Callback<PriceListBean>() {
        @Override
        public void onResponse(Call<PriceListBean> call, Response<PriceListBean> response) {
            Log.e("response is","++++"+response.body().toString());
        }

        @Override
        public void onFailure(Call<PriceListBean> call, Throwable t) {

            Log.e("failure message is",""+t.getLocalizedMessage());
           // t.getLocalizedMessage();

        }
    });

以下是我从服务器获取的json。

 {
    "price": [{
        "id": 678,
        "date_time": "2017-07-25",
        "type": "Diesel",
        "place": "Chandigarh",
        "price": "55.75"
    }, {
        "id": 639,
        "date_time": "2017-07-25",
        "type": "Petrol",
        "place": "Chandigarh",
        "price": "64.74"
    }],
    "errorCode": 0,
    "errorDesc": "Success"
}

我为此创造了Pojo。

public class PriceListBean {

@SerializedName("price")
@Expose
private List<Price> price = null;
@SerializedName("errorCode")
@Expose
private Integer errorCode;
@SerializedName("errorDesc")
@Expose
private String errorDesc;

public List<Price> getPrice() {
    return price;
}

public void setPrice(List<Price> price) {
    this.price = price;
}

public Integer getErrorCode() {
    return errorCode;
}

public void setErrorCode(Integer errorCode) {
    this.errorCode = errorCode;
}

public String getErrorDesc() {
    return errorDesc;
}

public void setErrorDesc(String errorDesc) {
    this.errorDesc = errorDesc;
}

}

这可以通过Volley或Gson轻松完成但是当我尝试使用Retrofit时,我陷入了困境。我不确定我是否正确格式json

2 个答案:

答案 0 :(得分:0)

您可以向我们提供您的Price对象吗? 在我的头顶,你的Price类看起来应该是这样的

public class Price {
    @SerializedName("id")
    @Expose
    private long id;
    @SerializedName("date_time")
    @Expose
    private String date_time;
    @SerializedName("type")
    @Expose
    private String type;
    @SerializedName("place")
    @Expose
    private String place;
    @SerializedName("price")
    @Expose
    private String price;

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

    public long getId() {
        return this.id;
    }

    public void setDate_time(String date_time) {
        this.date_time = date_time;
    }

    public String getDate_time() {
        return this.date_time;
    }

    public void setType(String type) {
        this.type = type;
    }

    public String getType() {
        return this.type;
    }

    public void setPlace(String place) {
        this.place = place;
    }

    public String getPlace() {
        return this.place;
    }

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

    public String getPrice() {
        return this.price;
    }
}

尝试使用此代替您当前的Price对象,并告知错误是否仍然存在。

答案 1 :(得分:0)

您的回复获取数据列表,然后更改此代码..

 Log.e("response is","++++"+response.body().toString());

并在代码下面使用..

  @Override
    public void onResponse(Call<PriceListBean> call, Response<PriceListBean> response) {
        List<PriceListBean> data=response.body();
       // Log.e("response is","++++"+response.body().toString());
    }

我希望您将以下依赖项添加到应用程序级别的gradle文件中。

    implementation 'com.squareup.retrofit2:converter-gson:2.3.0'