私の電話から連絡先を取得しようとしていますが、データをPHPサービスに投稿してデータベースに保存したいと考えています。検索した後、私は名前と値のペアを持つ配列を作成し、スニペットJSONArrayをPHPに投稿する
public void getContacts() throws IOException, JSONException {
List<String> phnnumbers = new ArrayList<String>();
List<String> names = new ArrayList<String>();
ContentResolver cr = getContentResolver();
Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI,
null, null, null, null);
if (cur.getCount() > 0) {
while (cur.moveToNext()) {
String id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
String name = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
if (Integer.parseInt(cur.getString(cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
System.out.println("name : " + name + ", ID : " + id);
names.add(name);
// get the phone number
Cursor pCur = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?",
new String[]{id}, null);
while (pCur.moveToNext()) {
String phone = pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
System.out.println("phone" + phone);
phnnumbers.add(phone);
}
}
}
Log.d("NAMES",name.toString());
// registerContacts(this,URL,names,phnnumbers);
}
}
は、出力は、私はデータベースに格納することができるようにどのように私はこれをフォーマットします。この
[name[]="Chacha Kori, phone[]="+123456987", name[]="Gabuli Somi", [phone[]="+123456789",name[]="Geto Somi", phone[]="+123456789",
のようなものです。私のPHP側
function SynchContacts() {
$phone = $this->input->post('phone');
$name = $this->input->post('name');
for ($i = 0; $i < count($phone); $i++) {
$data = array(
'name' => $name[$i],
'phone' => $phone[$i]
);
$this->saveData('phonebook', $data);
}
}
ご意見はありますか? PHP側の
@BクマールdこのメソッドはsanitizeJSONBody();を実行します。 – Alphy
私はsanitizeJSONBody()のメソッドの実装を追加しました;実際にはこのメソッドはLog.vにリクエストとレスポンスを表示するために使用されます(セキュリティ上の理由からパスワードフィールドがあれば***を置き換えます)。 – Bethan
@B Kumar、これまでのところ良くなっていますが、ボディの出力を見ることができますが、{"phone": "[+ 123、+ 456、789、 +456、789] "}、理由は何でしょうか? – Alphy