如何在Android中以Json格式存储日志数据?

问题描述 投票:1回答:2

我正在android应用程序中记录事件。

现在我需要以JSON格式存储应用程序生成的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);
}
java android json logging
2个回答
2
投票

试试这个

班级变化:

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字符串在这里:

 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();
        }
    }

从您想要存储LOG数据的位置调用此方法

createJson("12343434");

输出:

{"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;
    }
}

MainActivity.java(调用方法的简单类)

public class StringToJs extends AppCompatActivity {

    private StringToJson stringToJson;
    private ArrayList jsonArray;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_string_to_js);

        jsonArray = new ArrayList();
        for (int i = 0; i < 10; i++) {
            jsonArray.add(i);
        }

        stringToJson = new StringToJson(StringToJs.this);

        Log.e("JSON: ", stringToJson.getJson("my_name", "Cricket", "23", "1", jsonArray));


    }
}

OUTPUT

{
    "name": "my_name",
    "sport": "Cricket",
    "age": "23",
    "id": "1",
    "lastScores": [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
}

-1
投票
    private void log(Context context,String data) {
        try {
            Log.i("info",data);
            OutputStreamWriter outputStreamWriter = new OutputStreamWriter(context.openFileOutput("log.txt", Context.MODE_PRIVATE));
            outputStreamWriter.append(data);
            outputStreamWriter.close();
        }
        catch (IOException e) {
            Log.e("Exception", "File write failed: " + e.toString());
        } 
    }
© www.soinside.com 2019 - 2024. All rights reserved.