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<BeanType> getDataTypeLeft();

@GET("product/getProductCatagory")
    Observable<BeanRightType> getDataTypeRight(@Query("cid") String cid);
    @GET("product/getCarts")
    Observable<BeanCar> getDataCar(@Query("uid") String uid);
//https://www.zhaoapi.cn/product/updateCarts?uid=71&sellerid=1&pid=1&selected=0&num=10
    @GET("product/updateCarts")
    Observable<BeanCarUpData>getUpCar(@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<BeanCarUpData> 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<BeanCarUpData>() {
                                                @Override
                                                public void call(BeanCarUpData beanCarUpData) {

                                                }
                                            });



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值