我试图将联系人详细信息插入MySQL数据库,
这是我的代码:
public void SyncContact(){
ContentResolver cr = getContentResolver();
Cursor phones = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, null);
JSONArray ja = new JSONArray();
HashMap<String,String> hashmap_contactList = new HashMap<String, String>();
String id="";
String name="";
String mobileNo="";
String emailContact="";
String orgName="";
String title="";
String note="";
String street="";
String dob="";
int i = 0;
while (phones.moveToNext()) {
id = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.CONTACT_ID));
name = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
mobileNo = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
Cursor emailCur = cr.query(ContactsContract.CommonDataKinds.Email.CONTENT_URI, null, ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = ?", new String[]{id}, null);
if(emailCur.getCount()>0) {
while (emailCur.moveToNext()) {
emailContact = emailCur.getString(emailCur.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
String emailType = emailCur.getString(emailCur.getColumnIndex(ContactsContract.CommonDataKinds.Email.TYPE));
}
}else{
emailContact="";
}
emailCur.close();
String orgWhere = ContactsContract.Data.CONTACT_ID + " = ? AND " + ContactsContract.Data.MIMETYPE + " = ?";
String[] orgWhereParams = new String[]{id,ContactsContract.CommonDataKinds.Organization.CONTENT_ITEM_TYPE};
Cursor orgCur = cr.query(ContactsContract.Data.CONTENT_URI,null, orgWhere, orgWhereParams, null);
if(orgCur.getCount()>0) {
if (orgCur.moveToFirst()) {
orgName = orgCur.getString(orgCur.getColumnIndex(ContactsContract.CommonDataKinds.Organization.DATA));
title = orgCur.getString(orgCur.getColumnIndex(ContactsContract.CommonDataKinds.Organization.TITLE));
}
}else{
orgName="";
title="";
}
orgCur.close();
String[] noteWhereParams = new String[]{id,ContactsContract.CommonDataKinds.Note.CONTENT_ITEM_TYPE};
Cursor noteCur = cr.query(ContactsContract.Data.CONTENT_URI, null, orgWhere, noteWhereParams, null);
if(noteCur.getCount()>0) {
if (noteCur.moveToFirst()) {
note = noteCur.getString(noteCur.getColumnIndex(ContactsContract.CommonDataKinds.Note.NOTE));
}
}else{
note="";
}
noteCur.close();
String[] addrWhereParams = new String[]{id,ContactsContract.CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE};
Cursor addrCur = cr.query(ContactsContract.Data.CONTENT_URI,null, orgWhere, addrWhereParams, null);
if(addrCur.getCount()>0){
while(addrCur.moveToNext()) {
street = addrCur.getString(addrCur.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.STREET));
}
}else {
street="";
}
addrCur.close();
String[] selectionArgs = new String[] {id,ContactsContract.CommonDataKinds.Event.CONTENT_ITEM_TYPE};
Cursor dobCur = cr.query(ContactsContract.Data.CONTENT_URI, null, orgWhere, selectionArgs, null);
if(dobCur.getCount()>0) {
if (dobCur.moveToFirst()) {
dob = dobCur.getString(dobCur.getColumnIndex(ContactsContract.CommonDataKinds.Event.DATA));
}
}else {
dob="";
}
dobCur.close();
if(!hashmap_contactList.containsKey(name)){
hashmap_contactList.put(name, " ");
System.out.println("!! Contact ID is : " + id);
System.out.println("!! Contact Name is : " + name);
System.out.println("!! Contact Number is : " + mobileNo);
System.out.println("!!prepare Email " + emailContact );
System.out.println("!!prepare Comapny name:" + orgName);
System.out.println("!!prepare Designation :" + title);
System.out.println("!!prepare Note :" + note);
System.out.println("!!prepare Street:" + street);
System.out.println("!!hello dob :" + dob);
try{
JSONObject jsonObject = new JSONObject();
jsonObject.put("contact_uniqueid",id);
jsonObject.put("contact_name",name);
jsonObject.put("contact_number",mobileNo);
jsonObject.put("email",emailContact);
jsonObject.put("oraganization",orgName);
jsonObject.put("job_title",title);
jsonObject.put("address",street);
jsonObject.put("note",note);
jsonObject.put("dob",dob);
ja.put(jsonObject);
mainjson.put("data",ja);
}catch (Exception e){
e.printStackTrace();
}
i++;
}
}
phones.close();
System.out.println("!!json " + mainjson);
try {
Log.d("!!!main_json", mainjson.toString(1));
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
ContactSync contactSync = new ContactSync();
contactSync.execute();
}
public class ContactSync extends AsyncTask<String,Void,String>{
String JsonString;
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected String doInBackground(String... params) {
HttpPost post = new HttpPost(str_url);
try{
StringEntity entity = new StringEntity(mainjson.toString());
post.setEntity(entity);
post.setHeader("Accept", "application/json");
post.setHeader("Content-type", "application/json");
DefaultHttpClient client = new DefaultHttpClient();
BasicResponseHandler handler = new BasicResponseHandler();
String response = client.execute(post, handler);
System.out.println("!!Response : " + response);
JSONObject jsonObject = new JSONObject(response);
System.out.println("!!Response " + jsonObject);
System.out.println("!! " + jsonObject.getString("data"));
JSONObject success_status = jsonObject.getJSONObject("data");
System.out.println("!!Succes MSg :" + success_status.getString("success"));
if(success_status.getString("success").toString().equalsIgnoreCase("true"))
{
System.out.println("Response after data inserted....."+success_status.getString("success").toString());
CommonFunction.saveSharedPreference(CommonFunction.contactsync_flag, "1", SplashActivity.this);
}
}catch (Exception e){
e.printStackTrace();
System.out.println("!! : "+e.getMessage());
System.out.println("!!!!!!manishhhhhhh......" );
}
return null;
}
protected void onPostExecute(String s) {
super.onPostExecute(s);
}
API代码为:
public function temp(){
$_POST = json_decode(file_get_contents('php://input'), true);
$this->load->library('form_validation');
$boolean = TRUE;
$data = array();
$contact_data = $_POST['data'];
$i=0;
foreach($contact_data AS $row){
$fields['contact_uniqueid']=$row['contact_uniqueid'];
$fields['contact_name']=$row['contact_name'];
$fields['contact_number']=$row['contact_number'];
$fields['email']=$row['email'];
$fields['oraganization']=$row['oraganization'];
$fields['job_title']=$row['job_title'];
$fields['address']=$row['address'];
$fields['note']=$row['note'];
$fields['dob']=$row['dob'];
$data[$i] = $fields;
$i++;
}
$this->express_model->set_table('contact_list');
$data_obj = $this->express_model->saveBatch($data);
if ($boolean == TRUE)
{
$final_data['data']['success'] = 'true';
$final_data['data']['message'] = 'Data inserted successfully';
print_r(json_encode($final_data));
}
exit;
}
所有联系人详细信息都会插入到数据库中,我必须得到&#34; true&#34;在JSON响应中但我收到此错误:
<div style="border:1px solid #990000;padding-left:20px;margin:0 0 10px 0;">
我使用用codeigniter编写的PHP API。
答案 0 :(得分:0)
将数据类型检查到数据库模式中并尝试插入适当的值..
当我们对字段
使用DateTime数据类型时,通常会发生这种情况