AutoSearchBug
原文连接:http://9aiplay.com/record/AutoSearchBug/
我的Github:https://github.com/SHPDZY
我的博客:http://9aiplay.com/
这是一款可以在项目崩溃时获取异常
自动联网在Stack Overflow,百度或其他平台上寻找回答链接和详情
原理
android通过UncaughtExceptionHandler来实现获取应用全局的crash信息
启动一个服务来完成联网查找回答
Thread.UncaughtExceptionHandler handler = new Thread.UncaughtExceptionHandler() {
public void uncaughtException(final Thread thread, final Throwable ex) {
String errorMsg = getErrorMsg(ex);
Intent intent = new Intent(mApp, AutoSearchBugService.class);
intent.putExtra(ERROR_MSG, errorMsg);
intent.putExtra(MAX_SIZE, maxSize);
intent.putExtra(SHOW_ANSWER, showAnswer);
intent.putExtra(SEARCH_TYPE, searchType);
mApp.startService(intent);
mUEH.uncaughtException(thread, ex);
}
};
public class AutoSearchBugService extends Service implements AutoSearchBugGlobal {
···
private void search(final String errorMsg, int maxSize, final int showAnswer, int searchType) {
AutoSearchBugHttpUtil.doGet(
AutoSearchBugAPI.SEARCH_URL +
AutoSearchBugAPI.PAGE_SIZE + maxSize +
AutoSearchBugAPI.ERROR_MSG + errorMsg.replace(" ", "%20") +
AutoSearchBugAPI.SHOW_ANSWER + showAnswer +
AutoSearchBugAPI.SEARCH_TYPE + searchType,
new AutoSearchBugHttpUtil.HResponse() {
@Override
public void onStart() {
StringBuffer log = new StringBuffer();
log.append(LINE_START);
log.append(SEARCHING);
log.append(LINE_END);
log(log);
}
@Override
public void onFinish(String msg) {
resolveLog(msg, errorMsg, showAnswer);
stopSelf();
}
@Override
public void onError(String error) {
StringBuffer log = new StringBuffer();
log.append(String.format(ASB_ERROR, error));
log.append(LINE_END);
log(log);
stopSelf();
}
});
}
···
}
服务端通过Jsoup来解析html
@RestController
@RequestMapping("/android")
@Api("android接口")
public class SearchBugApi {
···
/**
* 通过errorMsg来查找回答
* @param page 获取几条数据
* @param errorMsg crash信息
* @param showAnswer 是否显示回答详情
* @param type 0搜索stackoverflow 1搜索baidu
* @return
*/
@GetMapping("autosearchbug")
public R autosearchbug(int page, String errorMsg, int showAnswer, int type) {
switch (type) {
case 0:
return getStackOverFlow(page, errorMsg, showAnswer);
case 1:
return getBD(page, errorMsg);
default:
return getStackOverFlow(page, errorMsg, showAnswer);
}
}
private R getStackOverFlow(int page, String errorMsg, int showAnswer) {
String url = "https://stackoverflow.com/search?q=" + errorMsg;
List<AutoSearchBugEntity> autoSearchBugList = new ArrayList();
try {
Document doc = Jsoup.connect(url).get();
···
return R.ok().putData(autoSearchBugList);
} catch (IOException e) {
e.printStackTrace();
return R.error(e.getMessage());
}
}
···
}
如何引入
Android Studio 引入
第1步 将JitPack存储库添加到您的构建文件
将其添加到存储库末尾的根build.gradle中:
allprojects {
repositories {
...
maven { url 'https://jitpack.io' }
}
}
第2步 添加依赖关系
dependencies {
implementation 'com.github.SHPDZY:AutoSearchBug:1.0.1'
}
Eclipse 引入
建议使用As,方便版本更新。
dependencies {
compile project(path: ':AutoSearchBug')
}
如何使用
AutoSearchBugControl.getBuilder()
.setMaxSize(3) //显示数量
.showAnswer(true) //true显示回答详情
.setSearchType(0) //0在stackoverflow搜索 1:在百度搜索
.init();
效果图
默认初始化未显示回答详情
自定义初始化显示详情