JSON错误:由以下引起:java.lang.IllegalStateException:预期BEGIN_OBJECT但在第1行第1行STRING为STRING

时间:2017-05-19 14:20:08

标签: android json gson retrofit2

我想使用Retrofit 2从服务器检索数据。我已经阅读了很多相同问题的答案,但我无法弄清楚问题是什么,thisthis。但是,我一次又一次地遇到同样的问题。

这是我的界面类

public interface Product_APi {
    @GET("/s=&cid=4&page=1&sort=1&manufacturer=&limit=100&color=&size=&price=")
    Call<Total_Products> getProductDetail();
}

这是我的第一个模型类

package com.hussain.lithoproductdetail;

import com.google.gson.annotations.SerializedName;

public class Product {
    @SerializedName("id")
    private String id;
    @SerializedName("total_pages")
    private int total_pages;
    @SerializedName("name")
    private String name;
    @SerializedName("type")
    private String type;
    @SerializedName("sku")
    private String sku;
    @SerializedName("img")
    private String img;
    @SerializedName("img2")
    private String img2;
    @SerializedName("default_sort")
    private String default_sort;
    @SerializedName("price")
    private int price;
    @SerializedName("sale_price")
    private String sale_price;
    @SerializedName("stock_qty")
    private String stock_qty;
    @SerializedName("stock_qty_min")
    private String stock_qty_min;
    @SerializedName("stock_qty_min_sales")
    private String stock_qty_min_sales;
    @SerializedName("status")
    private String status;
    @SerializedName("currency")
    private String currency;
    @SerializedName("category_id")
    private String category_id;
    @SerializedName("start")
    private int start;
    @SerializedName("limit")
    private String limit;
    @SerializedName("manufacturer")
    private String manufacturer;

    public Product() {
    }

    public Product(String id, int total_pages, String name, String type, String sku, String img, String img2, String default_sort, int price, String sale_price, String stock_qty, String stock_qty_min, String stock_qty_min_sales, String status, String currency, String category_id, int start, String limit, String manufacturer) {
        this.id = id;
        this.total_pages = total_pages;
        this.name = name;
        this.type = type;
        this.sku = sku;
        this.img = img;
        this.img2 = img2;
        this.default_sort = default_sort;
        this.price = price;
        this.sale_price = sale_price;
        this.stock_qty = stock_qty;
        this.stock_qty_min = stock_qty_min;
        this.stock_qty_min_sales = stock_qty_min_sales;
        this.status = status;
        this.currency = currency;
        this.category_id = category_id;
        this.start = start;
        this.limit = limit;
        this.manufacturer = manufacturer;
    }

    public String getId() {
        return id;
    }

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

    public int getTotal_pages() {
        return total_pages;
    }

    public void setTotal_pages(int total_pages) {
        this.total_pages = total_pages;
    }

    public String getName() {
        return name;
    }

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

    public String getType() {
        return type;
    }

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

    public String getSku() {
        return sku;
    }

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

    public String getImg() {
        return img;
    }

    public void setImg(String img) {
        this.img = img;
    }

    public String getImg2() {
        return img2;
    }

    public void setImg2(String img2) {
        this.img2 = img2;
    }

    public String getDefault_sort() {
        return default_sort;
    }

    public void setDefault_sort(String default_sort) {
        this.default_sort = default_sort;
    }

    public int getPrice() {
        return price;
    }

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

    public String getSale_price() {
        return sale_price;
    }

    public void setSale_price(String sale_price) {
        this.sale_price = sale_price;
    }

    public String getStock_qty() {
        return stock_qty;
    }

    public void setStock_qty(String stock_qty) {
        this.stock_qty = stock_qty;
    }

    public String getStock_qty_min() {
        return stock_qty_min;
    }

    public void setStock_qty_min(String stock_qty_min) {
        this.stock_qty_min = stock_qty_min;
    }

    public String getStock_qty_min_sales() {
        return stock_qty_min_sales;
    }

    public void setStock_qty_min_sales(String stock_qty_min_sales) {
        this.stock_qty_min_sales = stock_qty_min_sales;
    }

    public String getStatus() {
        return status;
    }

    public void setStatus(String status) {
        this.status = status;
    }

    public String getCurrency() {
        return currency;
    }

    public void setCurrency(String currency) {
        this.currency = currency;
    }

    public String getCategory_id() {
        return category_id;
    }

    public void setCategory_id(String category_id) {
        this.category_id = category_id;
    }

    public int getStart() {
        return start;
    }

    public void setStart(int start) {
        this.start = start;
    }

    public String getLimit() {
        return limit;
    }

    public void setLimit(String limit) {
        this.limit = limit;
    }

    public String getManufacturer() {
        return manufacturer;
    }

    public void setManufacturer(String manufacturer) {
        this.manufacturer = manufacturer;
    }
}

这是我的第二个模型类

package com.hussain.lithoproductdetail;

import android.widget.LinearLayout;

import com.google.gson.annotations.SerializedName;

import java.util.ArrayList;
import java.util.List;


public class Total_Products {
    private List<Product> products;


    public Total_Products(){}
    public Total_Products(List<Product> products) {
        this.products = products;
    }

    public List<Product> getProducts() {
        return products;
    }

    public void setProducts(List<Product> products) {
        this.products = products;
    }
}

这是我的Retrofit Manager Class

package com.hussain.lithoproductdetail;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.hussain.lithoproductdetail.constant.Constants;

import okhttp3.OkHttpClient;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;



public class RestManager {
    private static Retrofit retrofit = null;
    private Product_APi mProduct_aPi;

    Gson gson = new GsonBuilder()
            .setLenient()
            .create();

    OkHttpClient client = new OkHttpClient.Builder()
            .addInterceptor(new BasicAuthInterceptor("myUserName", "myPassword"))
            .build();

    public Product_APi getmFlowerApi(){
        if(retrofit==null){
            Retrofit mretrofit  = new Retrofit.Builder()
                    .baseUrl(Constants.Http.BASE_URL)
                    .client(client)
                    .addConverterFactory(GsonConverterFactory.create(gson))
                    .build();
            mProduct_aPi = mretrofit.create(Product_APi.class);
        }
        return  mProduct_aPi;
    }
}

Log Cat错误是:

com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 1 column 1 path $
05-19 18:33:24.381 31343-31343/com.hussain.lithoproductdetail W/System.err:     at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:224)
05-19 18:33:24.381 31343-31343/com.hussain.lithoproductdetail W/System.err:     at retrofit2.converter.gson.GsonResponseBodyConverter.convert(GsonResponseBodyConverter.java:37)
05-19 18:33:24.381 31343-31343/com.hussain.lithoproductdetail W/System.err:     at retrofit2.converter.gson.GsonResponseBodyConverter.convert(GsonResponseBodyConverter.java:25)
05-19 18:33:24.381 31343-31343/com.hussain.lithoproductdetail W/System.err:     at retrofit2.ServiceMethod.toResponse(ServiceMethod.java:118)
05-19 18:33:24.381 31343-31343/com.hussain.lithoproductdetail W/System.err:     at retrofit2.OkHttpCall.parseResponse(OkHttpCall.java:212)
05-19 18:33:24.381 31343-31343/com.hussain.lithoproductdetail W/System.err:     at retrofit2.OkHttpCall$1.onResponse(OkHttpCall.java:106)
05-19 18:33:24.381 31343-31343/com.hussain.lithoproductdetail W/System.err:     at okhttp3.RealCall$AsyncCall.execute(RealCall.java:135)
05-19 18:33:24.381 31343-31343/com.hussain.lithoproductdetail W/System.err:     at okhttp3.internal.NamedRunnable.run(NamedRunnable.java:32)
05-19 18:33:24.381 31343-31343/com.hussain.lithoproductdetail W/System.err:     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
05-19 18:33:24.381 31343-31343/com.hussain.lithoproductdetail W/System.err:     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
05-19 18:33:24.381 31343-31343/com.hussain.lithoproductdetail W/System.err:     at java.lang.Thread.run(Thread.java:818)
05-19 18:33:24.381 31343-31343/com.hussain.lithoproductdetail W/System.err: Caused by: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 1 column 1 path $
05-19 18:33:24.381 31343-31343/com.hussain.lithoproductdetail W/System.err:     at com.google.gson.stream.JsonReader.beginObject(JsonReader.java:385)
05-19 18:33:24.381 31343-31343/com.hussain.lithoproductdetail W/System.err:     at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:213)
05-19 18:33:24.381 31343-31343/com.hussain.lithoproductdetail W/System.err:     ... 10 more
05-19 18:33:24.381 31343-31343/com.hussain.lithoproductdetail I/Error: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 1 column 1 path $

1 个答案:

答案 0 :(得分:0)

我收到了额外/的以下错误。

String BASE_URL = "www.example.com/"

在端点我再次连接/

  

@GET( “/ S =&安培; CID = 4和;页= 1&安培;排序= 1&安培;制造商=安培;限= 100安培;颜色=安培;大小=安培;价格=”)       调用getProductDetail();

从端点开始删除额外/后,问题已解决。所有其他都是对的。