Android学习能力之统计日志上传设计

一款软件就像一个孩子,不断的在学习,,在探索,当孩子犯下错误的时候,我们可以去包容,当孩子犯不改的时候,获取他就不再让人喜欢,甚至是去抛弃他。人之常情的问题,也是做软件的我们需要去考虑的问题。同样孩子的成长速度和懂事程度也是我们非常关注的。

本章讲述Android日志上传功能,更快的更准确的将孩子的错误通知给我们,我们帮助孩子改正它的错误。

1.日志统计和日志存储 (Context context, String ex) {if (context == null) {return;}PrintStream printStream = null;ByteArrayOutputStream bos = null;try {bos = new ByteArrayOutputStream(256);printStream = new PrintStream(bos);printStream.print(ex);JSONObject createJson = createJson(context, new String(bos.toByteArray()));//(1)做jsonDBManager.getInstance(context).addCrashLog(createJson.toString());//(2)存储数据信息Prefs.setPrefBoolean(PrefsContants.IS_NEEDED_UPLOAD, true));} catch (Exception e) {Log.e(“CrashUploadUtils”, “writeLog”, e);} finally {closeQuietly(printStream);//(3)closeQuietly(bos);}}

由上面代码可以看出我们将数据字符串传入统计记录后,首先做成json数据,如步骤(1);其次将数据存入数据库,如步骤(2);最后关闭我们的数据流,如步骤三。

(1)做成json数据

private static JSONObject createJson(Context context, String log) {JSONObject object = null;try {object = new JSONObject();object.put(“type”, “crash”);JSONObject data = new JSONObject();data.put(“log”, log);data.put(“uk”, AccountManager.getUK(context));data.put(“version”, IMManager.getVersion());data.put(“trigger_id”, Utility.getTriggerId(context));if (!TextUtils)) {data);}if (!TextUtils)) {data);}if (!TextUtils)) {data);}object.put(“data”, data);object.put(“ts”, System.currentTimeMillis());} catch (JSONException e) {Log.e(“CrashUploadUtils”, “createJson”, e);}return object;}

内容这里可以忽略,自己放入就好。

(2)添加数据库

(String log) {synchronized (mSyncLock) {ContentValues values = new ContentValues();values.put(CrashLogColumns.COLUMN_COTENT, log);insert(TableDefine.DB_TABLE_CRASH_LOG, values);}}

其中insert函数,读者要自己写了。 (3)关闭数据流

(OutputStream os) {if (os != null) {try {os.close();} catch (IOException e) {Log.e(“CrashUploadUtils”, “closeQuietly”, e);}}}

上面我们完成了,日志的统计和信息的存储,其中日志的存储看上面添加数据库的过程。

2.日志上传

日志上传是该功能的关键步骤,涉及的问题有:怎么上传,什么时候上传,什么网络环境下上传? 日志上传功能

private static void upLoad(Context context) {getCrashLog(context);first < 0) {return;}StringBuilder builder = new StringBuilder();builder.append(“device_id=” + Utility.getDeviceType(context));builder.append(“&appid=” + AccountManager.getAppid(context));buildersecond.toString());String jsonResult = null;try {//2上传异常信息jsonResult = HttpUtility.doUploadPost(url, builder.toString().getBytes(“utf-8”));} catch (UnsupportedEncodingException e) {Log.e(“CrashUploadUtils”, “upLoadCrash UnsupportedEncodingException”, e);}if (jsonResult == null) {Log.e(“CrashUploadUtils”, “upload crash log failed!!”);return;}try {JSONObject jsonObject = new JSONObject(jsonResult);int errorCode = jsonObject.optInt(“err_code”);String msg = jsonObject.optString(“msg”);//根据网络结果删除已经上传过的信息,并更新上传记录时间if (errorCode == OK) {//3删除上传过的信息int result = DBManager.getInstance(context).deleteLogBeforeId(log.first);if (result > 0) {//4更新上传时间updateUploadTime(context, System.currentTimeMillis());}}} catch (JSONException e) {Log.e(“CrashUploadUtils”, “upLoadCrash JSONException”, e);}}听过许多故事,见过旅行风景,就这样,

Android学习能力之统计日志上传设计

相关文章:

你感兴趣的文章:

标签云: