我遇到了问题。我试图通过Retrofit从服务器获取JSON,但我无法根据数据库模型将其转换回列表。
代码是这样的:
public void syncFromCloud() throws SQLException, IOException {
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(IntentConstants.API_URL)
.addConverterFactory(GsonConverterFactory.create())
.build();
FoodLogInterface service = retrofit.create(FoodLogInterface.class);
Call<List<FoodLog>> call = service.getFoodLogList(mFoodLogs);
}
我通过Retrofit这样连接:
public interface FoodLogInterface {
@GET("http://pastebin.com/6Uaq4ZHW")
Call<List<FoodLog>> getFoodLogList(@Query ("http://pastebin.com/UhE12m3m") List<FoodLog> foodLog);
}
我可以从这里获得GSON,但是我无法将数据bac添加到mFoodLogs列表中。
这是我想要在课堂上放回的JSON,我也是从pastebin链接中检索它:http://pastebin.com/6Uaq4ZHW
[{
"date": "Thursday, February 4, 2016 at 1:28 PM",
"dateEdited": "Thursday, February 4, 2016 at 1:28 PM",
"description": "",
"id": "6d15cf24-3f0a-4fed-814f-1c16fe93ebd2",
"title": "yhytt",
"isSavedToCloud": false,
"isDeleted": false
}, {
"date": "Thursday, February 4, 2016 at 1:31 PM",
"dateEdited": "Thursday, February 4, 2016 at 1:31 PM",
"description": "",
"id": "f3e7c018-04e7-428d-813b-36712bf0821d",
"title": "yhytt",
"isSavedToCloud": false,
"isDeleted": false
}]
答案 0 :(得分:0)
我猜你需要将返回的json存储到mFoodLogs变量中。为什么需要在服务调用中将mFoodLogs作为参数传递?
我建议进行以下更改。
1.取消呼叫,直接返回列表。同时删除您传递的查询参数。
public interface FoodLogInterface {
@GET("http://pastebin.com/6Uaq4ZHW")
List<FoodLog> getFoodLogList();
}
2.将返回的值直接分配给mFoodLogs
public void syncFromCloud() throws SQLException, IOException {
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(IntentConstants.API_URL)
.addConverterFactory(GsonConverterFactory.create())
.build();
FoodLogInterface service = retrofit.create(FoodLogInterface.class);
mFoodLogs = service.getFoodLogList();
}
如果您的FoodLog类包含json中返回的所有属性,则应在mFoodLogs中正确设置值