【发布时间】: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<Turbine>不是Turbine。因此,Call<List<Turbine>>不是Call<Turbine>。您的 Web 服务是从turbines/{id}请求返回涡轮机列表还是单个涡轮机? -
嗨,Commons,它实际上是一个单一的,所以我已经从所有区域中删除了它周围的
- 包装,现在它看起来不错!
-
我认为@ViktorYakunin 的回复在这里无效。
标签: java android retrofit retrofit2