ORMLite 数据库模型通过改造适配器错误转为 GSON; java.lang.IllegalStateException

ORMLite database model to GSON via retrofit adapter error; java.lang.IllegalStateException

我收到此错误:

com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_ARRAY but was STRING at line 1 column 1 path $

我正在尝试从 JSON http://pastebin.com/bC8s3bUf 直接从 url.

创建回调

数据模型表示为 ORMLite 数据库模型。通过 GSON 手动创建 JSON 会创建此 JSON 字符串:http://pastebin.com/bC8s3bUf

数据库模型

@DatabaseField(id = true, index = true, canBeNull = true, columnName = ID)
String id;

@DatabaseField(columnName = TITLE)
String title;

@DatabaseField(columnName = DESCRIPTION)
String description;

@DatabaseField(columnName = DATE)
String date;

@DatabaseField(columnName = IMAGE)
String image;

@DatabaseField(columnName = IS_SAVED_TO_CLOUD)
boolean isSavedToCloud;

@DatabaseField(columnName = DATE_EDITED)
String dateEdited;

@DatabaseField(columnName = IS_DELETED)
boolean isDeleted;

public FoodLog() {
    id = UUID.randomUUID().toString();
    isSavedToCloud = false;
    isDeleted = false;
}

@Override
public int describeContents() {
    return 0;
}

public FoodLog(Parcel in) {
    this.id = in.readString();
    this.title = in.readString();
    this.description = in.readString();
    this.date = in.readString();
    this.image = in.readString();
    this.isSavedToCloud = (in.readByte() != 0);
    this.dateEdited = in.readString();
    this.isDeleted = (in.readByte() != 0);
}

@Override
public void writeToParcel(Parcel out, int flags) {
    out.writeString(id);
    out.writeString(title);
    out.writeString(description);
    out.writeString(date);
    out.writeString(image);
    out.writeByte((byte) (isSavedToCloud ? 1 : 0));
    out.writeString(dateEdited);
    out.writeByte((byte) (isDeleted ? 1 : 0));
}

public static final Parcelable.Creator<FoodLog> CREATOR = new Parcelable.Creator<FoodLog>(){
    public FoodLog createFromParcel(Parcel in){
        return new FoodLog(in);
    }
    public FoodLog[] newArray(int size){
        return new FoodLog[size];
    }
};

界面

public interface FoodLogInterface {

@GET(IntentConstants.API_URL)
Call<List<FoodLog>> getFoodLog();

@POST(IntentConstants.API_URL)
Call<List<FoodLog>> postFoodLog(@Body List<FoodLog> foodLog);}

正在从 url

获取数据
Retrofit retrofit = new Retrofit.Builder()
            .baseUrl("http://pastebin.com/bC8s3bUf")
            .addConverterFactory(GsonConverterFactory.create())
            .build();
    FoodLogInterface service = retrofit.create(FoodLogInterface.class);
    Call<List<FoodLog>> call = service.getFoodLog();
    call.enqueue(new Callback<List<FoodLog>>() {
        @Override
        public void onResponse(Response<List<FoodLog>> response, Retrofit retrofit) {
            Log.d("CallBack", " response is " + response);
        }
        @Override
        public void onFailure(Throwable t) {
            Log.d("CallBack", " Throwable is " +t);
        }
    });
}

您引用的 URL 不是 .json 文件本身。它更像是整个网页。你的解析失败了。您必须使用 http://pastebin.com/raw/bC8s3bUf 而不是引用 .json 文件本身