retrofit +rxjava简单封装 日志拦截器 应用拦截器

compile 'com.squareup.retrofit2:retrofit:2.0.0-beta4'
compile 'com.squareup.retrofit2:converter-gson:2.0.0-beta4'
compile 'com.squareup.retrofit2:adapter-rxjava:2.0.0-beta4'
compile 'com.google.code.gson:gson:2.6.2'

compile 'io.reactivex:rxandroid:1.2.1'

compile 'com.squareup.okhttp3:logging-interceptor:3.4.1'
compile 'com.squareup.okhttp3:okhttp:3.4.1'

////////////////////////////////////////////////////////
ApiService

import rx.Observable;
 
public interface ApiService {
////https://www.zhaoapi.cn/quarter/getUserVideos?source=android&appVersion=101&uid=5&page=6
    @GET("user/login")
    Observable<Bean>  getTop(@QueryMap Map<String,String> map);
}
////////////////////////////////////////////////////////////////////
RetrofitUtils
public class RetrofitUtils {
    private static volatile  RetrofitUtils  instance;
    public static  RetrofitUtils  getInstance(){
        if(instance==null){
            synchronized (RetrofitUtils.class){
                if(instance==null){
                    instance=new RetrofitUtils();
                }
            }
        }
        return  instance;
    }


   

    public Retrofit getNews(){
   String baseUrl= "https://www.zhaoapi.cn/";

   HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
   interceptor.setLevel(HttpLoggingInterceptor.Level.BASIC);
   OkHttpClient okHttpClient=new OkHttpClient.Builder()
        //  .addInterceptor(new CommonParamsinterceptor() )
        .addInterceptor(interceptor)
        .build();

Retrofit retrofit=new Retrofit.Builder()

.baseUrl(baseUrl)

.client(okHttpClient)

.addConverterFactory(GsonConverterFactory.create())

.addCallAdapterFactory(RxJavaCallAdapterFactory.create())

.build();

return retrofit;

}

}

///////////////////////////////////////////////////////////

//CommonParamsinterceptor
public class CommonParamsinterceptor  implements Interceptor {
    @Override
    public Response intercept(Chain chain) throws IOException {

        //得到原始的请求对象
        Request request=chain.request();

        //得到请求方式
        String method=request.method();

        if("GET".equals(method)){
            //去除原始的URL
            String oldurl=request.url().toString();

            //拼接公共参数
           // String newurl=oldurl+"source=android&appVersion=101";

            String[] f=oldurl.split("\\?");

            String s=f[0]+"?source=android&appVersion=101&"+f[1];

            Log.d("fff",s.toString());
            //构建新的request
            request=new Request.Builder()
                    .url(s)
                    .build();

        }else if("POST".equals(method)){
            //取出旧的参数和URL
            FormBody body=(FormBody)request.body();

            String oldUrl=request.url().toString();

            //构建新的FromBody
            FormBody.Builder newFormBody = new FormBody.Builder();

            for (int i = 0; i < body.size();i++){

                String key = body.name(i);//keywors
                String value = body.value(i);//value

                newFormBody.add(key,value);
            }
            //公共参数
            newFormBody.add("source","android");
            newFormBody.add("appVersion","101");

            //新构建的reqeust
            request = new Request.Builder()
                    .url(oldUrl)
                    .post(newFormBody.build())
                    .build();
        }
        return chain.proceed(request);
    }
}

//////////////////////////////////////////////////////////////////////////////

Map<String,String> map=new HashMap<>();
map.put("mobile","13269671778");
map.put("password","1234567");

retrofitUtils = RetrofitUtils.getInstance();

retrofitUtils.getNews()
        .create(ApiService.class)
        .getTop(map)
        .subscribeOn(Schedulers.io())
        .observeOn(AndroidSchedulers.mainThread())
        .subscribe(new Subscriber<Bean>() {
            @Override
            public void onCompleted() {
            }
            @Override
            public void onError(Throwable e) {
                Log.d("mmm",e.getMessage());
            }
            @Override
            public void onNext(Bean bean) {
                Log.d("xxx",bean.getMsg().toString());
            }
        });




阅读更多
上一篇GreenDao实现搜索记录
下一篇RecyclerView 多条目加载 和点击事件
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭
关闭