Retrofit+Rxjava+ok+拦截器的封装
来源:互联网 发布:unity3d ongui常用函数 编辑:程序博客网 时间:2024/05/16 12:37
Retrofit+Rxjava+ok+拦截器+RecyclerView+XRecyclerView
//依赖
compile 'com.squareup.retrofit2:retrofit:2.0.1'
compile 'com.squareup.retrofit2:converter-gson:2.0.1'
compile 'com.squareup.retrofit2:adapter-rxjava:2.0.1'
compile 'io.reactivex:rxandroid:1.1.0'
compile 'com.squareup.okhttp3:logging-interceptor:3.4.1'
compile 'com.facebook.fresco:fresco:0.14.1'
compile 'com.jcodecraeer:xrecyclerview:1.3.2'
compile 'com.android.support:recyclerview-v7:21.0.+'
compile 'com.squareup.okhttp3:okhttp:3.0.0'
compile 'com.squareup.okio:okio:1.5.0'
// 封装类
package greendao.bewei.com.lianxi;
import java.util.concurrent.TimeUnit;
import okhttp3.OkHttpClient;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;
public class RetrofitUtils {
private static RetrofitUtils retrofitUtils;
private RetrofitUtils(){
}
public static RetrofitUtils getInstance(){
if(retrofitUtils==null){
synchronized (RetrofitUtils.class){
if(retrofitUtils==null){
retrofitUtils=new RetrofitUtils();
}
}
}
return retrofitUtils;
}
private static Retrofit retrofit;
public static synchronized Retrofit getRetrofit(String url){
//日志拦截器
/* HttpLoggingInterceptor httpLoggingInterceptor=new HttpLoggingInterceptor(new HttpLoggingInterceptor.Logger() {
@Override
public void log(String message) {
Log.i("xxx",message);
}
});
httpLoggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);*/
//网络拦截器
LoggingInterceptor loggingInterceptor = new LoggingInterceptor();
OkHttpClient okHttpClient=new OkHttpClient.Builder()
.addInterceptor(loggingInterceptor)
.connectTimeout(500, TimeUnit.SECONDS)
.readTimeout(500,TimeUnit.SECONDS)
.retryOnConnectionFailure(false)
.build();
if(retrofit==null){
retrofit=new Retrofit.Builder().baseUrl(url)
.client(okHttpClient)
.addConverterFactory(GsonConverterFactory.create())
.addCallAdapterFactory(RxJavaCallAdapterFactory.create())
.build();
}
return retrofit;
}
public <T>T getApiService(String url,Class<T> cl){
Retrofit retrofit = getRetrofit(url);//得到retrofit
return retrofit.create(cl);//返回的就是网络接口对象
}
}
//拦截器
package test.bwie.com.mymusic.Model.OkHttp;
import android.os.Build;
import android.util.Log;
import com.google.gson.Gson;
import java.io.IOException;
import java.util.logging.Logger;
import okhttp3.Interceptor;
import okhttp3.Request;
import okhttp3.Response;
/**
* 网络拦截器
*/
public class LoggingInterceptor implements Interceptor {
private static final String UA = "User-Agent";
@Override
public Response intercept(Chain chain) throws IOException {
Request request = chain.request()
.newBuilder()
.addHeader(UA, makeUA())
.build();
return chain.proceed(request);
}
private String makeUA() {
String s = Build.BRAND + "/" + Build.MODEL + "/" + Build.VERSION.RELEASE;
return Build.BRAND + "/" + Build.MODEL + "/" + Build.VERSION.RELEASE;
}
}
- Retrofit+Rxjava+ok+拦截器的封装
- Retrofit+Rxjava的封装
- MVP基类,Retrofit,OkHttp拦截器,RxJava封装
- MVP基类,Retrofit,OkHttp拦截器,RxJava封装
- retrofit+ok+rxjava+fresco的demo
- Retrofit+RXJAVA添加拦截器
- MVP+rxJava+retrofit+拦截器
- RxJava与Retrofit的封装
- RxJava+Retrofit+MVVM的封装
- Retrofit和RxJava的封装
- MVP+Retrofit+RxJava+Okhttp拦截器
- MVP+Retrofit+RxJava+Okhttp拦截器
- Rxjava + Retrofit 错误拦截
- Rxjava 和 Retrofit 的封装使用
- Retrofit+OKhttp+RxJava框架的封装
- RxJava+okhttp+Retrofit+Mvp 的封装
- RXJava+Retrofit+MVP的简单封装
- Retrofit + Rxjava网络层的优雅封装
- c3p0和dbcp的区别
- 关于错误:android.widget.Toolbar无法转换为android.support.v7.widget.Toolbar
- word中mathtype使用问题
- 深度学习(一)logistic回归
- Anaconda安装opencv
- Retrofit+Rxjava+ok+拦截器的封装
- java基础零碎基础知识整理
- 【Machine Learning】KNN学习算法与C语言实现
- RxJava+Retrofit+OkHttp+mvp
- 可变参数的实现
- C语言基础与提高4——指针函数,函数指针
- Swagger与SpringMVC整合
- BZOJ3728: PA2014Final Zarowki
- 图形学opengl实验二-桌子的矩阵变换