compile 'io.reactivex.rxjava2:rxjava:2.0.1'
compile 'io.reactivex.rxjava2:rxandroid:2.0.1'
compile 'com.squareup.retrofit2:retrofit:2.3.0'
compile 'com.squareup.retrofit2:converter-gson:2.3.0'
compile 'com.squareup.retrofit2:adapter-rxjava2:2.3.0'
//ok拦截日志
compile 'com.squareup.okhttp3:logging-interceptor:3.5.0'
//自定义接口 存放uri串
public interface Getnet { //retrofit要求有一个根地址,此处我们用主机域名作为根地址,/严格要求必须有 //http://www.yulin520.com/a2a/impressApi/news/mergeList?pageSize=10 你要解析的uri串 public static String net="http://www.yulin520.com/a2a/"; }
//接口为 网络请求的方式
public interface TestService { //User 为uri串 转换的bean对象 //impressApi/news/mergeList?pageSize=10&page=10为 @GET("impressApi/news/mergeList?pageSize=10&page=10")
Observable<User> getUser();
@GET(
"impressApi/news/mergeList?pageSize=10")
Observable<User> getUsermap(@QueryMap Map<String,String> map);@POST( "impressApi/news/mergeList?pageSize=10") @FormUrlEncoded
Observable<User> postUser(@Field("page") String id);@POST( "impressApi/news/mergeList?pageSize=10") @FormUrlEncoded
Observable<User> postUsermap(@FieldMap Map<String,String> map);
@GET
Observable<BannerBean> postRetrofit(@Url String url, @QueryMap() Map<String, String> map);
//购物车 @POST("product/getCarts?source=android") @FormUrlEncoded Call<Gouwuche> getUser(@FieldMap Map<String,String> map);//解析代码
Call<Gouwuche> user = Util.getmInstance().getnetjson(Getnet.shouye).getUser(map); user.enqueue(new Callback<Gouwuche>() { @Override public void onResponse(Call<Gouwuche> call, Response<Gouwuche> response) { Gouwuche body = response.body(); if (body!=null) { getgouwuchebean.getgouwuchejson(body); } } @Override public void onFailure(Call<Gouwuche> call, Throwable t) { } });
}
//工具包 解析json串的时候用
public class Util { private volatile static Util util=null; private Util(){ } public static Util getmInstance(){ if (util==null){ synchronized (Util.class){ if (util==null){ util=new Util(); } } } return util; }
OkHttpClient okHttpClient = new OkHttpClient.Builder().
addInterceptor(new MyInter()).build();
//ok拦截日志 OkHttpClient httpClient = new OkHttpClient.Builder(). addNetworkInterceptor(new HttpLoggingInterceptor().setLevel(HttpLoggingInterceptor.Level.BODY)) .build();public Testservice getnetjson(String uri){ Retrofit retrofit = newRetrofit.Builder().baseUrl(uri)
.addConverterFactory(GsonConverterFactory.create())
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.client(okHttpClient )//添加拦截器时用
.build();
Testservive testservive = retrofit.create(Testservive.class); return testservive;
//拦截器 class MyInter implements Interceptor { @Override public Response intercept(Chain chain) throws IOException { //获取原来的body Request request = chain.request(); RequestBody body = request.body(); if (body instanceof FormBody) { //遍历原来的所有参数,加到新的Body里面,最后将公共参数加到新的Body FormBody.Builder newBuilder = new FormBody.Builder(); for (int i = 0; i < ((FormBody) body).size(); i++) { String name = ((FormBody) body).name(i); String value = ((FormBody) body).value(i); //放入新的 newBuilder.add(name, value); } //在将公共参数添加进去 newBuilder.add("page", "20"); FormBody newBody = newBuilder.build(); //创建新的请求 Request newRequest = request.newBuilder().post(newBody).build(); Response response = chain.proceed(newRequest); return response; } return null; }}
//解析json串代码
Testservive testservive = Util.getnetjson(Getnet.net); Observable<User> user1 = testservive.getUser(map);
user1.subscribeOn(Schedulers.newThread()) .observeOn(AndroidSchedulers.mainThread()) .subscribe(new Consumer<User>() { @Override public void accept(User user) throws Exception { List<User.DataBean> data = user.getData(); } });
//ok拦截器
public class LoggingInterceptor implements Interceptor { @Override public Response intercept(Interceptor.Chain chain) throws IOException { //首先取到Request Request request = chain.request(); Response response = null; Request requestProcess = null ; if("GET".equals(request.method())){ String url = request.url().toString() + "?source=android&appVersion=101"; Request.Builder builder = request.newBuilder() ; builder.get().url(url); requestProcess = builder.build(); response = chain.proceed(requestProcess); } else { FormBody.Builder builder = new FormBody.Builder() ; RequestBody requestBody = request.body() ; if(requestBody instanceof FormBody){ FormBody formBody = (FormBody)requestBody ; for (int i=0;i<formBody.size();i++){ builder.add(formBody.encodedName(i),formBody.encodedValue(i)); } builder.add("source","android") .add("appVersion","101"); } requestProcess = request.newBuilder().url(request.url().toString()).post(builder.build()).build() ; response = chain.proceed(requestProcess); } return response; } }