我从Constractcontact表中获取每个人的联系人详细信息并将所有联系人存储到arraylist对象中ArrayList<SavingContacts> contacts = new ArrayList<SavingContacts>();
然后我将此对象发送到Asynctask进行后台工作,以便将这些联系人存储到MySql数据库中,I有一个makehttprequest(.............)
函数,它接受namevaluepair,但我想发送arraylist对象makehhtprequest作为namevalue对,但我不能这样做,请帮助我我能做什么,我错在哪里,
以下是我的代码:
This code is in oncreate function of **activity**
ContentResolver cr = getContentResolver();
// SavingContacts savingcontacts=new SavingContacts();
ArrayList<SavingContacts> contacts = new ArrayList<SavingContacts>();
Cursor = getContentResolver().query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
null, null,
ContactsContract.Contacts.DISPLAY_NAME + " ASC");
Log.d("database1" ,"17");
if (Cursor.getCount() > 0) {
while (Cursor.moveToNext()) {
SavingContacts savingcontacts=new SavingContacts();
phone = Cursor.getString(Cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
if (!TextUtils.isEmpty(phone)) {
Log.d("Your Location4", "ok4:");
name = Cursor.getString(Cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
id = Cursor.getString(Cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.CONTACT_ID));
// photouri=Cursor.getString(Cursor.getColumnIndex(ContactsContract.CommonDataKinds.Photo.PHOTO));
email=Cursor.getString(Cursor.getColumnIndex(ContactsContract.CommonDataKinds.Email.TYPE));
System.out.println("contactId="+ id+ ", name=" + name + ", phoneNumber=" + phone+"Email="+email);
savingcontacts.savingcontact(id, name, phone, email);
contacts.add(savingcontacts);
}
}
}
Cursor.close();
new LoadSavingInDatabase().execute(contacts);
}
AsyncTask类:
public class LoadSavingInDatabase extends AsyncTask<ArrayList<SavingContacts>,String,String>{
private static final String TAG_SUCCESS = "success";
private static final String URL = "http://amiranzur.com/android_connect/create_product.php";
JSONObject jsonObject= null;
@Override
protected String doInBackground(ArrayList<SavingContacts>... param) {
ArrayList<SavingContacts> contactArray = param[0];
**`Please give some idea at this point what can I do so I can pass arraylist object to makehttp function.`**
JSONObject jsonObject= new JSONParser().makeHttpRequest(URL, "POST", ContactArray);// error
if(jsonObject != null){
try {
int success = jsonObject.getInt(TAG_SUCCESS);
if (success == 1) {
Log.d("create","lpc");
bool = true;
Log.d("insert","true" + bool);
} else {
}
} catch (JSONException e) {
Log.d("exception","exc "+e);
Log.d("create","lpc");
}
}
else if(jsonObject == null){
Log.d("null", "null1");
bool = false;
}
return null;
}
protected void onPostExecute(boolean bool){
if(bool == false)
Log.d("Insertion failed", "ID already inserted");
}
}