【问题标题】:RestAdapter (retrofit) not resolving in androidRestAdapter(改造)无法在android中解决
【发布时间】:2015-12-02 03:42:26
【问题描述】:

所以我正在尝试将 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 进行一些调用。但是重新适配器只是没有得到resolved。将鼠标悬停在上面时,它只会显示symbol can't be resolved。这里发生了什么?

【问题讨论】:

    标签: android retrofit


    【解决方案1】:

    你有两个选择:

    1) 使用稳定的 Retrofit 1

    这有你需要的RestAdapter 类。

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

    2) 迁移到改造 2

    RestAdapter 类已重命名为 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/ 以获取更新。

    【讨论】:

      【解决方案2】:

      第 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

      【讨论】:

        【解决方案3】:

        如果你想运行你的代码,你需要使用旧版本的 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

        【讨论】:

          【解决方案4】:

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

          如果你想接受 json 结果并将其解析为 DAO,则必须调用 Gson Converter 作为单独的依赖项。

          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

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2018-11-07
            • 2015-11-17
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多