10
votes

I am trying to read the httpstatus code e body in the success case of a request. So I created the code below to test but I failed to get the onNext called, I tried to use the okhttp (com.squareup.okhttp.Response) and retrofit Response (retrofit.Response) class, but I couldn't make it work.

Can someone help me to read the body and httpstatus code here? I would like to keep using the Observables. Thanks in advance.

package com.app.rest;

import com.squareup.okhttp.Response;

import retrofit.GsonConverterFactory;
import retrofit.Retrofit;
import retrofit.RxJavaCallAdapterFactory;
import retrofit.http.GET;
import rx.Observable;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

public class TestApiClient {
    public interface Test {
        @GET("/posts")
        Observable<Response> getPosts();
    }

    public TestApiClient() {
        new Retrofit.Builder()
                .baseUrl("http://jsonplaceholder.typicode.com")
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                .build().create(Test.class).getPosts().subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread()).subscribe(new Subscriber<Response>() {
            @Override
            public void onCompleted() {

            }

            @Override
            public void onError(Throwable e) {
                e.toString();
            }

            @Override
            public void onNext(Response response) {
                response.toString();
            }
        });
    }
}
4

4 Answers

6
votes

I got the answer.

import com.squareup.okhttp.ResponseBody;
import retrofit.Response;

...

public Observable<Response<ResponseBody>> xxx(){}
...

Playlist playlist = Playlist.parse(((ResponseBody)response.body()).byteStream());

Actually, response.body() is a Object, You can cast it to another type.In this case, it is ResponseBody.

4
votes

I got it working with the code below, it was to needed to use the generics of retrofit.Response with ResponseBody:

package com.app.rest;


import com.squareup.okhttp.ResponseBody;

import retrofit.GsonConverterFactory;
import retrofit.Response;
import retrofit.Retrofit;
import retrofit.RxJavaCallAdapterFactory;
import retrofit.http.GET;
import rx.Observable;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

public class TestApiClient {
    public interface Test {
        @GET("/posts")
        Observable<Response<ResponseBody>> getPosts();
    }

    public TestApiClient() {
        new Retrofit.Builder()
                .baseUrl("http://jsonplaceholder.typicode.com")
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                .build().create(Test.class).getPosts().subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread()).subscribe(new Subscriber<Response<ResponseBody>>() {
            @Override
            public void onCompleted() {

            }

            @Override
            public void onError(Throwable e) {
                e.toString();
            }

            @Override
            public void onNext(Response<ResponseBody> response) {
                response.toString();
            }
        });
    }
}
2
votes

You need to define your interface's Observable as

@GET("/posts") Observable<Result<MyPost>>

Then you can easily get the response body in onNext() like this:

@Override
public void onNext(Result<MyPost> result) {
    result.response().body();
}
1
votes

Sometimes the response.toString() or response.body() gives you unreadable string, I had to get the bytes from the response body and construct a new string from it.

@Override
public void onNext(Response<ResponseBody> response) {
    try {
        String responseStr = new String(response.body().bytes());
        Log.d("responseBodyResponse", responseStr);
    } catch (Exception e) {
        Log.d("responseBodyResponse Exception", e.getMessage());
    }

}