Retrofit 无法创建调用适配器

Retrofit is unable to create call adapter

这是我的用户服务界面

 @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);

在activity我打电话

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

这会造成异常

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

我做错了什么?

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'

由于您已经包含 addCallAdapterFactory(RxJavaCallAdapterFactory.create()),您希望使用 Observable 来管理您的通话。在您的界面中,明确给出参数化的 Observable 而不是 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);

然后您的 service 方法为您创建可观察对象,您可以订阅或用作可观察管道的开始。

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

Aleksei,如果您需要最简单的解决方案来从 Retrofit 库中获取字符串结果,那么您必须多次调用:

  1. 起初,Gradle 亲属:

    compile 'com.squareup.retrofit2:retrofit:2.0.0-beta4'
    compile 'com.squareup.retrofit2:converter-scalars:2.0.0-beta4'
    
  2. 您修改后的 UserService 接口

    @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. 服务客户端创建代码:

    static UserService SERVICE_INSTANCE = (new Retrofit.Builder()
            .baseUrl(Constants.Api.URL_BASE)
            .addConverterFactory(ScalarsConverterFactory.create())
            .build()).create(UserService.class);    
    
  4. 调用请求:

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

我希望,解决方案是明确的,并展示了简单的字符串接收方法。如果您需要其他数据解析器,请查看此处的转换器列表 Retrofit CONVERTERS