统计下载出错log

This commit is contained in:
huangzhuanghua
2016-10-09 09:49:00 +08:00
parent 8aa90935d8
commit 52f2dd22f2
6 changed files with 74 additions and 12 deletions

View File

@ -17,8 +17,6 @@ import javax.net.ssl.X509TrustManager;
* Created by khy on 2016/10/8.
*/
public class HttpsUtils {
private static MyHostnameVerifier myHostnameVerifier;
private static SSLSocketFactory mySSLSocketFactory;
private static class MyTrustManager implements X509TrustManager {
@Override
@ -47,16 +45,21 @@ public class HttpsUtils {
}
private static SSLSocketFactory mSSLSocketFactory;
private static HostnameVerifier mHostnameVerifier;
public static HttpsURLConnection getHttpsURLConnection(URL url) throws Exception {
if (myHostnameVerifier == null || mySSLSocketFactory == null) {
myHostnameVerifier = new MyHostnameVerifier();
if (mSSLSocketFactory == null || mHostnameVerifier == null) {
SSLContext sc = SSLContext.getInstance("TLS");
sc.init(null, new TrustManager[]{new MyTrustManager()}, new SecureRandom());
mySSLSocketFactory = sc.getSocketFactory();
HttpsURLConnection.setDefaultSSLSocketFactory(mySSLSocketFactory);
HttpsURLConnection.setDefaultHostnameVerifier(myHostnameVerifier);
}
mSSLSocketFactory = sc.getSocketFactory();
mHostnameVerifier = new MyHostnameVerifier();
HttpsURLConnection.setDefaultSSLSocketFactory(mSSLSocketFactory);
HttpsURLConnection.setDefaultHostnameVerifier(mHostnameVerifier);
}
return (HttpsURLConnection) url.openConnection();
}
}

View File

@ -195,7 +195,21 @@ public class DownloadEntity implements Serializable {
public void setSpeed(long speed) {
this.speed = speed;
}
public String getError() {
if (meta != null) {
return meta.get("error");
}
return null;
}
public void setError(String error) {
if (meta == null) {
meta = new HashMap<>();
}
meta.put("error", error);
}
public HashMap<String, String> getMeta() {
return meta;
}

View File

@ -19,4 +19,10 @@ public interface DownloadListener {
*/
void onStatusChanged(DownloadStatus status);
/**
* 更新任务下载状态
* @param status
*/
void onStatusChanged(DownloadStatus status, String error);
}

View File

@ -113,7 +113,11 @@ public class DownloadTask implements DownloadListener {
@Override
public void onStatusChanged(DownloadStatus status) {
onStatusChanged(status, null);
}
@Override
public void onStatusChanged(DownloadStatus status, String error) {
if (status == DownloadStatus.cancel
|| status == DownloadStatus.hijack) {
entry.setProgress(0);
@ -136,6 +140,7 @@ public class DownloadTask implements DownloadListener {
/*********************** 处理网络异常 ***********************/
if (status == DownloadStatus.timeout || status == DownloadStatus.neterror) {
entry.setError(error);
DownloadTask task = DataChanger.getInstance().getDownloadingTasks().remove(entry.getUrl());
if (task != null) {
task.pause();

View File

@ -2,6 +2,7 @@ package com.gh.download;
import android.content.Context;
import android.text.TextUtils;
import android.util.Log;
import com.gh.common.util.HttpsUtils;
import com.gh.common.util.Utils;
@ -56,7 +57,7 @@ public class DownloadThread extends Thread {
URL url = new URL(entry.getUrl());
Utils.log("url = " + entry.getUrl());
HttpURLConnection connection = null;
HttpURLConnection connection;
if ("https".equals(url.getProtocol())) {
connection = HttpsUtils.getHttpsURLConnection(url);
} else {
@ -146,11 +147,13 @@ public class DownloadThread extends Thread {
bos.close();
}
} catch (Exception e) {
String errorMsg = Log.getStackTraceString(e);
//e.getMessage() will null error
if (!TextUtils.isEmpty(e.getMessage()) && e.getMessage().contains("connection timeout")) {
listener.onStatusChanged(DownloadStatus.timeout);
listener.onStatusChanged(DownloadStatus.timeout, errorMsg);
} else {
listener.onStatusChanged(DownloadStatus.neterror);
listener.onStatusChanged(DownloadStatus.neterror, errorMsg);
}
Utils.log(DownloadThread.class.getSimpleName(),
"exception-->" + e.toString());

View File

@ -152,6 +152,9 @@ public class MainActivity extends BaseFragmentActivity implements OnClickListene
if (DownloadStatus.hijack.equals(downloadEntity.getStatus())) {
processHijack(downloadEntity);
return;
} else if (DownloadStatus.neterror.equals(downloadEntity.getStatus())
|| DownloadStatus.timeout.equals(downloadEntity.getStatus())) {
uploadNeterrorLog(downloadEntity);
}
if (downloadEntity.getName().contains("光环助手") && isShowDownload) {
processGhAssistDownload(downloadEntity);
@ -328,6 +331,34 @@ public class MainActivity extends BaseFragmentActivity implements OnClickListene
AppController.addToRequestQueue(request, TAG);
}
// 上传网络错误log
private void uploadNeterrorLog(DownloadEntity downloadEntity) {
String version = PackageUtils.getVersion(this);
String user = DeviceUtils.getDeviceID(this);
String channel = (String) PackageUtils.getMetaData(this,
getPackageName(), "TD_CHANNEL_ID");
Map<String, Object> map = new HashMap<>();
map.put("url", downloadEntity.getUrl());
map.put("game", downloadEntity.getName());
map.put("game_id", downloadEntity.getGameId());
map.put("platform", downloadEntity.getPlatform());
map.put("version", version);
map.put("user", user);
map.put("device_id", TokenUtils.getDeviceId(this));
map.put("channel", channel);
map.put("error", downloadEntity.getError());
String url = "http://data.ghzhushou.com/api/v1d0/log";
Map<String, String> params = new HashMap<>();
params.put("topic", "neterror");
params.put("source", "GH-ASSIST-Client");
params.put("time", String.valueOf(Utils.getTime(this)));
params.put("content", new JSONObject(map).toString());
StringExtendedRequest request = new StringExtendedRequest(Method.POST, url, null, null);
request.setParams(params);
request.setShouldCache(false);
AppController.addToRequestQueue(request, TAG);
}
/*
* 黄壮华 按连续按返回键两次才退出应用
*/