【问题标题】:JSON Error: Caused by: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 1 column 1 path $JSON 错误:原因:java.lang.IllegalStateException:预期 BEGIN_OBJECT 但在第 1 行第 1 列路径 $
【发布时间】:2017-05-19 14:20:07
【问题描述】:

我想使用 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;
    }
}

这是我的改造经理课程

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;
    }
}

日志猫错误是:

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 $

【问题讨论】:

  • 你能分享你返回的json吗?
  • { products: [ { id: "", total_pages: 1, name: "", type: "", sku: "", img: "", img2: "", default_sort: " ", price: 1, sale_price: "", stock_qty: "", stock_qty_min: "", stock_qty_min_sales: "", status: "", currency: "", category_id: "", start: 0, limit: "",制造商:“”}]}
  • JSON 无效,密钥(产品、ID 等)必须用引号括起来。
  • @tima 我不这么认为,JSON 无效,因为我在原始项目中从同一个 API 获取 json 数据,这里我正在测试 Litho 框架,并从同一个 api 得到错误跨度>
  • 感谢所有我解决了我的问题,代码和 Json 响应中没有错误,我得到的错误是由于额外的反斜杠,我在这里添加了额外的反斜杠'@ GET("/s=&cid=4&page=1&sort=1&manufacturer=&limit=100&color=&size=&price=")'

标签: android json gson retrofit2


【解决方案1】:

我收到以下错误,带有额外的 /

String BASE_URL = "www.example.com/"

在端点我再次在这里连接/

@GET("/s=&cid=4&page=1&sort=1&manufacturer=&limit=100&color=&size=&price=") 调用 getProductDetail();

从端点的开头删除额外的/ 后,问题解决了。其他都是对的。

【讨论】:

    猜你喜欢
    • 2018-01-10
    • 1970-01-01
    • 2017-07-04
    • 2020-11-09
    • 1970-01-01
    • 2020-03-10
    • 2021-01-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多