2015-12-16 29 views
7

我只是在做一個GET請求,但我得到這個錯誤:無法爲java.util.List的改造2.0.0-β2創建轉換

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<model.Store> 

而且是因爲這條線的代碼:

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; 
} 

我使用的是改造版本2.0.0-β2。

+0

我也有這個問題,我問了改造開發商做出錯誤這裏更有意義https://github.com/square/retrofit/issues/1774。 –

回答

18

在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 Lib, 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(); 
+0

這一個爲我編譯com.squareup.retrofit:converter-gson:2.0.0-beta2'' – superkytoz

+0

也可以通過這個編譯添加它'com.squareup.retrofit2:converter-gson:2.0.0' –

3
public interface SubpriseAPI { 
    @GET("api/locations/get") 
    Call<List<Store>> listStores(@Path("store") String store); 
} 

你宣佈@Path叫商店,所以在您的@GET註釋改造期待找到替換的佔位符。例如。

@GET("api/locations/{store}") 
Call<List<Store>> listStores(@Path("store") String store); 
相關問題