有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java从服务器检索改装请求并将其放入<List<Model>>

我有个问题。我试图通过改造从服务器获取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);
}

我通过如下改装进行连接:

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

共 (1) 个答案

  1. # 1 楼答案

    我猜您需要将返回的json存储到mFoodLogs变量。为什么需要在服务调用中将mFoodLogs作为参数传递

    我建议做以下修改

    一,。删除调用,而是直接返回列表。还要删除正在传递的查询参数

    public interface FoodLogInterface {
    
    @GET("http://pastebin.com/6Uaq4ZHW")
        List<FoodLog> getFoodLogList();
    }
    

    二,。将返回值直接分配给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中正确设置这些值