繁体   English   中英

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

[英]read json data sent from android in php

因此,我的Android应用正在读取用户联系人列表,然后为其创建一个JSONObject并将该对象作为POST发送到我的PHP Server,然后我的PHP Server将读取发布的数据并将其存储在数据库中。 到目前为止,我的进步-

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数据是否有任何问题。 请指导我。谢谢!

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

您的PHP代码可能正在接收JSON,但作为对象,必须将其作为JSON字符串接收才能进行解码。

请参阅此内容以了解您的情况。 希望它能解决您的问题。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM