Net 包的封装 emmmmm..

已下是包结构



1  新建Api   数据解救类

/**
 * Created by  on 2018/5/10.
 */

public interface Api {
    //https://www.zhaoapi.cn/product/getProducts
    public  String URL_="https://www.zhaoapi.cn/";
    //https://www.zhaoapi.cn/product/getCatagory

}

2   GetRequest  接口  两种方式

import java.util.Map;

import retrofit2.Call;
import retrofit2.http.Field;
import retrofit2.http.FieldMap;
import retrofit2.http.FormUrlEncoded;
import retrofit2.http.GET;
import retrofit2.http.POST;
import retrofit2.http.Query;

/**
 * Created by Think on 2018/5/10.
 */

public interface GetRequest_Interface {
    //https://www.zhaoapi.cn/product/getProducts
    //https://www.zhaoapi.cn/
    @GET("product/getProducts")
    Call<Bean> getDame(@Query("pscid") String pscid);



    @POST("product/getProducts")
    @FormUrlEncoded
    Call<Bean> getDDD(@FieldMap Map<String,String> map);

}

import retrofit2.Call;
import retrofit2.http.GET;
 

/**
 * Created by  on 2018/5/10.
 */

public interface GetRequest {
    @GET("ad/getAd")
    Call<Imgbean> getDame();

    @GET("product/getCatagory")
    Call<rvClassbeans> getrvClass();

}

3   拦截器

import java.io.IOException;
import okhttp3.FormBody;
import okhttp3.HttpUrl;
import okhttp3.Interceptor;
import okhttp3.Request;
import okhttp3.Response;


/**
 * Created by  on 2018/5/10.
 */

public class MyInterceptor implements Interceptor {
    @Override
    public Response intercept(Chain chain) throws IOException {
        Response response;
        //区分是GET请求还是POST请求
        Request originRequest = chain.request();
        if ("GET".equals(originRequest.method())) {
            HttpUrl httpUrl = originRequest.url()
                    .newBuilder()
                    .addQueryParameter("source", "android")
                    .build();
            Request request = new Request.Builder().url(httpUrl).build();
            //发送请求
            response = chain.proceed(request);
        } else {
            FormBody.Builder builder = new FormBody.Builder();

            FormBody body = (FormBody) originRequest.body();
            int size = body.size();
            for (int i = 0; i < size; i++) {
                builder.add(body.name(i), body.value(i));
            }
            builder.add("source", "android");
            FormBody formBody = builder.build();
            Request request = new Request.Builder()
                    .url(originRequest.url())
                    .post(formBody)
                    .build();
            response = chain.proceed(request);

        }
        return response;
    }
}

4   ok 的封装

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

/**
 * Created by  on 2018/5/10.
 */

public class OkhttpUntils {
    private volatile static OkhttpUntils util=null;
    private OkhttpUntils(){
    }
    public static OkhttpUntils getmInstance(){
        if (util==null){
            synchronized (OkhttpUntils.class){
                if (util==null){
                    util=new OkhttpUntils();
                }
            }
        }
        return util;
    }


    public GetRequest  getRequestInterface(String str){
        //okHttp拦截器
        OkHttpClient builder = new OkHttpClient.Builder()
                .addInterceptor(new MyInterceptor())//拦截器
                .build();

        //创建Retrofit实例
        retrofit2.Retrofit retrofit=new retrofit2.Retrofit.Builder()
                .baseUrl(str)//设置网路请求URL
                .client(builder)//设置okHttp拦截器
                .addConverterFactory(GsonConverterFactory.create())//设置数据解析器
                .addCallAdapterFactory(RxJavaCallAdapterFactory.create())//支持RXjava平台
                .build();

        GetRequest  anInterface = retrofit.create(GetRequest.class);

        return anInterface;
    }
}





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Jonly_W

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值