简体   繁体   中英

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

I want to retrieve data from server using Retrofit 2. I have read many answers of same question, but I can't figure out what the problem is, this and this . However, I am getting the same problem again and again.

This is my interface class

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

This is my First Model Class

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

This is my Second Model Class

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

This is my 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 error is:

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 $

I am getting the following error with extra / .

String BASE_URL = "www.example.com/"

and in endpoint I was again concatenating / here

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

After removing extra / from the start of endpoint the issue resolved. All other is right.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM