我需要将 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());
我是一名优秀的程序员,十分优秀!