概述
首先创建一个Util包,在此包下创建8个类
第一个 CrashConfig
import com.example.ex_demo1.BuildConfig;
* 描述:控制crash时是否产生Log文件
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产生错误日志
}
第二个 CrashHandimport android.content.Context;
import android.os.Build;
import android.os.Looper;
import android.util.Log;
import android.widget.Toast;
import java.io.FileOutputStream;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.io.Writer;
import java.lang.Thread.UncaughtExceptionHandler;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
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("nn");
sb.append("手机型号:n");
sb.append(Build.MODEL).append("nn");
sb.append("系统版本:n");
sb.append(Build.VERSION.RELEASE).append("nn");
sb.append("异常时间:n");
sb.append(formatter.format(new Date())).append("nn");
sb.append("异常类型:n");
sb.append(e.getClass().getName()).append("nn");
sb.append("异常信息:n");
sb.append(e.getMessage()).append("nn");
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, “保存失败”);
}
}
}
第三个 FileUtils
import android.os.Environment;
import java.io.File;
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();
}
}
第四个 GsonArrayCallback
import android.os.Handler;
import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonParser;
import java.io.IOException;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;
public abstract class GsonArrayCallback 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);
}
});
}
}
第五个 GsonObjectCallback
import android.os.Handler;
import com.google.gson.Gson;
import java.io.IOException;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;
/**
* 1. 类的用途 如果要将得到的json直接转化为集合 建议使用该类
* 该类的onUi() onFailed()方法运行在主线程
* 2. @author forever
* 3. @date 2017/9/24 18:47
*/
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);
}
});
}
}
第六个 LoggingInterceptor
import android.util.Log;
import java.io.IOException;
import okhttp3.Interceptor;
import okhttp3.Request;
import okhttp3.Response;
import okhttp3.ResponseBody;
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;
}
}
第七个 NetWorkUtils
package com.example.ex_demo1.utils;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
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;
}
}
第八个 OkHttp3Utils
import android.content.Intent;
import android.net.Uri;
import android.os.Environment;
import android.os.Handler;
import android.util.Log;
import com.example.ex_demo1.app.MyApp;
import com.example.ex_demo1.view.MainActivity;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import okhttp3.Cache;
import okhttp3.CacheControl;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.Interceptor;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
import okhttp3.logging.HttpLoggingInterceptor;
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;
}
}
}
八个类创建完之后 在创建一个app包 里面放入一个Application类,切记要再清单文件中注册
import android.app.Application;
import com.example.ex_demo1.BuildConfig;
import com.example.ex_demo1.utils.CrashConfig;
import com.example.ex_demo1.utils.CrashHandler;
import com.example.ex_demo1.utils.FileUtils;
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("nn")
.append("当前编译模式:")
.append(BuildConfig.DEBUG ? "debug模式" : "release模式")
.append("nn")
.append("存放错误日志文件夹是否存在:" + b)
.append("nn")
.append("存放错误日志文件夹物理路径:")
.append("nn")
.append(FileUtils.file.getAbsolutePath());
}
public static MyApp getInstance() {
return mInstance;
}
}
以上代码 使用方式 在Maiactivity中
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());
}
});
}
});
}
添加网络权限
最后
以上就是完美导师为你收集整理的封装OKHttp 拦截器 异常捕获 打印log日志的全部内容,希望文章能够帮你解决封装OKHttp 拦截器 异常捕获 打印log日志所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复