我正在安装android应用程序中的事件。
现在我需要以 JSON 格式存储应用程序生成的php artisan migrate
数据。我该怎么做...对此有何建议?
这是使用Log
类
AppEventLogger
答案 0 :(得分: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();
}
}
}
Out Put:
{
"JsonData": [{
"context": "AppEventLogger",
"logData": " ID : 1 Result : true failedattempts : 11 attempts : 112 eventTime : mLastInteractionTime : 12"
}, {
"context": "AppEventLogger",
"logData": " eventTime : 1512374548115 isLoggedOut : false"
}]
}
旧答案
你的日志字符串在这里:
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));
}
}
<强>输出强>
{
"name": "my_name",
"sport": "Cricket",
"age": "23",
"id": "1",
"lastScores": [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
}
答案 1 :(得分:-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());
}
}