Caused by: java.lang.IllegalArgumentException: Could not locate call adapter for rx.Observable错误

public class TRService {
    public static final String BASE_TRC_URL = "http://www.tuling123.com/openapi/";

    private static Retrofit retrofit = new Retrofit.Builder()
            .baseUrl(BASE_TRC_URL)
            .addConverterFactory(GsonConverterFactory.create())
            .build();

    private TRService() {
    }

    public static TRApi createTRService() {
        return retrofit.create(TRApi.class);
    }
}


public interface TRApi {

    /**
     * 可以看到这里应该是post传递的参数。
     * @param key
     * @param info
     * @param userid
     * @return
     */
    @FormUrlEncoded
    @POST("api")
    Observable getTRResponse(@Field("key") String key, @Field("info") String info, @Field("userid") String userid);
}


调用的地方:

private void gainChat(String str) {
        service.getTRResponse(Contant.TRC_KEY, str, Contant.TRC_USER_ID)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Action1() {
                    @Override
                    public void call(TREntity trEntity) {
                        if (trEntity != null) {
                            String str;
                            if (trEntity.getCode() == 40004) {
                                str = Contant.TRC_ROBOT_REST;
                            } else {
                                str = trEntity.getText();
                            }
                            addData(new ChatBean(TRClientAdapter.TYPE_ROBOT, str));
                        }
                    }
                }, new Action1() {
                    @Override
                    public void call(Throwable throwable) {
                        addData(new ChatBean(TRClientAdapter.TYPE_ROBOT, Contant.TRC_ROBOT_FAILED));
                    }
                });
    }


出现如下错误:

Caused by: java.lang.IllegalArgumentException: Could not locate call adapter for rx.Observable<>

要在构建Retrofit时指定适配器模式为RxJavaCallAdapterFactory,修改如下:

public class TRService {
    public static final String BASE_TRC_URL = "http://www.tuling123.com/openapi/";

    private static Retrofit retrofit = new Retrofit.Builder()
            .baseUrl(BASE_TRC_URL)
            .addConverterFactory(GsonConverterFactory.create())
            .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
            .build();

    private TRService() {
    }

    public static TRApi createTRService() {
        return retrofit.create(TRApi.class);
    }
}



如果没有使用RxJavaCallAdapterFractory,那么使用Retrofit的回调,如下:

public interface TRApi {

    /**
     * 可以看到这里应该是post传递的参数。
     * @param key
     * @param info
     * @param userid
     * @return
     */
    @FormUrlEncoded
    @POST("api")
    Call getTRResponse(@Field("key") String key, @Field("info") String info, @Field("userid") String userid);
}


你可能感兴趣的:(Caused by: java.lang.IllegalArgumentException: Could not locate call adapter for rx.Observable错误)