简体   繁体   中英

Retrofit with RXJava/RxAndroid: How does the Observable work?

I'm just starting with Retrofit and RXJava. I have a api call that uploads a file and return a http response that has an empty body and status 200 (in case everythign works fine). I translated that to retrofit as:

 @PUT("/in")
 Observable<Void> uploadFile(@Body ResponseBody file);

But when whe use mApi.uploadFile(f), I get an exception:

Caused by: java.lang.IllegalArgumentException: Could not locate call 
adapter for rx.Observable<java.lang.Void>. Tried:
    * retrofit.ExecutorCallAdapterFactory
    at retrofit.Retrofit.nextCallAdapter(Retrofit.java:207)
    at retrofit.Retrofit.callAdapter(Retrofit.java:175)
    at retrofit.MethodHandler.createCallAdapter(MethodHandler.java:45)

I read here that for an empty response without RxJava, the best practive is to use Void. That doesn't work with RXJava? Do I have to implement an adapter? .

Edit 1:

It is necessary to have a CallAdapterFactory added to retrofit as stated in the answer here . I also had to convert the File parameter. Still, the error remains.

Second step after I figure out how to map the request to retrofit is to chain muiltiple file uploads. How do I do that?

Links to advanced tutorials on using RXJava + Retrofit or samples are welcome too.

I guess your problem is you cannot pass a File as an argument, you should pass a RequestBody param as

RequestBody.create(mediaType, file);

So the signature of the retrofit call should look like:

@PUT("/in")
Observable<Void> uploadFile(RequestBody file);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM