无法为 java.util.List Retrofit 2.0.0-beta2 创建转换器
Unable to create converter for java.util.List Retrofit 2.0.0-beta2
我正在执行 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
也是因为这行代码:
Call<List<Store>> call = subpriseAPI.listStores(response);
所以我尝试使用这行代码来查看它是什么类型:
System.out.println(subpriseAPI.listStores(response).getClass().toString());
但后来我得到了同样的错误所以它不让我知道它是什么类型。在下面你可以看到我的代码。
StoreService.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:
public interface SubpriseAPI {
@GET("api/locations/get")
Call<List<Store>> listStores(@Path("store") String store);
}
Store.java:
public class Store {
String name;
}
我正在使用 Retrofit 版本 2.0.0-beta2。
public interface SubpriseAPI {
@GET("api/locations/get")
Call<List<Store>> listStores(@Path("store") String store);
}
您声明了一个名为 store 的 @Path
,因此在您的 @GET
注释改造中期望找到替换的占位符。例如
@GET("api/locations/{store}")
Call<List<Store>> listStores(@Path("store") String store);
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 Jackson: com.squareup.retrofit:converter-jackson
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
// 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'
所以,
String baseUrl = "" ;
Retrofit client = new Retrofit.Builder()
.baseUrl(baseUrl)
.addConverterFactory(GsonConverterFactory.create())
.build();
我正在执行 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
也是因为这行代码:
Call<List<Store>> call = subpriseAPI.listStores(response);
所以我尝试使用这行代码来查看它是什么类型:
System.out.println(subpriseAPI.listStores(response).getClass().toString());
但后来我得到了同样的错误所以它不让我知道它是什么类型。在下面你可以看到我的代码。
StoreService.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:
public interface SubpriseAPI {
@GET("api/locations/get")
Call<List<Store>> listStores(@Path("store") String store);
}
Store.java:
public class Store {
String name;
}
我正在使用 Retrofit 版本 2.0.0-beta2。
public interface SubpriseAPI {
@GET("api/locations/get")
Call<List<Store>> listStores(@Path("store") String store);
}
您声明了一个名为 store 的 @Path
,因此在您的 @GET
注释改造中期望找到替换的占位符。例如
@GET("api/locations/{store}")
Call<List<Store>> listStores(@Path("store") String store);
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 Jackson: com.squareup.retrofit:converter-jackson
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
// 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'
所以,
String baseUrl = "" ;
Retrofit client = new Retrofit.Builder()
.baseUrl(baseUrl)
.addConverterFactory(GsonConverterFactory.create())
.build();