1

I need to convert a String[] to an JsonArray and I don't know how. I am new in android development i want to insert call log details in MySQL database. so, from android side i am getting an string and but I don't know how convert that string into Jsonarray. plz help to sort out this problem thanks in advance

Here is my java code.......

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

    }
  }
1

2 Answers 2

0

Try this,

// 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());
Sign up to request clarification or add additional context in comments.

3 Comments

Only you can do that. I have shown you the way. Understand how your API accepts the data and create your data according to it. Good luck. :)
Handle the exception using a try-catch block.
@Neeraj after handling exception application is unfortunately stopped plz helpfor sort out this problem
0

It is easy.You can use one of the constructors of JSONArray class.

JSONArray jArr = new JSONArray(strArr)

where strArr is your String array.

More here: https://developer.android.com/reference/org/json/JSONArray.html#JSONArray(java.lang.Object)

3 Comments

Can you say how to make String Array in my java code ?
Hey @Secret_Volcano, accept the answer if it has solved your problem. :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.