如何将string []数组插入MySQL数据库?

时间:2016-10-27 09:00:26

标签: java android mysql arrays

我是android开发的新手我想在MySQL数据库中插入调用日志详细信息。所以,从android方面我得到一个arrayList,我已将该列表转换为string []数组,但我无法在数据库中插入此数组,我将HashMap<String,Array>插入整个数据。但是hashsmap不能将数组争论作为string []数组。 plz提前感谢帮助解决这个问题

这是java代码..

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

}

这里我试图将string []数组转换为java.sql数组,但Connection.createArrayOf()显示无法从静态上下文引用非静态方法的错误。

1 个答案:

答案 0 :(得分:0)

首先创建一个POJO类来存储数据,

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
}

在while循环之前创建List并在循环内插入数据

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

使用GSON libraryArrayList转换为JSON

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

将此数据发布到服务器。请参阅here如何执行此操作。