无法为 java.util.List Retrofit 2.0.0-beta2 创建转换器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34315499/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Unable to create converter for java.util.List Retrofit 2.0.0-beta2
提问by superkytoz
I'm just doing a GET request, but I'm getting this error:
我只是在执行 GET 请求,但出现此错误:
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.yomac_000.chargingpoint/com.example.yomac_000.chargingpoint.AllStores}: java.lang.IllegalArgumentException: Unable to create converter for java.util.List
java.lang.RuntimeException:无法启动活动 ComponentInfo{com.example.yomac_000.chargingpoint/com.example.yomac_000.chargingpoint.AllStores}:java.lang.IllegalArgumentException:无法为 java.util.List 创建转换器
And it's because of this line of code:
这是因为这行代码:
Call<List<Store>> call = subpriseAPI.listStores(response);
So I had tried with this line of code to see what type it is:
所以我试过用这行代码来看看它是什么类型:
System.out.println(subpriseAPI.listStores(response).getClass().toString());
But then I get the same error so it doesn't let me know what type it is. Here below you can see my code.
但是后来我得到了同样的错误,所以它没有让我知道它是什么类型。在下面你可以看到我的代码。
StoreService.java:
商店服务.java:
public class StoreService {
public static final String BASE_URL = "http://getairport.com/subprise/";
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(BASE_URL)
.build();
SubpriseAPI subpriseAPI = retrofit.create(SubpriseAPI.class);
String response = "";
public List<Store> getSubprises() {
Call<List<Store>> call = subpriseAPI.listStores(response);
try {
List<Store> listStores = call.execute().body();
System.out.println("liststore "+ listStores.iterator().next());
return listStores;
} catch (IOException e) {
// handle errors
}
return null;
}
}
SubpriseAPI.java:
SubpriseAPI.java:
public interface SubpriseAPI {
@GET("api/locations/get")
Call<List<Store>> listStores(@Path("store") String store);
}
Store.java:
商店.java:
public class Store {
String name;
}
I'm using Retrofit version 2.0.0-beta2.
我正在使用 Retrofit 版本 2.0.0-beta2。
回答by Anderson K
In the 2+ version you need to inform the Converter
在 2+ 版本中,您需要通知转换器
CONVERTERS
By default, Retrofit can only deserialize HTTP bodies into OkHttp's ResponseBody type and it can only accept its RequestBody type for @Body.
Converters can be added to support other types. Six sibling modules adapt popular serialization libraries for your convenience.
Gson: com.squareup.retrofit:converter-gson Hymanson: com.squareup.retrofit:converter-Hymanson
Moshi: com.squareup.retrofit:converter-moshi
Protobuf: com.squareup.retrofit:converter-protobuf
Wire: com.squareup.retrofit:converter-wire
Simple XML: com.squareup.retrofit:converter-simplexml
转换器
默认情况下,Retrofit 只能将 HTTP 主体反序列化为 OkHttp 的 ResponseBody 类型,并且它只能接受 @Body 的 RequestBody 类型。
可以添加转换器以支持其他类型。为了您的方便,六个兄弟模块采用流行的序列化库。
Gson: com.squareup.retrofit:converter-gson Hymanson: com.squareup.retrofit:converter-Hymanson
Moshi: com.squareup.retrofit:converter-moshi
Protobuf: com.squareup.retrofit:converter-protobuf
线: com.squareup。改造:转换器线
简单 XML:com.squareup.retrofit:converter-simplexml
// Square libs, consume Rest API
compile 'com.squareup.retrofit:retrofit:2.0.0-beta1'
compile 'com.squareup.okhttp:okhttp:2.4.0'
compile 'com.squareup.retrofit:converter-gson:2.0.0-beta1'
So,
所以,
String baseUrl = "" ;
Retrofit client = new Retrofit.Builder()
.baseUrl(baseUrl)
.addConverterFactory(GsonConverterFactory.create())
.build();
回答by Blackbelt
public interface SubpriseAPI {
@GET("api/locations/get")
Call<List<Store>> listStores(@Path("store") String store);
}
you declared a @Path
called store, so in your @GET
annotation retrofit is expecting to find the placeholder for the substitution. E.g.
您声明了一个@Path
被调用的商店,因此在您的@GET
注释改造中期望找到替换的占位符。例如
@GET("api/locations/{store}")
Call<List<Store>> listStores(@Path("store") String store);