读取php中从android发送的json数据

时间:2018-01-22 06:15:37

标签: php android

所以我的Android应用程序正在读取用户联系人列表,然后创建它的JSONObject并将该对象作为POST发送到我的PHP服务器,然后我的PHP服务器将读取发布的数据并将其存储在数据库中。到目前为止我的进步 -

Android部分 -

JSONArray obj = new JSONArray();    
private void getDetails(){
        try {
            Uri uri = ContactsContract.CommonDataKinds.Phone.CONTENT_URI;
            ContentResolver cr = getContentResolver();
            Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI,null, null, null, null);
            String[] projection    = new String[] {ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,
                    ContactsContract.CommonDataKinds.Phone.NUMBER };
            Cursor names = getContentResolver().query(uri, projection, null, null, null);
            int indexName = names.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME);
            int indexNumber = names.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);

            names.moveToFirst();
            do {
                String name   = names.getString(indexName);
                String number = names.getString(indexNumber);
                JSONObject cust= new JSONObject();
                cust.put("name",name);
                cust.put("mobile",number);

                obj.put(cust);
            } while (names.moveToNext());
        }
        catch (Exception e){
            e.printStackTrace();
        }
    }

               try {
                    HttpClient httpclient = new DefaultHttpClient();
                    ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
                    nameValuePairs.add(new BasicNameValuePair("contactList", obj.toString()));
                    HttpPost httppost = new HttpPost("http://doupnow.com/Demo/sendContact.php");
                    httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
                    HttpResponse response = httpclient.execute(httppost);
                    HttpEntity entity = response.getEntity();
                    Log.d("Server Response",entity.toString());
                }
                catch (Exception ee)
                {

                }

创建的JSON数据是 -

[{"mobile":"+1-703-349-3003","name":"Consultant Veshakptnam"},
 {"mobile":"+91 33 3057 0062","name":"Niit Subhashis"},
 {"mobile":"+91 33 3057 0074","name":"Vivek Rawat NIIT HO"}]

这是我的php页面 -

<?php 
    require_once('config.php');

    $data = json_decode($_POST["contactList"]);

    foreach ($data as $d1) 
    {
        $stmt = $linkID1->prepare("insert into demo set name=?,mobile=?");
        $stmt->bind_param("ss", $d1->name,$d1->mobile);
        $stmt->execute();
        $stmt->close();
    }
    mysqli_close($linkID1);

    echo 0;
?>

但是我的数据没有存储在mysql中,读取json数据有什么问题。请指导我。谢谢!!

2 个答案:

答案 0 :(得分:1)

问题不在于您如何读取json数据,而在于如何将其插入数据库。代替     $stmt = $linkID1->prepare("insert into demo set name=?,mobile=?");$stmt = $linkID1->prepare("insert into demo(name,mobile) values(?,?)");

答案 1 :(得分:-1)

您的PHP代码可能正在接收JSON,但作为Object,它必须作为要解码的JSON字符串接收。

请参阅this以了解您的理解。希望它能解决你的问题。