utils包下:
--------------------------------------------------------
public class CrashConfig {
public static final boolean DEBUG = BuildConfig.DEBUG;
public static final boolean HAVE_LOG = DEBUG ? true : false;//debug产生错误日志
// public static final String HAVE_LOG = DEBUG ? false : true;//release产生错误日志
}
---------------------------------------------------------
public class CrashHandler implements UncaughtExceptionHandler {
public static final String TAG = "CrashHandler";
//系统默认的UncaughtException处理类
private UncaughtExceptionHandler mDefaultHandler;
//CrashHandler实例
private static CrashHandler INSTANCE = new CrashHandler();
//程序的Context对象
private Context mContext;
//用于格式化日期,作为日志文件名的一部分
private DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss");
/**
* 保证只有一个CrashHandler实例
*/
private CrashHandler() {
}
/**
* 获取CrashHandler实例 ,单例模式
*/
public static CrashHandler getInstance() {
return INSTANCE;
}
/**
* 初始化
*
* @param context
*/
public void init(Context context) {
mContext = context;
//获取系统默认的UncaughtException处理器
mDefaultHandler = Thread.getDefaultUncaughtExceptionHandler();
//设置该CrashHandler为程序的默认处理器
Thread.setDefaultUncaughtExceptionHandler(this);
}
/**
* 当UncaughtException发生时会转入该函数来处理
*/
@Override
public void uncaughtException(Thread thread, Throwable ex) {
if (!handleException(ex) && mDefaultHandler != null) {
//如果用户没有处理则让系统默认的异常处理器来处理
mDefaultHandler.uncaughtException(thread, ex);
} else {
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
Log.e(TAG, "error : ", e);
}
//退出程序
android.os.Process.killProcess(android.os.Process.myPid());
System.exit(1);
}
}
/**
* 自定义错误处理,收集错误信息 发送错误报告等操作均在此完成.
*
* @param ex
* @return true:如果处理了该异常信息;否则返回false.
*/
private boolean handleException(final Throwable ex) {
if (ex == null) {
return false;
}
final String strhh = saveCrashInfo2File(ex);
Log.e(TAG, strhh);
//使用Toast来显示异常信息
new Thread() {
@Override
public void run() {
Looper.prepare();
Toast.makeText(mContext, strhh, Toast.LENGTH_LONG).show();
// Toast.makeText(mContext, "很抱歉,程序出现异常,即将退出.", Toast.LENGTH_LONG).show();
Looper.loop();
}
}.start();
//收集设备参数信息,保存日志文件
writeFileSdcardFile(FileUtils.SDPATH, "Crash_" + System.currentTimeMillis() + ".txt", saveCrashInfo2File(ex), ex.getMessage());
return true;
}
/**
* 收集设备信息与错误日志
*
* @param e
*/
public String saveCrashInfo2File(Throwable e) {
StringBuilder sb = new StringBuilder();
sb.append("生产厂商:\n");
sb.append(Build.MANUFACTURER).append("\n\n");
sb.append("手机型号:\n");
sb.append(Build.MODEL).append("\n\n");
sb.append("系统版本:\n");
sb.append(Build.VERSION.RELEASE).append("\n\n");
sb.append("异常时间:\n");
sb.append(formatter.format(new Date())).append("\n\n");
sb.append("异常类型:\n");
sb.append(e.getClass().getName()).append("\n\n");
sb.append("异常信息:\n");
sb.append(e.getMessage()).append("\n\n");
sb.append("异常堆栈:\n");
Writer writer = new StringWriter();
PrintWriter printWriter = new PrintWriter(writer);
e.printStackTrace(printWriter);
Throwable cause = e.getCause();
while (cause != null) {
cause.printStackTrace(printWriter);
cause = cause.getCause();
}
printWriter.close();
String result = writer.toString();
sb.append(result);
return sb.toString();
}
/**
* 保存错误信息到文件中
*
* @param path
* @param fileName 文件名
* @param write_str 错误日志
* @param ex 错误信息
*/
public void writeFileSdcardFile(String path, String fileName, String write_str, String ex) {
if (!FileUtils.file.exists()) {
FileUtils.CreateDir();
}
try {
FileOutputStream fout = new FileOutputStream(path + fileName);
byte[] bytes = write_str.getBytes();
fout.write(bytes);
fout.close();
Log.e(TAG, "保存成功" + path + fileName);
//此地做上传错误日志代码
// uploadLogFile(new File(path + fileName), ex);
} catch (Exception e) {
e.printStackTrace();
Log.e(TAG, "保存失败");
}
}
}
----------------------------------------------------------------
public class FileUtils {
public static String SDPATH = Environment.getExternalStorageDirectory() + "/ACrash/";//文件夹路径
public static File file = new File(SDPATH);
public static void CreateDir() {
if (!file.exists()) {//创建文件夹
file.mkdirs();
}
}
/**
* 检查路径是否存在
*
* @param path
* @return
*/
public static boolean checkFilePathExists(String path) {
return new File(path).exists();
}
}
-------------------------------------------------------
public abstract class GsonArrayCallback<T> implements Callback {
private Handler handler = OkHttp3Utils.getInstance().getHandler();
//主线程处理
public abstract void onUi(List<T> list);
//主线程处理
public abstract void onFailed(Call call, IOException e);
//请求失败
@Override
public void onFailure(final Call call, final IOException e) {
handler.post(new Runnable() {
@Override
public void run() {
onFailed(call, e);
}
});
}
//请求json 并直接返回集合 主线程处理
@Override
public void onResponse(Call call, Response response) throws IOException {
final List<T> mList = new ArrayList<T>();
String json = response.body().string();
JsonArray array = new JsonParser().parse(json).getAsJsonArray();
Gson gson = new Gson();
Class<T> cls = null;
Class clz = this.getClass();
ParameterizedType type = (ParameterizedType) clz.getGenericSuperclass();
Type[] types = type.getActualTypeArguments();
cls = (Class<T>) types[0];
for(final JsonElement elem : array){
//循环遍历把对象添加到集合
mList.add((T) gson.fromJson(elem, cls));
}
handler.post(new Runnable() {
@Override
public void run() {
onUi(mList);
}
});
}
}
-----------------------------------------------------------
public abstract class GsonObjectCallback<T> implements Callback {
private Handler handler = OkHttp3Utils.getInstance().getHandler();
//主线程处理
public abstract void onUi(T t);
//主线程处理
public abstract void onFailed(Call call, IOException e);
//请求失败
@Override
public void onFailure(final Call call, final IOException e) {
handler.post(new Runnable() {
@Override
public void run() {
onFailed(call, e);
}
});
}
//请求json 并直接返回泛型的对象 主线程处理
@Override
public void onResponse(Call call, Response response) throws IOException {
//如果返回的数据超过1M就不要使用string方法,这样会把数据全部加载到内存中,应该使用流;
String json = response.body().string();
Class<T> cls = null;
Class clz = this.getClass();
ParameterizedType type = (ParameterizedType) clz.getGenericSuperclass();
Type[] types = type.getActualTypeArguments();
cls = (Class<T>) types[0];
Gson gson = new Gson();
final T t = gson.fromJson(json, cls);
handler.post(new Runnable() {
@Override
public void run() {
onUi(t);
}
});
}
}
----------------------------------------------------------------------
public class LoggingInterceptor implements Interceptor {
@Override
public Response intercept(Chain chain) throws IOException {
//这个chain里面包含了request和response,所以你要什么都可以从这里拿
Request request = chain.request();
long t1 = System.nanoTime();//请求发起的时间
// logger.info(String.format("发送请求 %s on %s%n%s",
// request.url(), chain.connection(), request.headers()));
Log.e("******",request.url()+"");
Response response = chain.proceed(request);
long t2 = System.nanoTime();//收到响应的时间
//这里不能直接使用response.body().string()的方式输出日志
//因为response.body().string()之后,response中的流会被关闭,程序会报错,我们需要创建出一
//个新的response给应用层处理
ResponseBody responseBody = response.peekBody(1024 * 1024);
//
// logger.info(String.format("接收响应: [%s] %n返回json:【%s】 %.1fms%n%s",
// response.request().url(),
// responseBody.string(),
// (t2 - t1) / 1e6d,
// response.headers()));
Log.e("******",responseBody.string());
return response;
}
}
-------------------------------------------------------------
public class NetWorkUtils {
//判断网络是否连接
public static boolean isNetWorkAvailable(Context context) {
//网络连接管理器
ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
//网络信息
NetworkInfo info = connectivityManager.getActiveNetworkInfo();
if (info != null) {
return true;
}
return false;
}
}
---------------------------------------------------------
public class OkHttp3Utils {
/**
* 懒汉 安全 加同步
* 私有的静态成员变量 只声明不创建
* 私有的构造方法
* 提供返回实例的静态方法
*/
private static OkHttp3Utils okHttp3Utils = null;
private OkHttp3Utils() {
}
public static OkHttp3Utils getInstance() {
if (okHttp3Utils == null) {
//加同步安全
synchronized (OkHttp3Utils.class) {
if (okHttp3Utils == null) {
okHttp3Utils = new OkHttp3Utils();
}
}
}
return okHttp3Utils;
}
private static OkHttpClient okHttpClient = null;
public synchronized static OkHttpClient getOkHttpClient() {
if (okHttpClient == null) {
//判空 为空创建实例
// okHttpClient = new OkHttpClient();
/**
* 和OkHttp2.x有区别的是不能通过OkHttpClient直接设置超时时间和缓存了,而是通过OkHttpClient.Builder来设置,
* 通过builder配置好OkHttpClient后用builder.build()来返回OkHttpClient,
* 所以我们通常不会调用new OkHttpClient()来得到OkHttpClient,而是通过builder.build():
*/
// File sdcache = getExternalCacheDir();
//缓存目录
File sdcache = new File(Environment.getExternalStorageDirectory(), "cache");
int cacheSize = 10 * 1024 * 1024;
//OkHttp3拦截器
HttpLoggingInterceptor httpLoggingInterceptor = new HttpLoggingInterceptor(new HttpLoggingInterceptor.Logger() {
@Override
public void log(String message) {
Log.i("xxx", message.toString());
}
});
//Okhttp3的拦截器日志分类 4种
//共包含四个级别:NONE、BASIC、HEADER、BODY
//NONE 不记录 BASIC 请求/响应行 HEADER 请求/响应行 + 头 BODY 请求/响应行 + 头 + 体
httpLoggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
okHttpClient = new OkHttpClient.Builder().connectTimeout(15, TimeUnit.SECONDS)
//添加OkHttp3的拦截器
.addInterceptor(httpLoggingInterceptor)
.addInterceptor(new LoggingInterceptor())
.addNetworkInterceptor(new CacheInterceptor(){
@Override
public Response intercept(Chain chain) throws IOException {
return super.intercept(chain);
}
})
.writeTimeout(20, TimeUnit.SECONDS).readTimeout(20, TimeUnit.SECONDS)
.cache(new Cache(sdcache.getAbsoluteFile(), cacheSize))
.build();
}
return okHttpClient;
}
private static Handler mHandler = null;
public synchronized static Handler getHandler() {
if (mHandler == null) {
mHandler = new Handler();
}
return mHandler;
}
/**
* get请求
* 参数1 url
* 参数2 回调Callback
*/
public static void doGet(String url, Callback callback) {
//创建OkHttpClient请求对象
OkHttpClient okHttpClient = getOkHttpClient();
//创建Request
Request request = new Request.Builder().url(url).build();
//得到Call对象
Call call = okHttpClient.newCall(request);
//执行异步请求
call.enqueue(callback);
}
/**
* post请求
* 参数1 url
* 参数2 回调Callback
*/
public static void doPost(String url, Map<String, String> params, Callback callback) {
//创建OkHttpClient请求对象
OkHttpClient okHttpClient = getOkHttpClient();
//3.x版本post请求换成FormBody 封装键值对参数
FormBody.Builder builder = new FormBody.Builder();
//遍历集合
for (String key : params.keySet()) {
builder.add(key, params.get(key));
}
//创建Request
Request request = new Request.Builder().url(url).post(builder.build()).build();
Call call = okHttpClient.newCall(request);
call.enqueue(callback);
}
/**
* post请求上传文件
* 参数1 url
* 参数2 回调Callback
*/
public static void uploadPic(String url, File file, String fileName) {
//创建OkHttpClient请求对象
OkHttpClient okHttpClient = getOkHttpClient();
//创建RequestBody 封装file参数
RequestBody fileBody = RequestBody.create(MediaType.parse("application/octet-stream"), file);
//创建RequestBody 设置类型等
RequestBody requestBody = new MultipartBody.Builder().setType(MultipartBody.FORM).addFormDataPart("file", fileName, fileBody).build();
//创建Request
Request request = new Request.Builder().url(url).post(requestBody).build();
//得到Call
Call call = okHttpClient.newCall(request);
//执行请求
call.enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
}
@Override
public void onResponse(Call call, Response response) throws IOException {
//上传成功回调 目前不需要处理
}
});
}
/**
* Post请求发送JSON数据
* 参数一:请求Url
* 参数二:请求的JSON
* 参数三:请求回调
*/
public static void doPostJson(String url, String jsonParams, Callback callback) {
RequestBody requestBody = RequestBody.create(MediaType.parse("application/json; charset=utf-8"), jsonParams);
Request request = new Request.Builder().url(url).post(requestBody).build();
Call call = getOkHttpClient().newCall(request);
call.enqueue(callback);
}
/**
* 下载文件 以流的形式把apk写入的指定文件 得到file后进行安装
* 参数一:请求Url
* 参数二:保存文件的路径名
* 参数三:保存文件的文件名
*/
public static void download(final MainActivity context, final String url, final String saveDir) {
Request request = new Request.Builder().url(url).build();
Call call = getOkHttpClient().newCall(request);
call.enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
Log.i("xxx", e.toString());
}
@Override
public void onResponse(Call call, final Response response) throws IOException {
InputStream is = null;
byte[] buf = new byte[2048];
int len = 0;
FileOutputStream fos = null;
try {
is = response.body().byteStream();
//apk保存路径
final String fileDir = isExistDir(saveDir);
//文件
File file = new File(fileDir, getNameFromUrl(url));
fos = new FileOutputStream(file);
while ((len = is.read(buf)) != -1) {
fos.write(buf, 0, len);
}
fos.flush();
//apk下载完成后 调用系统的安装方法
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(file), "application/vnd.android.package-archive");
context.startActivity(intent);
} catch (IOException e) {
e.printStackTrace();
} finally {
if (is != null) is.close();
if (fos != null) fos.close();
}
}
});
}
/**
* @param saveDir
* @return
* @throws IOException 判断下载目录是否存在
*/
public static String isExistDir(String saveDir) throws IOException {
// 下载位置
if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
File downloadFile = new File(Environment.getExternalStorageDirectory(), saveDir);
if (!downloadFile.mkdirs()) {
downloadFile.createNewFile();
}
String savePath = downloadFile.getAbsolutePath();
Log.e("savePath", savePath);
return savePath;
}
return null;
}
/**
* @param url
* @return 从下载连接中解析出文件名
*/
private static String getNameFromUrl(String url) {
return url.substring(url.lastIndexOf("/") + 1);
}
/**
* 为okhttp添加缓存,这里是考虑到服务器不支持缓存时,从而让okhttp支持缓存
*/
private static class CacheInterceptor implements Interceptor {
@Override
public Response intercept(Chain chain) throws IOException {
// 有网络时 设置缓存超时时间1个小时
int maxAge = 60 * 60;
// 无网络时,设置超时为1天
int maxStale = 60 * 60 * 24;
Request request = chain.request();
if (NetWorkUtils.isNetWorkAvailable(MyApp.getInstance())) {
//有网络时只从网络获取
request = request.newBuilder().cacheControl(CacheControl.FORCE_NETWORK).build();
} else {
//无网络时只从缓存中读取
request = request.newBuilder().cacheControl(CacheControl.FORCE_CACHE).build();
/* Looper.prepare();
Toast.makeText(MyApp.getInstance(), "走拦截器缓存", Toast.LENGTH_SHORT).show();
Looper.loop();*/
}
Response response = chain.proceed(request);
if (NetWorkUtils.isNetWorkAvailable(MyApp.getInstance())) {
response = response.newBuilder()
.removeHeader("Pragma")
.header("Cache-Control", "public, max-age=" + maxAge)
.build();
} else {
response = response.newBuilder()
.removeHeader("Pragma")
.header("Cache-Control", "public, only-if-cached, max-stale=" + maxStale)
.build();
}
return response;
}
}
}
********************************************************
创建applcation
public class MyApp extends Application{
public static MyApp mInstance;
@Override
public void onCreate() {
super.onCreate();
mInstance = this;
FileUtils.CreateDir();//创建错误日志文件夹
if (CrashConfig.HAVE_LOG) {
CrashHandler crashHandler = CrashHandler.getInstance();
crashHandler.init(this.getApplicationContext());
}
boolean b = FileUtils.checkFilePathExists(FileUtils.SDPATH);
StringBuffer buffer = new StringBuffer();
buffer.append("是否会生成错误日志:"+(CrashConfig.HAVE_LOG+""))
.append("\n\n")
.append("当前编译模式:")
.append(BuildConfig.DEBUG ? "debug模式" : "release模式")
.append("\n\n")
.append("存放错误日志文件夹是否存在:" + b)
.append("\n\n")
.append("存放错误日志文件夹物理路径:")
.append("\n\n")
.append(FileUtils.file.getAbsolutePath());
}
public static MyApp getInstance() {
return mInstance;
}
}
-----------------------------------------------
Mainactivity ok请求
protected void initData()
{
OkHttp3Utils.doGet("http://120.27.23.105/product/getProducts?pscid=1", new Callback() {
@Override
public void onFailure(Call call, IOException e) {
}
@Override
public void onResponse(Call call, Response response) throws IOException {
final String string = response.body().string();
handler.post(new Runnable() {
@Override
public void run() {
Gson gson = new Gson();
GoodsBean bean = gson.fromJson(string, GoodsBean.class);
mDatas.addAll(bean.data);
System.out.println(mDatas.size());
}
});
}
});
**************************************
recyclerView多条目的Adapter
public class MyAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private List<GoodsBean.DataBean> list = new ArrayList<>();
private Context context;
private LayoutInflater inflater;
private final int ZERO_TYPE = 0,ONE_TYPE = 1,TEO_TYPE = 2;
// public MyAdapter(Context context) {
// this.context = context;
// inflater = LayoutInflater.from(context);
// }
public MyAdapter(List<GoodsBean.DataBean> list, Context context) {
this.list = list;
this.context = context;
inflater = LayoutInflater.from(context);
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
final ViewHolder1 holder1;
final ViewHolder2 holder2;
final ViewHolder3 holder3;
if (viewType == ZERO_TYPE) {
final View view = inflater.inflate(R.layout.goods_item, parent, false);
holder2 = new ViewHolder2(view);
view.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
setRecyclerViewListener.setRecyclerViewListener(view,holder2.getLayoutPosition());
}
});
return holder2;
}else if (viewType == ONE_TYPE){
final View view = inflater.inflate(R.layout.recycler_item1, parent, false);
holder1 = new ViewHolder1(view);
view.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
setRecyclerViewListener.setRecyclerViewListener(view,holder1.getLayoutPosition());
}
});
return holder1;
}
final View view = inflater.inflate(R.layout.recycler_item2, parent, false);
holder3 = new ViewHolder3(view);
view.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
setRecyclerViewListener.setRecyclerViewListener(view,holder3.getLayoutPosition());
}
});
return holder3;
}
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
GoodsBean.DataBean bean = list.get(position);
String[] split = bean.images.split("\\|");
if (holder instanceof ViewHolder2){
Glide.with(context).load(split[0]).into(((ViewHolder2) holder).imageView);
((ViewHolder2) holder).textView.setText(bean.subhead);
}
if (holder instanceof ViewHolder1){
((ViewHolder1) holder).textView.setText(bean.title);
}
if (holder instanceof ViewHolder3){
Glide.with(context).load(split[0]).into(((ViewHolder3) holder).img1);
Glide.with(context).load(split[1]).into(((ViewHolder3) holder).img2);
Glide.with(context).load(split[2]).into(((ViewHolder3) holder).img3);
Glide.with(context).load(split[0]).into(((ViewHolder3) holder).img4);
}
}
@Override
public int getItemCount() {
return list==null?0:list.size();
}
@Override
public int getItemViewType(int position) {
if (list.get(position).itemtype == ONE_TYPE){
return ONE_TYPE;
}else if (list.get(position).itemtype == ZERO_TYPE){
return ZERO_TYPE;
}
return TEO_TYPE;
}
public class ViewHolder1 extends RecyclerView.ViewHolder{
public TextView textView;
public ViewHolder1(View itemView) {
super(itemView);
textView = (TextView)itemView.findViewById(R.id.one_text);
}
}
public class ViewHolder2 extends RecyclerView.ViewHolder{
public TextView textView;
public ImageView imageView;
public ViewHolder2(View itemView) {
super(itemView);
textView = (TextView)itemView.findViewById(R.id.zero_text);
imageView = (ImageView)itemView.findViewById(R.id.zero_img);
}
}
public class ViewHolder3 extends RecyclerView.ViewHolder{
public ImageView img1,img2,img3,img4;
public ViewHolder3(View itemView) {
super(itemView);
img1 = (ImageView)itemView.findViewById(R.id.two_img1);
img2 = (ImageView)itemView.findViewById(R.id.two_img2);
img3 = (ImageView)itemView.findViewById(R.id.two_img3);
img4 = (ImageView)itemView.findViewById(R.id.two_img4);
}
}
private SetRecyclerViewListener setRecyclerViewListener;
public void setListener(SetRecyclerViewListener setRecyclerViewListener){
this.setRecyclerViewListener = setRecyclerViewListener;
}
public interface SetRecyclerViewListener{
void setRecyclerViewListener(View view,int position);
}
}