2015-12-16 18 views
7

Tôi chỉ làm một yêu cầu GET, nhưng tôi nhận được lỗi này:Không thể tạo chuyển đổi cho java.util.List Retrofit 2.0.0-beta2

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> 

Và đó là vì dòng này của mã:

Call<List<Store>> call = subpriseAPI.listStores(response); 

vì vậy, tôi đã cố gắng với dòng mã này để xem những gì nó là loại:

System.out.println(subpriseAPI.listStores(response).getClass().toString()); 

Nhưng sau đó tôi nhận được lỗi tương tự như vậy nó không le tôi biết đó là loại gì Dưới đây bạn có thể thấy mã của tôi.

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

Tôi đang sử dụng phiên bản Retrofit 2.0.0-beta2.

+0

Tôi cũng có vấn đề này và tôi đã hỏi các nhà phát triển Retrofit để làm cho lỗi có ý nghĩa hơn ở đây https://github.com/square/retrofit/issues/1774. –

Trả lời

18

Trong phiên bản 2+ bạn cần phải thông báo cho Chuyển đổi

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' 

Vì vậy,

String baseUrl = "" ; 
Retrofit client = new Retrofit.Builder() 
.baseUrl(baseUrl) 
.addConverterFactory(GsonConverterFactory.create()) 
.build(); 
+0

Điều này làm việc cho tôi 'biên dịch 'com.squareup.retrofit: converter-gson: 2.0.0-beta2'' – superkytoz

+0

cũng bạn có thể thêm nó bằng cách biên dịch này' com.squareup.retrofit2: converter-gson: 2.0.0 ' –

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

bạn tuyên bố một cửa hàng @Path gọi, vì vậy trong @GET chú thích retrofit bạn đang mong đợi để tìm placeholder sử dụng quyền thay người. Ví dụ.

@GET("api/locations/{store}") 
Call<List<Store>> listStores(@Path("store") String store); 
Các vấn đề liên quan