gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 20:07:55 24 4
gpt4 key购买 nike

我是 Android 开发新手,我想在 MySQL 数据库中插入通话记录详细信息。所以,从android端我得到一个 arrayList 并且我已经将该列表转换为 string[] 数组,但我无法在数据库中插入这个数组,我用 HashMap<String,Array> 插入整个数据。但 hashsmap 无法将数组争论视为 string[] 数组。请帮助解决这个问题,提前致谢

这是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() 显示无法从静态上下文引用非静态方法的错误。

最佳答案

首先创建一个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如何做到这一点。

关于java - 如何将string[]数组插入MySQL数据库?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40280395/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com