Search in sources :

Example 1 with GankApi

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

the class RxJavaBaseActivity method withRetrofit2.

private void withRetrofit2() {
    OkHttpClient.Builder mBuilder = new OkHttpClient.Builder();
    mBuilder.readTimeout(10, TimeUnit.SECONDS);
    mBuilder.connectTimeout(30, TimeUnit.SECONDS);
    HttpLoggingInterceptor mLoggingInterceptor = new HttpLoggingInterceptor();
    mLoggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
    mBuilder.addInterceptor(mLoggingInterceptor);
    OkHttpClient mClient = mBuilder.build();
    Retrofit.Builder builder = new Retrofit.Builder();
    builder.baseUrl(BASE_URL);
    builder.client(mClient);
    Retrofit mRetrofit = builder.build();
    final GankApi mGankApi = mRetrofit.create(GankApi.class);
    final Call<ResponseBody> mCall = mGankApi.getJson("10/1");
    Observable.create(new ObservableOnSubscribe<ResponseBody>() {

        @Override
        public void subscribe(ObservableEmitter<ResponseBody> e) throws Exception {
            e.onNext(mCall.execute().body());
        }
    }).map(new Function<ResponseBody, String>() {

        @Override
        public String apply(ResponseBody responseBody) throws Exception {
            return responseBody.string();
        }
    }).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(new Observer<String>() {

        @Override
        public void onSubscribe(Disposable d) {
            Log.e(TAG, "onSubscribe: d=" + d);
        }

        @Override
        public void onNext(String s) {
            logContent.setText(s);
        }

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

        @Override
        public void onComplete() {
        }
    });
}
Also used : Disposable(io.reactivex.disposables.Disposable) OkHttpClient(okhttp3.OkHttpClient) GankApi(home.smart.fly.http.model.GankApi) ResponseBody(okhttp3.ResponseBody) Retrofit(retrofit2.Retrofit) Function(io.reactivex.functions.Function) HttpLoggingInterceptor(okhttp3.logging.HttpLoggingInterceptor)

Example 2 with GankApi

use of home.smart.fly.http.model.GankApi 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

GankApi (home.smart.fly.http.model.GankApi)2 OkHttpClient (okhttp3.OkHttpClient)2 HttpLoggingInterceptor (okhttp3.logging.HttpLoggingInterceptor)2 Retrofit (retrofit2.Retrofit)2 GankAndroid (home.smart.fly.http.model.GankAndroid)1 Disposable (io.reactivex.disposables.Disposable)1 Function (io.reactivex.functions.Function)1 ResponseBody (okhttp3.ResponseBody)1