Retrofit+XRjava+okhttp封装

package com.example.lenovo.moniyuekao.utils;

import java.util.concurrent.TimeUnit;

import okhttp3.OkHttpClient;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;


/**
 * date:2018/6/28 14:48

 * function:
 */
public class NetUtils {

    private final Retrofit retrofit;
    private final OkHttpClient client;
    private final MySession mySession;

    private NetUtils(String url){
        client = new OkHttpClient.Builder().addInterceptor(new HttpLoggingInterceptor().setLevel(HttpLoggingInterceptor.Level.BODY)).writeTimeout(1000, TimeUnit.SECONDS).readTimeout(1000,TimeUnit.SECONDS).connectTimeout(1000,TimeUnit.SECONDS).build();
        retrofit = new Retrofit.Builder().baseUrl(url).client(client).addCallAdapterFactory(RxJavaCallAdapterFactory.create()).addConverterFactory(GsonConverterFactory.create()).build();
        mySession = retrofit.create(MySession.class);
    }

    private static NetUtils netUtils;

        public static NetUtils getNetUtils(String url){
            if (netUtils==null){
                synchronized (NetUtils.class){
                    if (netUtils==null){
                        netUtils=new NetUtils(url);
                    }
                }
            }
            return netUtils;
        }

public MySession mySessionApi(){
            return mySession;
}

}
package com.example.lenovo.moniyuekao.utils;


import com.example.lenovo.moniyuekao.bean.BeanCar;
import com.example.lenovo.moniyuekao.bean.BeanCarUpData;
import com.example.lenovo.moniyuekao.bean.BeanRightType;
import com.example.lenovo.moniyuekao.bean.BeanType;

import retrofit2.http.GET;
import retrofit2.http.Query;
import rx.Observable;


/**
 * date:2018/6/28 14:57

 * function:
 */
public interface MySession {
@GET("product/getCatagory")
Observable getDataTypeLeft();

@GET("product/getProductCatagory")
    Observable getDataTypeRight(@Query("cid") String cid);
    @GET("product/getCarts")
    Observable getDataCar(@Query("uid") String uid);
//https://www.zhaoapi.cn/product/updateCarts?uid=71&sellerid=1&pid=1&selected=0&num=10
    @GET("product/updateCarts")
    ObservablegetUpCar(@Query("uid") String uid,@Query("sellerid") String sellerid,@Query("pid") String pid,@Query("selected")String selected,@Query("num")String num);
}
 
  
    implementation 'io.reactivex:rxjava:1.1.0'
    implementation 'io.reactivex:rxandroid:1.1.0'
    implementation 'com.squareup.retrofit2:retrofit:2.0.0-beta4'
    implementation 'com.squareup.retrofit2:converter-gson:2.0.0-beta4'
    implementation 'com.squareup.retrofit2:adapter-rxjava:2.0.0-beta4'
    implementation 'com.squareup.okhttp3:okhttp:3.5.0'
    implementation 'com.squareup.okhttp3:logging-interceptor:3.6.0'
    Observable upCar = api.getUpCar("15768",  beanCarData.get(i).getList().get(j).getSellerid() + "", beanCarData.get(i).getList().get(j).getPid() + "", "0", beanCarData.get(i).getList().get(j).getNum() + "");
                                    upCar.subscribeOn(Schedulers.io())
                                            .observeOn(AndroidSchedulers.mainThread())
                                            .subscribe(new Action1() {
                                                @Override
                                                public void call(BeanCarUpData beanCarUpData) {

                                                }
                                            });



你可能感兴趣的:(Retrofit+XRjava+okhttp封装)