Home » Android » android – Retrofit is unable to create call adapter

android – Retrofit is unable to create call adapter

Posted by: admin June 15, 2020 Leave a comment

Questions:

This is my UserService Interface

 @GET(Constants.Api.URL_LOGIN)
    String loginUser(@Field("email") String email, @Field("password") String pass, @Field("secret") String secret, @Field("device_id") String deviceid, @Field("pub_key") String pubkey, @Field("device_name") String devicename);

In the activity I am calling

retrofit = new Retrofit.Builder()
                .baseUrl(Constants.Api.URL_BASE)
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                .build();
service = retrofit.create(UserService.class);
String status = service.loginUser(loginedt.getText().toString(), passwordedt.getText().toString(), secret, device_id, pub_key, device_name);

This creates an exception

java.lang.IllegalArgumentException: Unable to create call adapter for class java.lang.String
    for method UserService.loginUser

What am I doing wrong?

Gradle :

compile 'com.squareup.retrofit:retrofit:2.+'
compile 'com.squareup.retrofit:adapter-rxjava:2.0.0-beta1'
compile 'com.squareup.retrofit:converter-gson:2.0.0-beta1'
How to&Answers:

Since you have included addCallAdapterFactory(RxJavaCallAdapterFactory.create()), you are looking to use Observable‘s to manage your calls. In your interface, explicitly give the parameterized Observable instead of a Call

@GET(Constants.Api.URL_LOGIN)
    Observable<String> loginUser(@Field("email") String email, @Field("password") String pass, @Field("secret") String secret, @Field("device_id") String deviceid, @Field("pub_key") String pubkey, @Field("device_name") String devicename);

and then your service methods create observables for you that you can subscribe to or use as the start of an observable pipeline.

Observable<String> status = service.loginUser(loginedt.getText().toString(), passwordedt.getText().toString(), secret, device_id, pub_key, device_name);
status.subscribe(/* onNext, onError, onComplete handlers */);

Answer:

Aleksei, if you need the most simple solution to get String result from Retrofit library, than you have to do this several calls:

  1. At first, Gradle dependecies:

    compile 'com.squareup.retrofit2:retrofit:2.0.0-beta4'
    compile 'com.squareup.retrofit2:converter-scalars:2.0.0-beta4'
    
  2. Your modified UserService Interface

    @GET(Constants.Api.URL_LOGIN)
    Call< String> loginUser(@Field("email") String email, @Field("password") String pass, @Field("secret") String secret, @Field("device_id") String deviceid, @Field("pub_key") String pubkey, @Field("device_name") String devicename);
    
  3. Service client creation code:

    static UserService SERVICE_INSTANCE = (new Retrofit.Builder()
            .baseUrl(Constants.Api.URL_BASE)
            .addConverterFactory(ScalarsConverterFactory.create())
            .build()).create(UserService.class);    
    
  4. Calling the request:

    SERVICE_INSTANCE.loginUser(*all your params*).execute().body();
    

I hope, the solution is clear and shows simple String receive approach. If you need some another data parser, please, take a look at the conversters list here Retrofit CONVERTERS.