小编典典

如何在Android中将单个字符串转换为JsonArray?

java

我需要将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);

    }
  }

阅读 257

收藏
2020-11-30

共1个答案

小编典典

试试这个,

// 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());
2020-11-30