My PHP service should receive contacts from Android phones. How can I insert this data into a server database?
I'm getting an error while sending data as an array to my remote database. What did I wrong?
public class PhoneBookActivity extends Activity {
ListView listViewPhoneBook;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.phone_book);
listViewPhoneBook = (ListView) findViewById(R.id.listPhoneBook);
Cursor cursor = getContentResolver().query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
new String[] {Phone._ID, Phone.DISPLAY_NAME, Phone.NUMBER},
null, null, null);
startManagingCursor(cursor);
// Cursor phones = getContentResolver().query(
// ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
// null,
// ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = " + contactId,
// null, null);
String[] arrayColumns = new String[]{ Phone.DISPLAY_NAME, Phone.NUMBER, Phone._ID };
int[] arrayViewID = new int[]{ R.id.name , R.id.number, R.id.id };
SimpleCursorAdapter adapter = new SimpleCursorAdapter(
this, R.layout.each_contact, cursor, arrayColumns, arrayViewID);
listViewPhoneBook.setAdapter(adapter);
JSONArray mJSONArray = new JSONArray(Arrays.asList(arrayColumns));
send(mJSONArray);
}
private void send(JSONArray mJSONArray) {
try {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://www.example.org/save_contacts.php?");
httppost.setEntity(new UrlEncodedFormEntity(mJSONArray));
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
result = co(is);
}
...
Build it into a JSON response in a format you find easy to understand. Then send it to a webservice that you'll need to write that parses the JSON and adds it to the database. Look at the JSONObject API in the android docs or the GSON library for ways to form JSON easily.