我是Android開發新手,我想在MySQL數據庫中插入通話記錄詳細信息,在這裏我創建了簡單的ArrayAdapter
,它沒有在listview中設置,第二件事是如何在MySQL服務器中插入數據。如何使用ArrayAdapter(Java)將數據插入MySQL服務器?
這裏是我的Java代碼
public class MainActivity extends Activity {
ListView listView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView = (ListView) 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;
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;
}
List<DataBean> DataBeanList = new ArrayList<DataBean>();
DataBean dataBean = new DataBean(number, type, fDate, duration);
DataBeanList.add(dataBean);
Log.d("tag", DataBeanList.toString());
}
managedCursor.close();
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,android.R.id.text1);
listView.setAdapter(arrayAdapter);
}
}
這裏是我的PHP腳本.........
<?php
//Importing our db connection script
require_once('connect.php');
if($_SERVER['REQUEST_METHOD']=='POST'){
//Getting values
$number = $_POST['number'];
$duration = $_POST['duration'];
$type = $_POST['type'];
$time = $_POST['time'];
//Creating an sql query
$sql = "INSERT INTO call_detail (number,duration,type,time) VALUES ('$number','$duration','$type','$time')";
//Executing query to database
if(mysqli_query($con,$sql)){
echo 'Entry Added Successfully';
}else{
echo 'Could Not Add Entry';
}
//Closing the database
mysqli_close($con);
}
所以基本上你想插入到MySQL表中?你有沒有用PHP編寫腳本來從java獲取數據? –
是的我已經在php中編寫了腳本....... @ helldawg –
對,所以你只是想要一種方法來實際上將數據從android發送到php文件,然後將它們插入到mysql中。這可以實現與httpUrlConnection併發出POST請求 –