一、工具类的封装
- SharedPreferencesUtils工具类
public class SharedPreferencesUtils {
/**
* 保存在手机里面的文件名
*/
private static final String FILE_NAME = "share_date";
/**
* 保存数据的方法,我们需要拿到保存数据的具体类型,然后根据类型调用不同的保存方法
* @param context
* @param key
* @param object
*/
public static void setParam(Context context , String key, Object object){
String type = object.getClass().getSimpleName();
SharedPreferences sp = context.getSharedPreferences(FILE_NAME, Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sp.edit();
if("String".equals(type)){
editor.putString(key, (String)object);
}
else if("Integer".equals(type)){
editor.putInt(key, (Integer)object);
}
else if("Boolean".equals(type)){
editor.putBoolean(key, (Boolean)object);
}
else if("Float".equals(type)){
editor.putFloat(key, (Float)object);
}
else if("Long".equals(type)){
editor.putLong(key, (Long)object);
}
editor.commit();
}
/**
* 得到保存数据的方法,我们根据默认值得到保存的数据的具体类型,然后调用相对于的方法获取值
* @param context
* @param key
* @param defaultObject
* @return
*/
public static Object getParam(Context context , String key, Object defaultObject){
String type = defaultObject.getClass().getSimpleName();
SharedPreferences sp = context.getSharedPreferences(FILE_NAME, Context.MODE_PRIVATE);
if("String".equals(type)){
return sp.getString(key, (String)defaultObject);
}
else if("Integer".equals(type)){
return sp.getInt(key, (Integer)defaultObject);
}
else if("Boolean".equals(type)){
return sp.getBoolean(key, (Boolean)defaultObject);
}
else if("Float".equals(type)){
return sp.getFloat(key, (Float)defaultObject);
}
else if("Long".equals(type)){
return sp.getLong(key, (Long)defaultObject);
}
return null;
}
}
二、网络数据的封装
public class HttpUtil {
private static HttpUtil httpUtil;
private final Retrofit.Builder builder;
private HttpUtil() {
builder = new Retrofit.Builder()
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.addConverterFactory(GsonConverterFactory.create());
}
public <T> T getService(String url,Class<T> mClass){
return builder.baseUrl(url).build().create(mClass);
}
public static HttpUtil getInstance() {
if (httpUtil == null) {
synchronized (HttpUtil.class) {
if (httpUtil == null) {
httpUtil = new HttpUtil();
}
}
}
return httpUtil;
}
}
- Rxjava线程切换抽取
public class RxUtil {
/**
* <T , T>转换前的对象类型 转换后的对象类型
* @param <T>
* @return
*/
public static <T> ObservableTransformer<T, T> rx() {
return new ObservableTransformer<T, T>() {
@Override
public ObservableSource<T> apply(Observable<T> upstream) {
return upstream.subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread());
}
};
}
}
- 自定义客户端
private OkHttpClient getOkhttpClient() {
//设置本地缓存文件
File file = new File(Constants.PATH_CACHE);
//设置缓存文件大小
Cache cache = new Cache(file, 1024 * 1024 * 50);
//设置日志拦截器
HttpLoggingInterceptor httpLoggingInterceptor = new HttpLoggingInterceptor();
if (BuildConfig.DEBUG) {
httpLoggingInterceptor.setLevel(HttpLoggingInterceptor.Level.HEADERS);
} else {
httpLoggingInterceptor.setLevel(HttpLoggingInterceptor.Level.NONE);
}
return new OkHttpClient.Builder()
.cache(cache)
.addInterceptor(httpLoggingInterceptor)//设置日志拦截器
.addInterceptor(new MyCacheIntercepter())//设置网络拦截器
.addNetworkInterceptor(new MyCacheIntercepter())
.writeTimeout(10, TimeUnit.SECONDS)
.readTimeout(10, TimeUnit.SECONDS)
.connectTimeout(10, TimeUnit.SECONDS)
.retryOnConnectionFailure(true)
.build();
}
- 网络拦截器
public class MyCacheIntercepter implements Interceptor {
@Override
public Response intercept(Chain chain) throws IOException {
Request request = chain.request();
if (!isNetworkAvailable(App.getApp())) {
request = request.newBuilder().cacheControl(CacheControl.FORCE_CACHE).build();
}
Response originalResponse = chain.proceed(request);
if (isNetworkAvailable(App.getApp())) {
int maxAge = 0;
return originalResponse.newBuilder()
.removeHeader("Pragma")
.header("Cache-Control", "public ,max-age=" + maxAge)
.build();
} else {
int maxStale = 15 * 60;
return originalResponse.newBuilder()
.removeHeader("Pragma")
.header("Cache-Control", "public, only-if-cached, max-stale=" + maxStale)
.build();
}
}
}
- 检查是否有网
/**
* 检测是否有网
*/
public static boolean isNetworkAvailable(Context context) {
if (context != null) {
ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo info = cm.getActiveNetworkInfo();
if (info != null) {
return info.isAvailable();
}
}
return false;
}
- Observable简化工具类
public abstract class BaseObserver<T> implements Observer<T> {
@Override
public void onSubscribe(Disposable d) {
Log.e("TAG", "绑定成功");
}
@Override
public void onComplete() {
Log.e("TAG", "onComplete");
}
@Override
public void onError(Throwable e) {
Log.e("TAG", "onError:" + e.getMessage());
}
}
三、整体的界面的搭建
-
极客App初识
-
分包:根据功能和类型
-
抽取base类
-
网络工具封装
-
主界面搭建:布局、代码
网友评论