网络请求框架库OkHttpUtils
https://github.com/hongyangAndroid/okhttputils
广告轮播Banner
https://github.com/youth5201314/banner
图片加载库
https://github.com/bumptech/glide
下拉刷新上拉加载库
https://github.com/android-cjj/Android-MaterialRefreshLayout
Json解析库
https://github.com/google/gson
Alibaba开源库vLayout
https://github.com/alibaba/vlayout
在module的build.gradle导入库
导入网络请求库
compile 'com.zhy:okhttputils:2.6.2' compile
导入图片加载库
'com.github.bumptech.glide:okhttp3-integration:1.5.0@aar' compile 'com.squareup.okhttp3:okhttp:3.3.1' compile 'com.github.bumptech.glide:glide:3.7.0' compile
导入图片轮播及下拉刷新库
'com.youth.banner:banner:1.4.10' compile 'com.cjj.materialrefeshlayout:library:1.3.0'
vLayout及gson库
compile('com.alibaba.android:vlayout:1.2.8@aar') {transitive = true }
compile 'com.google.code.gson:gson:2.8.2'
网络请求权限,外部存储权限
private void initOkHttpUtils(){
//保持Cookie
CookieJarImpl cookieJar = new CookieJarImpl(new
PersistentCookieStore(getApplicationContext()));
OkHttpClient okHttpClient = new OkHttpClient.Builder()
.cookieJar(cookieJar)
.connectTimeout(10000L, TimeUnit.MILLISECONDS)
.readTimeout(10000L,TimeUnit.MILLISECONDS)
.addInterceptor(new LoggerInterceptor("TAG"))
.build();
OkHttpUtils.initClient(okHttpClient);
}
// 定义缓存大小和位置
builder.setDiskCache(new InternalCacheDiskCacheFactory(context, diskSize)); //内存中
builder.setDiskCache(new ExternalCacheDiskCacheFactory(context, "cache", diskSize)); //sd卡中
// 默认内存和图片池大小
MemorySizeCalculator calculator = new MemorySizeCalculator(context);
int defaultMemoryCacheSize = calculator.getMemoryCacheSize(); // 默认内存大小
int defaultBitmapPoolSize = calculator.getBitmapPoolSize(); // 默认图片池大小
builder.setMemoryCache(new LruResourceCache(defaultMemoryCacheSize)); // 该两句无需设置,
是默认的
builder.setBitmapPool(new LruBitmapPool(defaultBitmapPoolSize));
// 自定义内存和图片池大小
……
根据服务端响应数据格式封装:SverResponse及ResponseCode
public class SverResponse<T> implements Serializable {
private static final long serialVersionUID = 1L;
private int status;
private String msg;
private T data; ……
}
public enum ResponseCode {
SUCCESS(0,"SUCCESS"), ERROR(1,"ERROR"), UNLOGIN(2,"UNLOGIN");
private final int code;
private final String desc; ……
}
public static <T> SverResponse<T> formJson(String json,final Type type){
Type resultType = new ParameterizedType() {
@Override
public Type[] getActualTypeArguments() {
return new Type[]{type};
} …… };
return gson.fromJson(json,resultType);
}