【问题标题】:Retrofit is unable to create call adapter改造无法创建呼叫适配器
【发布时间】:2015-12-01 10:17:12
【问题描述】:

这是我的用户服务接口

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

在我正在调用的活动中

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

我做错了什么?

分级:

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'

【问题讨论】:

    标签: android retrofit rx-java retrofit2


    【解决方案1】:

    由于您已包含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,您可以订阅或用作 observable 管道的开始。

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

    【讨论】:

      【解决方案2】:

      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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2018-06-02
        • 1970-01-01
        • 2017-01-28
        • 2021-06-26
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多