22
33import android .content .Context ;
44import android .os .Handler ;
5+ import android .util .Log ;
56
67import com .loopj .android .http .AsyncHttpClient ;
78import com .loopj .android .http .AsyncHttpResponseHandler ;
1920 */
2021
2122public final class UploadLog {
23+ private static final String TAG = UploadLog .class .getSimpleName ();
2224 private String mLogUrl ; //上传服务器地址
2325 private String mLogPath ; //日志路径
2426 private Context mContext ;
@@ -40,6 +42,7 @@ public void upload(){
4042 @ Override
4143 public void run () {
4244 try {
45+ Log .i (TAG ,"mLogPath: " +mLogPath );
4346 File file = new File (mLogPath );
4447 String logZipPath = file .getParent ()+File .separator +"log.zip" ;
4548 zipUtil .setZipListener (new ZipUtil .ZIPListener () {
@@ -73,21 +76,37 @@ private void upload(String logPath) {
7376 httpClient .post (mContext , mLogUrl , params , new AsyncHttpResponseHandler () {
7477 @ Override
7578 public void onSuccess (int statusCode , Header [] headers , byte [] responseBody ) {
76- if (mUploadLogListener !=null ){
77- String msg =new String (responseBody );
78- if (statusCode ==200 ){
79- mUploadLogListener .onSuccess (msg );
80- }else {
81- mUploadLogListener .onFailure (msg ,statusCode );
79+ try {
80+ if (mUploadLogListener !=null ){
81+ if (responseBody ==null ){
82+ mUploadLogListener .onFailure ("上传日志发生错误!" ,statusCode );
83+ return ;
84+ }
85+ String msg =new String (responseBody );
86+ if (statusCode ==200 ){
87+ mUploadLogListener .onSuccess (msg );
88+ }else {
89+ mUploadLogListener .onFailure (msg ,statusCode );
90+ }
8291 }
92+ } catch (Exception e ) {
93+ e .printStackTrace ();
8394 }
8495 }
8596
8697 @ Override
8798 public void onFailure (int statusCode , Header [] headers , byte [] responseBody , Throwable error ) {
88- if (mUploadLogListener !=null ){
89- String msg =new String (responseBody );
90- mUploadLogListener .onFailure (msg ,statusCode );
99+ try {
100+ if (mUploadLogListener !=null ){
101+ if (responseBody ==null ){
102+ mUploadLogListener .onFailure ("上传日志发生错误!" ,statusCode );
103+ return ;
104+ }
105+ String msg =new String (responseBody );
106+ mUploadLogListener .onFailure (msg ,statusCode );
107+ }
108+ } catch (Exception e ) {
109+ e .printStackTrace ();
91110 }
92111 }
93112 });
@@ -101,15 +120,11 @@ public interface UploadLogListener{
101120 void onFailure (String errorMsg , int errorCode );
102121 }
103122
104- public void setUploadLogListener (UploadLogListener uploadLogListener ) {
105- this .mUploadLogListener = uploadLogListener ;
106- }
107-
108123 public void setLogUrl (String logUrl ) {
109- this .mLogUrl = mLogUrl ;
124+ this .mLogUrl = logUrl ;
110125 }
111126
112127 public void setLogPath (String logPath ) {
113- this .mLogPath = mLogPath ;
128+ this .mLogPath = logPath ;
114129 }
115130}
0 commit comments