我收到此错误:
com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_ARRAY but was STRING at line 1 column 1 path $
我试图直接从该网址创建此JSON http://pastebin.com/bC8s3bUf的回调。
数据模型表示为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);}
从网址获取数据
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);
}
});
}
答案 0 :(得分:1)
您引用的URL不是.json文件本身。它更像是整个网页。你的解析失败了。您必须使用http://pastebin.com/raw/bC8s3bUf而不是引用.json文件本身