RxJava 2 Retrofit和动态URL以及@url注释

时间:2017-01-29 10:57:22

标签: android retrofit rx-java

我正在尝试为multiplecalls构建一个接口。即http://server.com/firstQuery http://server.com/secondQuery

这是我改装的rx界面

public interface RequestInterfaceRx<T> {
    @GET("/")
    Observable<CurrencyOnServerConstruct> register();
    Call<ResponseBody> setUrl(@Url String url);
}

这是我使用的代码

private void setupQuery(){
    RequestInterfaceRx requestInterface = new Retrofit.Builder()
            .baseUrl("http://server.com/v3/")
            .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
            .addConverterFactory(GsonConverterFactory.create())
            .build().create(RequestInterfaceRx.class);
    requestInterface.setUrl("m");
    requestInterface.register()
            .observeOn(AndroidSchedulers.mainThread())
            .subscribeOn(Schedulers.io())
            .subscribe(this::handleResponse,this::handleError);
}

它没有按预期正常工作:)

        Caused by: java.lang.IllegalArgumentException: HTTP method annotation is required (e.g., @GET, @POST, etc.).
                                                   for method RequestInterfaceRx.setUrl
                                                   at retrofit2.ServiceMethod$Builder.methodError(ServiceMethod.java:720)
                                                   at retrofit2.ServiceMethod$Builder.methodError(ServiceMethod.java:711)
                                                   at retrofit2.ServiceMethod$Builder.build(ServiceMethod.java:174)
                                                   at retrofit2.Retrofit.loadServiceMethod(Retrofit.java:166)
                                                   at retrofit2.Retrofit$1.invoke(Retrofit.java:145)
                                                   at java.lang.reflect.Proxy.invoke(Proxy.java:393)
                                                   at $Proxy0.setUrl(Unknown Source)
                                                   at bytesand.myapplicationaqueryserverrx.MainActivity.setupQuery(MainActivity.java:29)
                                                   at bytesand.myapplicationaqueryserverrx.MainActivity.onCreate(MainActivity.java:21)
                                                   at android.app.Activity.performCreate(Activity.java:6237)
                                                   at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
                                                   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
                                                    ... 9 more

1 个答案:

答案 0 :(得分:2)

@GET Observable<CurrencyOnServerConstruct> executeFullUrlCall(@Url String url); 

这就是你做的事情

C完整文章https://medium.com/@sasa_sekulic/quick-and-easy-guide-to-retrofit-2-0-setup-or-migration-with-rxjava-ab7a11bc17df#.s5ifc5kov