Java 如何在android中以Json格式存储日志数据?
我正在android应用程序中记录事件 现在我需要以JSON格式存储应用程序生成的Java 如何在android中以Json格式存储日志数据?,java,android,json,logging,Java,Android,Json,Logging,我正在android应用程序中记录事件 现在我需要以JSON格式存储应用程序生成的Log数据。。。有什么建议吗 这是使用AppEventLogger类记录Android应用程序各种事件的代码 public class AppEventLogger { private static final String TAG = AppEventsLogger.class.getCanonicalName(); public void Loginlog(Long id, boolean resul
Log
数据。。。有什么建议吗
这是使用AppEventLogger
类记录Android应用程序各种事件的代码
public class AppEventLogger {
private static final String TAG = AppEventsLogger.class.getCanonicalName();
public void Loginlog(Long id, boolean result, int failedattempts, int attempts,long eventTime, long mLastInteractionTime) {
final long eventTime = System.currentTimeMillis();
logLogin(
id,
result,
failedattempts,
attempts,
eventTime,
mLastInteractionTime);
}
public void Logoutlog(long eventTime ,boolean isLoggedOut,){
final long eventTime = System.currentTimeMillis();
logLogout(
eventTime,
isLoggedOut);
}
试试这个 课堂上的变化:
package com.nct.dhruv.demotest;
import android.content.Context;
import android.util.Log;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
/**
* Created by user31 on 4/12/17.
*/
public class AppEventLogger {
private Context mContext;
private String appContextName = "AppEventLogger";
public AppEventLogger(Context context) {
mContext = context;
Loginlog(1, true, 11, 112, 121, 12);
Logoutlog(2, false);
}
//private static final String TAG = AppEventsLogger.class.getCanonicalName();
JSONArray jsonArray = new JSONArray();
public void Loginlog(long id, boolean result, int failedattempts, int attempts, long eventTime, long mLastInteractionTime) {
eventTime = System.currentTimeMillis();
createJson(appContextName , " ID : " + id + " Result : " + result + " failedattempts : " + failedattempts + " attempts : " + attempts + " eventTime : " + " mLastInteractionTime : " + mLastInteractionTime);
/*logLogin(id,
result,
failedattempts,
attempts,
eventTime,
mLastInteractionTime);*/
}
public void Logoutlog(long eventTime, boolean isLoggedOut) {
eventTime = System.currentTimeMillis();
createJson(appContextName, " eventTime : " + eventTime + " isLoggedOut : " + isLoggedOut);
}
private void createJson(String context, String logData) {
try {
Log.d("data", logData);
JSONObject jsonObjOne = new JSONObject();
jsonObjOne.put("context", context);
jsonObjOne.put("logData", logData);
jsonArray.put(jsonObjOne);
JSONObject json = new JSONObject();
json.put("JsonData", jsonArray);
// Log.e("Your_JSON", json.toString());
} catch (JSONException e) {
e.printStackTrace();
}
}
}
{
"JsonData": [{
"context": "AppEventLogger",
"logData": " ID : 1 Result : true failedattempts : 11 attempts : 112 eventTime : mLastInteractionTime : 12"
}, {
"context": "AppEventLogger",
"logData": " eventTime : 1512374548115 isLoggedOut : false"
}]
}
{"log":"12343434"}
输出:
package com.nct.dhruv.demotest;
import android.content.Context;
import android.util.Log;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
/**
* Created by user31 on 4/12/17.
*/
public class AppEventLogger {
private Context mContext;
private String appContextName = "AppEventLogger";
public AppEventLogger(Context context) {
mContext = context;
Loginlog(1, true, 11, 112, 121, 12);
Logoutlog(2, false);
}
//private static final String TAG = AppEventsLogger.class.getCanonicalName();
JSONArray jsonArray = new JSONArray();
public void Loginlog(long id, boolean result, int failedattempts, int attempts, long eventTime, long mLastInteractionTime) {
eventTime = System.currentTimeMillis();
createJson(appContextName , " ID : " + id + " Result : " + result + " failedattempts : " + failedattempts + " attempts : " + attempts + " eventTime : " + " mLastInteractionTime : " + mLastInteractionTime);
/*logLogin(id,
result,
failedattempts,
attempts,
eventTime,
mLastInteractionTime);*/
}
public void Logoutlog(long eventTime, boolean isLoggedOut) {
eventTime = System.currentTimeMillis();
createJson(appContextName, " eventTime : " + eventTime + " isLoggedOut : " + isLoggedOut);
}
private void createJson(String context, String logData) {
try {
Log.d("data", logData);
JSONObject jsonObjOne = new JSONObject();
jsonObjOne.put("context", context);
jsonObjOne.put("logData", logData);
jsonArray.put(jsonObjOne);
JSONObject json = new JSONObject();
json.put("JsonData", jsonArray);
// Log.e("Your_JSON", json.toString());
} catch (JSONException e) {
e.printStackTrace();
}
}
}
{
"JsonData": [{
"context": "AppEventLogger",
"logData": " ID : 1 Result : true failedattempts : 11 attempts : 112 eventTime : mLastInteractionTime : 12"
}, {
"context": "AppEventLogger",
"logData": " eventTime : 1512374548115 isLoggedOut : false"
}]
}
{"log":"12343434"}
旧答案 您的日志字符串如下所示:
private void createJson(String logData) {
try {
Log.d("data", logData);
JSONArray jsonArray = new JSONArray();
JSONObject jsonObj = new JSONObject();
jsonObj.put("log", logData);
jsonArray.put(jsonObj);
Log.e("Your_JSON",jsonObj.toString());
} catch (JSONException e) {
e.printStackTrace();
}
}
从要存储日志数据的位置调用此方法
createJson("12343434");
输出:
package com.nct.dhruv.demotest;
import android.content.Context;
import android.util.Log;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
/**
* Created by user31 on 4/12/17.
*/
public class AppEventLogger {
private Context mContext;
private String appContextName = "AppEventLogger";
public AppEventLogger(Context context) {
mContext = context;
Loginlog(1, true, 11, 112, 121, 12);
Logoutlog(2, false);
}
//private static final String TAG = AppEventsLogger.class.getCanonicalName();
JSONArray jsonArray = new JSONArray();
public void Loginlog(long id, boolean result, int failedattempts, int attempts, long eventTime, long mLastInteractionTime) {
eventTime = System.currentTimeMillis();
createJson(appContextName , " ID : " + id + " Result : " + result + " failedattempts : " + failedattempts + " attempts : " + attempts + " eventTime : " + " mLastInteractionTime : " + mLastInteractionTime);
/*logLogin(id,
result,
failedattempts,
attempts,
eventTime,
mLastInteractionTime);*/
}
public void Logoutlog(long eventTime, boolean isLoggedOut) {
eventTime = System.currentTimeMillis();
createJson(appContextName, " eventTime : " + eventTime + " isLoggedOut : " + isLoggedOut);
}
private void createJson(String context, String logData) {
try {
Log.d("data", logData);
JSONObject jsonObjOne = new JSONObject();
jsonObjOne.put("context", context);
jsonObjOne.put("logData", logData);
jsonArray.put(jsonObjOne);
JSONObject json = new JSONObject();
json.put("JsonData", jsonArray);
// Log.e("Your_JSON", json.toString());
} catch (JSONException e) {
e.printStackTrace();
}
}
}
{
"JsonData": [{
"context": "AppEventLogger",
"logData": " ID : 1 Result : true failedattempts : 11 attempts : 112 eventTime : mLastInteractionTime : 12"
}, {
"context": "AppEventLogger",
"logData": " eventTime : 1512374548115 isLoggedOut : false"
}]
}
{"log":"12343434"}
将
String
转换为JSON
StringToJson.java文件
/**
* Created by dhruv on 18/12/17.
*/
public class StringToJson {
private Context context;
//Crating constructor
public StringToJson(Context context) {
this.context = context;
}
public String getJson(String name, String sport, String age, String id, ArrayList lastScores) {
String result = "";
JSONObject json = new JSONObject();
JSONObject mainJson = new JSONObject();
JSONArray jsonArr = new JSONArray();
try {
mainJson.put("name", name);
mainJson.put("sport", sport);
mainJson.put("age", age);
mainJson.put("id", id);
if (!lastScores.isEmpty()) {
for (int i = 0; i < lastScores.size(); i++) {
jsonArr.put(lastScores.get(i));
}
}
mainJson.put("lastScores", jsonArr);
json.put("result", mainJson);
result = json.get("result").toString();
} catch (JSONException e) {
e.printStackTrace();
}
return result;
}
}
我可以创建一个实现Json格式的接口来存储日志吗data@priya是的,你可以告诉我该怎么做?我创建了一个名为AppEventLogger的类,它将数据记录在android应用程序中。它包含各种方法,如LoginLog(长id、长事件时间等)、LogoutLog(…)那么现在我需要如何将从这些方法获得的数据存储在json格式中,而不是创建一个全局数组,并将数据存储在该数组中,然后将其传递给JSONArray告诉我一件事,为什么要存储日志数据并与json协调?我使用这些日志数据进行分析是的,这很好,但你也可以通过在LogCat中显示日志来进行分析