package com.bawie.gouwuche.model.http;
import android.util.Log;
import com.bawie.gouwuche.IApi;
import com.bawie.gouwuche.model.bean.CartBean;
import java.util.concurrent.TimeUnit;
import io.reactivex.Observable;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;
import okhttp3.OkHttpClient;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;
/**
-
作者: 姓名
-
日期: 2019/9/6 19:23
*/
public class OkHttpUtils {
private static OkHttpUtils okHttpUtils=new OkHttpUtils();
private static final String BASE_URL = “http://172.17.8.100/”;
private final OkHttpClient okHttpClient;
private final IApi iApi;
public static OkHttpUtils getInstance() {return okHttpUtils;
}
private OkHttpUtils(){
HttpLoggingInterceptor httpLoggingInterceptor = new HttpLoggingInterceptor();
httpLoggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
okHttpClient = new OkHttpClient.Builder()
.writeTimeout(5, TimeUnit.SECONDS)
.readTimeout(5, TimeUnit.SECONDS)
.connectTimeout(5, TimeUnit.SECONDS)
.addInterceptor(httpLoggingInterceptor)
.build();Retrofit retrofit = new Retrofit.Builder() .baseUrl(BASE_URL) .client(okHttpClient) .addConverterFactory(GsonConverterFactory.create()) .addCallAdapterFactory(RxJava2CallAdapterFactory.create()) .build(); iApi = retrofit.create(IApi.class);
}
public void getBjp(final CartCallBack cartCallBack) {
Observable cart = okHttpUtils.iApi.getCart(7803, “15678207039317803”);
cart.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Observer() {
@Override
public void onSubscribe(Disposable d) {} @Override public void onNext(CartBean cartBean) { cartCallBack.ok(cartBean); } @Override public void one rror(Throwable e) { } @Override public void onComplete() { } });
}
public interface CartCallBack{
void ok(CartBean cartBean);
}
}