0

I am new in android development i want to insert call log details in MySQL database. so, from android side i am getting an arrayList and i have converted that list into string[] array but i am not able to insert this array in database here i am insert the whole data with HashMap<String,Array>. but hashsmap is not able to take array arguement as string[] array. plz help to sort out this problem thanks in advance

here is java code..

public class MainActivity extends AppCompatActivity {
ArrayList<String> arrayList;
String phNum,callType,samay,callDuration;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    ListView animalList=(ListView)findViewById(R.id.listView);

    arrayList = new ArrayList<String>();

    getCallDetails();
    // Create The Adapter with passing ArrayList as 3rd parameter
    ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, arrayList);
    // Set The Adapter
    animalList.setAdapter(arrayAdapter);
}

private void getCallDetails() {
    String strOrder = android.provider.CallLog.Calls.DATE + " DESC";
 /* Query the CallLog Content Provider */
    Cursor managedCursor = managedQuery(CallLog.Calls.CONTENT_URI, null,
            null, null, strOrder);
    int number = managedCursor.getColumnIndex(CallLog.Calls.NUMBER);
    int type = managedCursor.getColumnIndex(CallLog.Calls.TYPE);
    int date = managedCursor.getColumnIndex(CallLog.Calls.DATE);
    int duration = managedCursor.getColumnIndex(CallLog.Calls.DURATION);
    while (managedCursor.moveToNext()) {
        phNum = managedCursor.getString(number);
        String callTypeCode = managedCursor.getString(type);
        String strcallDate = managedCursor.getString(date);
        Date callDate = new Date(Long.valueOf(strcallDate));
         samay = callDate.toString();
         callDuration = managedCursor.getString(duration);
         callType = null;
        int callcode = Integer.parseInt(callTypeCode);
        switch (callcode) {
            case CallLog.Calls.OUTGOING_TYPE:
                callType = "Outgoing";
                break;
            case CallLog.Calls.INCOMING_TYPE:
                callType = "Incoming";
                break;
            case CallLog.Calls.MISSED_TYPE:
                callType = "Missed";
                break;
        }
        arrayList.add(phNum);
        arrayList.add(callDuration);
        arrayList.add(callType);
        arrayList.add(samay);
    }
    managedCursor.close();
    /*String[] array = new String[arrayList.size()];
    array = arrayList.toArray(array);

    for(String s : array)
    {Log.d("TAG",s);}*/
    final String[] data = arrayList.toArray(new String[arrayList.size()]);
    final java.sql.Array sqlArray = Connection.createArrayOf("VARCHAR", data);

    class getCallDetails extends AsyncTask<Void, Void, String> {
        @Override
        protected String doInBackground(Void... params) {
            HashMap<String, Array> param = new HashMap<String, Array>();
            param.put(Connect.KEY_ARRAY, sqlArray );

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

}

here i have tried to convert string[] array into java.sql array but Connection.createArrayOf() shows error of non-static method can not be referenced from a static context.

4
  • Send the data as JSON to your web service and insert it in MySQL. Commented Oct 27, 2016 at 9:15
  • thanks for your response @KNeerajLal will u plz explain how can i store while loop data in json??? Commented Oct 27, 2016 at 9:19
  • Not sure if relevant, but it could help a little bit: stackoverflow.com/questions/25582199/… Commented Oct 27, 2016 at 9:39
  • @volcano_Secret Did you check the answer? Commented Nov 9, 2016 at 9:26

1 Answer 1

0

First create a POJO class to store the data,

private class ContactData {
    String phNum;
    String callDuration;
    String callType;
    String samay;

    public ContactData(String phNum, String callDuration, String callType, String samay) {
        this.phNum = phNum;
        this.callDuration = callDuration;
        this.callType = callType;
        this.samay = samay;
    }

    // getters and setters
}

Create a List before the while loop and insert data into this inside the loop,

List<ContactData> items = new ArrayList<String>();
while (managedCursor.moveToNext()) {
    ...
    items.add(new ContactData(phNum, callDuration, callType, samay));
}

Use GSON library to convert ArrayList to JSON.

String listOfItems = new Gson().toJson(items);

Post this data to server. See here how to do this.

Sign up to request clarification or add additional context in comments.

Comments

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.