gpt4 book ai didi

java - 如何在android中将单个字符串转换为JsonArray?

转载 作者:太空宇宙 更新时间:2023-11-04 12:00:37 28 4
gpt4 key购买 nike

我需要将 String[] 转换为 JsonArray,但我不知道如何转换。我是 Android 开发新手,我想在 MySQL 数据库中插入通话记录详细信息。所以,从 android 端我得到一个字符串,但我不知道如何将该字符串转换为 Jsonarray。请帮助解决这个问题,提前致谢

这是我的java代码......

public class MainActivity extends Activity {
TextView textView;

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

textView = (TextView) findViewById(R.id.lv);
getCallDetails();
}

private void getCallDetails()
{
StringBuffer sb = new StringBuffer();
String strOrder = android.provider.CallLog.Calls.DATE + " DESC";

Cursor managedCursor = managedQuery(CallLog.Calls.CONTENT_URI, null,null, null, strOrder);
int number1 = managedCursor.getColumnIndex(CallLog.Calls.NUMBER);
int type1 = managedCursor.getColumnIndex(CallLog.Calls.TYPE);
int duration1 = managedCursor.getColumnIndex(CallLog.Calls.DURATION);

sb.append("Call Log :");

while (managedCursor.moveToNext())
{
final String number = managedCursor.getString(number1);
final String type2 = managedCursor.getString(type1);
final String date = managedCursor.getString(managedCursor.getColumnIndexOrThrow("date")).toString();
java.util.Date date1 = new java.util.Date(Long.valueOf(date));
final String duration = managedCursor.getString(duration1);
String type = null;

Log.e("abc",date.toString());
Log.e("abc",date1.toString());

final String fDate = date1.toString();


int callcode = Integer.parseInt(type2);
switch (callcode)
{
case CallLog.Calls.OUTGOING_TYPE:
type = "Outgoing";
break;
case CallLog.Calls.INCOMING_TYPE:
type = "Incoming";
break;
case CallLog.Calls.MISSED_TYPE:
type = "Missed";
break;
}
sb.append("\nPhone Number:--- " + number + "");
sb.append(" \nCall Type:--- " + type + " ");
sb.append("\nCall Date:--- " + date1 + "");
sb.append ("\nCall duration in sec :--- " + duration);
sb.append("\n----------------------------------");

class getCallDetails extends AsyncTask<Void,Void,String>
{
@Override
protected String doInBackground(Void... params)
{
HashMap<String,String> param = new HashMap<String, String>();
param.put(Connect.KEY_NUMBER,number);
param.put(Connect.KEY_TYPE,type2);
param.put(Connect.KEY_DATE,fDate);
param.put(Connect.KEY_DURATION,duration);

RequestHandler rh = new RequestHandler();
String res = rh.sendPostRequest(Connect.URL_ADD, param);
return res;
}

}

getCallDetails idata = new getCallDetails();
idata.execute();
}
managedCursor.close();
textView.setText(sb);

}
}

最佳答案

试试这个,

// Create JSONArray
JSONArray jArray = new JSONArray();
while (managedCursor.moveToNext())
{
final String number = managedCursor.getString(number1);
final String type2 = managedCursor.getString(type1);
final String date = managedCursor.getString(managedCursor.getColumnIndexOrThrow("date")).toString();
Date date1 = new Date(Long.valueOf(date));
final String fDate = date1.toString();
final String duration = managedCursor.getString(duration1);
String type = null;

// Create JSONObject
JSONObject item = new JSONObject();

// add the items to JSONObject
item.put("number", number);
item.put("type2", type2);
item.put("fDate", fDate);
item.put("duration", duration);

// add the JSONObject to JSONArray
jArray.put(item);
}
managedCursor.close();

System.out.println(jArray.toString());

关于java - 如何在android中将单个字符串转换为JsonArray?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40948014/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com