Java 如何将Arraylist对象作为Namevaluepair传递给makehttprequest
我从Constractcontact表中获取每个人的联系人详细信息,并将所有联系人存储到arraylist对象中,该对象为Java 如何将Arraylist对象作为Namevaluepair传递给makehttprequest,java,android,mysql,arraylist,android-asynctask,Java,Android,Mysql,Arraylist,Android Asynctask,我从Constractcontact表中获取每个人的联系人详细信息,并将所有联系人存储到arraylist对象中,该对象为arraylist contacts=new arraylist()然后我把这个对象发送给Asynctask做后台工作,所以这些联系人存储到MySql数据库中,我有一个makehttprequest(………..)函数,它接受namevaluepair,但是我想把arraylist对象作为namevaluepair发送给makehttprequest,但是我不能这样做,请帮助我
arraylist contacts=new arraylist()
然后我把这个对象发送给Asynctask做后台工作,所以这些联系人存储到MySql数据库中,我有一个makehttprequest(………..)
函数,它接受namevaluepair,但是我想把arraylist对象作为namevaluepair发送给makehttprequest,但是我不能这样做,请帮助我做什么,我错在哪里
下面是我的代码:
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);
}
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");
}
}
此代码位于**活动的oncreate函数中**
ContentResolver cr=getContentResolver();
//SavingContacts SavingContacts=新的SavingContacts();
ArrayList联系人=新建ArrayList();
Cursor=getContentResolver().query(
ContactsContract.CommonDataTypes.Phone.CONTENT\u URI,null,
空,空,,
Contacts contract.Contacts.DISPLAY_NAME+“ASC”);
日志d(“数据库1”、“17”);
if(Cursor.getCount()>0){
while(Cursor.moveToNext()){
SavingContacts SavingContacts=新的SavingContacts();
phone=Cursor.getString(Cursor.getColumnIndex(ContactsContract.CommonDataTypes.phone.NUMBER));
如果(!TextUtils.isEmpty(电话)){
Log.d(“您的位置4”,“ok4:”);
name=Cursor.getString(Cursor.getColumnIndex(ContactsContract.CommonDataTypes.Phone.DISPLAY_name));
id=Cursor.getString(Cursor.getColumnIndex(ContactsContract.CommonDataTypes.Phone.CONTACT_id));
//photouri=Cursor.getString(Cursor.getColumnIndex(ContactsContract.CommonDataTypes.Photo.Photo));
email=Cursor.getString(Cursor.getColumnIndex(ContactsContract.CommonDataTypes.email.TYPE));
System.out.println(“contactId=“+id+”,name=“+name+”,phoneNumber=“+phone+”Email=“+Email”);
保存联系人。保存联系人(id、姓名、电话、电子邮件);
联系人。添加(保存联系人);
}
}
}
Cursor.close();
新建LoadSavingInDatabase().execute(联系人);
}
异步任务类:
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);
}
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");
}
}
公共类LoadSavingDatabase扩展异步任务{
私有静态最终字符串标记_SUCCESS=“SUCCESS”;
私有静态最终字符串URL=”http://amiranzur.com/android_connect/create_product.php";
JSONObject JSONObject=null;
@凌驾
受保护字符串doInBackground(ArrayList…param){
ArrayList contactArray=param[0];
**`在这一点上,请给出一些想法,我可以做些什么,这样我就可以将arraylist对象传递给makehttp函数`**
JSONObject JSONObject=new JSONParser().makeHttpRequest(URL,“POST”,ContactArray);//错误
if(jsonObject!=null){
试一试{
int success=jsonObject.getInt(TAG_success);
如果(成功==1){
Log.d(“创建”、“lpc”);
布尔=真;
日志d(“插入”、“真实”+bool);
}否则{
}
}捕获(JSONException e){
日志d(“例外”、“exc”+e);
Log.d(“创建”、“lpc”);
}
}
else if(jsonObject==null){
Log.d(“空”、“空1”);
布尔=假;
}
返回null;
}
受保护的void onPostExecute(布尔布尔布尔值){
if(bool==false)
Log.d(“插入失败”,“ID已插入”);
}
}
plaese是指我已经访问过这个站点,但是他们一个接一个地发送namevalue对,但是我想发送一个arraylist对象来发出HTTP请求