【问题标题】:Adding retrofit into android project将改造添加到 android 项目中
【发布时间】:2017-11-01 18:20:00
【问题描述】:

我想尝试在一个新的 Android 项目中使用 Retrofit。

我在 build.gradle 中添加了以下内容:

compile 'com.squareup.retrofit2:retrofit:2.3.0'
compile 'com.squareup.retrofit2:converter-gson:2.3.0'
compile 'com.google.code.gson:gson:2.8.0'

我有一个名为“Turbine”的 POJO,如下所示:

public class Turbine {
    String name;
}

我有我的端点服务类:

import java.util.List;

import greenapps.objects.Turbine;
import retrofit2.Call;
import retrofit2.http.GET;
import retrofit2.http.Path;

    public interface GreenAppService {
        @GET("turbines/{id}")
        Call<List<Turbine>> turbine(@Path("id") String id);
    }

在我在 android 中的主要活动中,我有以下代码(这是我想要执行调用并取回我的 Turbine pojo 对象,其中填充了来自后端的数据:

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
...
...
...
//Relevant snippet starts here
Retrofit retrofit = new Retrofit.Builder()
            .baseUrl(Constants.API_V1_ENDPOINT)
            .build();

    GreenAppService service = retrofit.create(GreenAppService.class);
    Call<List<Turbine>> turbine = service.turbine("1");
    turbine.enqueue(new Callback<Turbine>() {
        @Override
        public void onResponse(Call<Turbine> call, Response<Turbine> response) {
            
        }

        @Override
        public void onFailure(Call<Turbine> call, Throwable t) {

        }
});//Relevant snippet ends here

turbine.enqueue 行我收到以下错误:

我认为这里的语法在某种程度上是错误的,但我不太明白是什么导致了问题。

另外,一旦这可行,我如何获得我的涡轮对象?是不是在做Turbine t = response.body();

【问题讨论】:

  • List&lt;Turbine&gt; 不是Turbine。因此,Call&lt;List&lt;Turbine&gt;&gt; 不是Call&lt;Turbine&gt;。您的 Web 服务是从 turbines/{id} 请求返回涡轮机列表还是单个涡轮机?
  • 嗨,Commons,它实际上是一个单一的,所以我已经从所有区域中删除了它周围的 包装,现在它看起来不错!
  • 我认为@ViktorYakunin 的回复在这里无效。

标签: java android retrofit retrofit2


【解决方案1】:

因为您定义了您正在等待涡轮机阵列。

turbine.enqueue(new Callback<Turbine>()

turbine.enqueue(new Callback<ArrayList<Turbine>>()

您还需要使用 arraylist 更新 onResponseonFailure 方法。

我强烈建议您为 GreenAppService 对象应用单例模式。

更新

这是一个单例模式的例子。

public final class WebService {

   private static GreenAppService sInstance;

   public static GreenAppService getInstance() {
       if (sInstance == null) {
           sInstance = new Retrofit
           .Builder()
           .baseUrl(Constants.API_V1_ENDPOINT)
           .build();
       }

       return sInstance;
   }
}

然后我们调用类似

WebService
    .getInstance()
    .yourmethod()
    .enqueue()

【讨论】:

  • 非常感谢。当您说为 GreenAppService 对象应用单例模式时,考虑到它是一个接口,我不确定如何。有这方面的指南吗?
  • 我为您的 GreenAppService 编写了一个示例,请查看。这样我们就不用在每次想使用的时候都创建GreenAppService了。
  • 修复了格式,如果你有编译错误。
【解决方案2】:

只需单击一下即可在项目中轻松添加改造。 (一次性设置)

Retrofit zip

  1. 解压文件,你会得到里面的“Retrofit”文件夹。

  2. 按照 Android Studio 路径复制此文件夹 Android\Android Studio\plugins\android\lib\templates\other

  3. 重新启动您的 Android Studio。

  4. 选择您要在其中添加改造的项目并在下面找到一个选项。

项目 > 新建 > 其他> 改造

你可以在这里看到完整的博客

https://medium.com/@mestri.vinayak.n/quick-install-retrofit-in-your-android-project-custom-template-a14a6adc77c2

【讨论】:

  • 虽然此链接可能会回答问题,但最好在此处包含答案的基本部分并提供链接以供参考。如果链接页面发生更改,仅链接答案可能会失效。 - From Review
  • 已更正@BillTür
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-10-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多