Search in sources :

Example 1 with GankAndroid

use of home.smart.fly.http.model.GankAndroid in project My-MVP by REBOOTERS.

the class RxJavaBaseActivity method withRetrofit2AndGson.

private void withRetrofit2AndGson() {
    final OkHttpClient mClient = new OkHttpClient.Builder().readTimeout(10, TimeUnit.SECONDS).connectTimeout(30, TimeUnit.SECONDS).addInterceptor(new HttpLoggingInterceptor().setLevel(HttpLoggingInterceptor.Level.BODY)).build();
    final Retrofit mRetrofit = new Retrofit.Builder().client(mClient).baseUrl(BASE_URL).addConverterFactory(GsonConverterFactory.create()).addCallAdapterFactory(RxJava2CallAdapterFactory.create()).build();
    GankApi mGankApi = mRetrofit.create(GankApi.class);
    Observable<GankAndroid> mAndroidObservable = mGankApi.getData("10/1");
    mAndroidObservable.subscribeOn(Schedulers.io()).map(new Function<GankAndroid, GankAndroid.ResultsEntity>() {

        @Override
        public GankAndroid.ResultsEntity apply(GankAndroid gankAndroid) throws Exception {
            return gankAndroid.getResults().get(0);
        }
    }).observeOn(AndroidSchedulers.mainThread()).subscribe(new Consumer<GankAndroid.ResultsEntity>() {

        @Override
        public void accept(GankAndroid.ResultsEntity resultsEntity) throws Exception {
            sb.append(resultsEntity.getCreatedAt()).append("\n").append(resultsEntity.getType()).append("\n").append(resultsEntity.getDesc()).append("\n").append(resultsEntity.getUrl()).append("\n").append(resultsEntity.getWho());
            logContent.setText(sb.toString());
        }
    });
}
Also used : OkHttpClient(okhttp3.OkHttpClient) GankApi(home.smart.fly.http.model.GankApi) Retrofit(retrofit2.Retrofit) HttpLoggingInterceptor(okhttp3.logging.HttpLoggingInterceptor) GankAndroid(home.smart.fly.http.model.GankAndroid)

Aggregations

GankAndroid (home.smart.fly.http.model.GankAndroid)1 GankApi (home.smart.fly.http.model.GankApi)1 OkHttpClient (okhttp3.OkHttpClient)1 HttpLoggingInterceptor (okhttp3.logging.HttpLoggingInterceptor)1 Retrofit (retrofit2.Retrofit)1