Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to create converter for java.util.List Retrofit 2.0.0-beta2

I'm just doing a GET request, but I'm getting this error:

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

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:

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

I'm using Retrofit version 2.0.0-beta2.

like image 664
superkytoz Avatar asked Dec 16 '15 15:12

superkytoz


2 Answers

In the 2+ version you need to inform the Converter

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'

So,

String baseUrl = "" ;
Retrofit client = new Retrofit.Builder()
    .baseUrl(baseUrl)
    .addConverterFactory(GsonConverterFactory.create())
    .build();
like image 176
Anderson K Avatar answered Oct 22 '22 11:10

Anderson K


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.

@GET("api/locations/{store}")
Call<List<Store>> listStores(@Path("store") String store);
like image 4
Blackbelt Avatar answered Oct 22 '22 12:10

Blackbelt