-1

我的 PHP 服务应该接收来自 Android 手机的联系人。如何将此数据插入服务器数据库?

将数据作为数组发送到远程数据库时出现错误。我做错了什么?

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);
        }
        ...
4

1 回答 1

1

以您容易理解的格式将其构建为 JSON 响应。然后将其发送到您需要编写的 Web 服务,该服务会解析 JSON 并将其添加到数据库中。查看 android 文档或 GSON 库中的 JSONObject API 以了解轻松形成 JSON 的方法。

于 2013-02-25T16:23:45.053 回答