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() {
}
});
}
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());
}
});
}
Aggregations