RestAdapter(改造)未在 android 中解析

RestAdapter (retrofit) not resolving in android

所以我正在尝试为我的项目使用 Retrofit。正如该网站所说,我已经包括 compile 'com.squareup.retrofit:retrofit:2.0.0-beta1'build.gradle 中。我正在阅读 link 中的教程。我想做类似这样的事情

final RestAdapter restadapter = new RestAdapter.Builder().setEndpoint("http://services.hanselandpetal.com").build();

        api flowerapi = restadapter.create(api.class);

        flowerapi.getData(new Callback<List<Flower>>() {
            @Override
            public void success(List<Flower> flowers, Response response) {
                flowerList = flowers;
                adapter adapt = new adapter(getApplicationContext(),R.layout.item_file,flowerList);
                //ListView listView = (ListView) findViewById(R.id.list);
                setListAdapter(adapt);
            }

在我的项目中,即对 API 进行一些调用。但是 restadapter 只是没有得到 resolved。将鼠标悬停在上面时,它只会显示 symbol can't be resolved。这里发生了什么?

版本 2 中的 API 发生了变化。这是您在此版本中的做法:

Retrofit retrofit = new Retrofit.Builder()
    .baseUrl("https://api.github.com")
    .build();

GitHubService service = retrofit.create(GitHubService.class);

更多信息请参考这里:Retrofit 2 home page

和这些幻灯片:Retrofit 2 presentation

您有两个选择:

1) 使用稳定的 Retrofit 1

这里有您需要的 RestAdapter class。

compile 'com.squareup.retrofit:retrofit:1.9.0'

2) 迁移到改造 2

RestAdapter class 重命名为 Retrofit 并且 API 完全重制。在 Jake Wharton's presentation.

中阅读更多内容

compile 'com.squareup.retrofit:retrofit:2.0.0-beta1'

截至 2016 年 6 月 30 日,最新版本为 2.1.0

compile 'com.squareup.retrofit2:retrofit:2.1.0'

请检查 http://square.github.io/retrofit/ 更新。

如果您想 运行 您的代码,您需要使用旧版本的 Retrofit。

compile 'com.squareup.retrofit:retrofit:1.9.0'

但是你正在使用

compile 'com.squareup.retrofit:retrofit:2.0.0-beta1'

所以你需要像这样更改你的代码。

Retrofit retrofit = new Retrofit.Builder()
.baseUrl("https://api.github.com")
.build();`

如需完整文档,请查看 RetroFit square

在 Retrofit 2.0 中,Converter 不再包含在软件包中。您需要自己插入一个转换器,否则 Retrofit 将只能接受字符串结果。因此,Retrofit 2.0 不再依赖于 Gson。

如果你想接受 json 结果并将其解析为 DAO,你必须将 Gson 转换器作为一个单独的依赖项。

compile 'com.squareup.retrofit:converter-gson:2.0.0-beta2'

并通过addConverterFactory 插入。请注意,RestAdapter 现在也已重命名为 Retrofit。

Retrofit retrofit = new Retrofit.Builder()
        .baseUrl("http://api.nuuneoi.com/base/")
        .addConverterFactory(GsonConverterFactory.create())
        .build();

service = retrofit.create(APIService.class);

更多信息:https://inthecheesefactory.com/blog/retrofit-2.0/en