将数据从Android应用程序发送到PHP服务器

时间:2015-04-15 03:22:51

标签: php android

这是我的android代码,它正常执行我调试它并说发送数据。

public void postRegistrationData(final String result) {
        // Create a new HttpClient and Post Header
        Thread thread = new Thread(new Runnable(){
            @Override
            public void run() {
                try {
                    try {
                        URL url;
                        HttpURLConnection urlConn;

                        url = new URL ("http://192.168.*.**/server/mypage.php");
                        urlConn = (HttpURLConnection)url.openConnection();
                        urlConn.setDoInput (true);
                        urlConn.setDoOutput (true);
                        urlConn.setUseCaches (false);
                        urlConn.setRequestProperty("Content-Type","application/json");   
                        urlConn.setRequestProperty("Accept", "application/json");
                        urlConn.setRequestMethod("POST");
                        urlConn.connect();  
                        //Create JSONObject here
                        JSONObject jsonParam = new JSONObject();
                        jsonParam.put(result, true);

                        // Send POST output.
                        /*printout = new DataOutputStream(urlConn.getOutputStream ());
                        printout.writeUTF(URLEncoder.encode(jsonParam.toString(),"UTF-8"));
                        Log.i("NOTIFICATION", "Data Sent");
                        printout.flush ();
                        printout.close ();*/
                        OutputStreamWriter os = new OutputStreamWriter(urlConn.getOutputStream(), "UTF-8");
                        os.write(jsonParam.toString());
                        Log.i("NOTIFICATION", "Data Sent");
                        os.close();

                    } catch (MalformedURLException muex) {
                        // TODO Auto-generated catch block
                        muex.printStackTrace();
                    } catch (IOException ioex){
                        ioex.printStackTrace();
                    } catch (JSONException jex) {
                        // TODO Auto-generated catch block
                        jex.printStackTrace();
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                    Log.e("ERROR", "There is error in this code");
                }
            }
        });

        thread.start(); 
    }

这是服务器(XAMPP)端PHP代码,它在我运行时正确插入数据,但不是来自我手机上的Android应用程序。我在本网站和其他网站上阅读了所有相似的回复,但他们的解决方案在过去的两天里对我没有用,并决定看看是否有人可以帮助我。非常需要和赞赏你的帮助。

<?php
        header('Content-type: application/json');
            // connect to the database
            $servername = "localhost";
            $username = "root";
            $password = "password";
            $dbname = "db"; 
            $received = json_decode(stripslashes($_POST['json']));
            // if text data was posted
           if(isset($received['result'])){
            //print_r($_POST);
            try {
                $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
                // set the PDO error mode to exception
                $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
                //$data   = urldecode($_POST['regid']);
                $sql = "INSERT INTO table (regID) VALUES ('$received')";
                // use exec() because no results are returned
                $conn->exec($sql);
                echo "New record created successfully";
            } catch (PDOException $e) {
                echo $sql . "<br>" . $e->getMessage();
            }
            $conn = null;
            }elseif ($received === NULL) {
                echo "Not value passed!!!!!!!!!!!!";
    }
        ?>

这是logcat日志。

04-14 21:08:44.464: D/dalvikvm(24167): GC_EXPLICIT freed 1544K, 27%
    free 30752K/42088K, paused 3ms+8ms, total 75ms 04-14 21:08:44.894:
    D/dalvikvm(24167): GC_EXPLICIT freed 1920K, 28% free 30464K/42088K,
    paused 5ms+7ms, total 71ms 04-14 21:08:45.234: D/dalvikvm(24167):
    GC_EXPLICIT freed 1667K, 28% free 30456K/42088K, paused 3ms+7ms,
    total 56ms 04-14 21:08:45.564: D/dalvikvm(24167): GC_EXPLICIT freed
    1650K, 28% free 30455K/42088K, paused 4ms+6ms, total 57ms 04-14
    21:08:45.984: D/dalvikvm(24167): GC_EXPLICIT freed 1654K, 28% free
    30439K/42088K, paused 5ms+8ms, total 69ms 04-14 21:08:46.414:
    D/dalvikvm(24167): GC_EXPLICIT freed 1665K, 28% free 30441K/42088K,
    paused 4ms+6ms, total 68ms 04-14 21:08:46.764: D/dalvikvm(24167):
    GC_EXPLICIT freed 1638K, 28% free 30440K/42088K, paused 5ms+5ms,
    total 61ms 04-14 21:08:47.074: D/dalvikvm(24167): GC_EXPLICIT freed
    1576K, 28% free 30454K/42088K, paused 3ms+6ms, total 56ms 04-14
    21:08:47.504: D/dalvikvm(24167): GC_EXPLICIT freed 1661K, 28% free
    30456K/42088K, paused 4ms+8ms, total 59ms 04-14 21:08:47.874:
    D/dalvikvm(24167): GC_EXPLICIT freed 1381K, 28% free 30439K/42088K,
    paused 4ms+6ms, total 58ms 04-14 21:08:48.224: D/dalvikvm(24167):
    GC_EXPLICIT freed 1542K, 28% free 30440K/42088K, paused 5ms+6ms,
    total 61ms 04-14 21:08:48.534: D/dalvikvm(24167): GC_EXPLICIT freed
    1420K, 28% free 30454K/42088K, paused 5ms+6ms, total 55ms 04-14
    21:08:48.904: D/dalvikvm(24167): GC_EXPLICIT freed 1516K, 28% free
    30454K/42088K, paused 4ms+6ms, total 58ms 04-14 21:08:49.274:
    D/dalvikvm(24167): GC_EXPLICIT freed 1450K, 28% free 30438K/42088K,
    paused 3ms+6ms, total 69ms 04-14 21:08:49.564: D/dalvikvm(24167):
    GC_EXPLICIT freed 1376K, 28% free 30454K/42088K, paused 5ms+6ms,
    total 56ms 04-14 21:08:49.624: I/NOTIFICATION(24167): Data Sent
    04-14 21:08:49.994: D/dalvikvm(24167): GC_EXPLICIT freed 1803K, 28%
    free 30468K/42088K, paused 5ms+6ms, total 68ms 04-14 21:08:50.344:
    D/dalvikvm(24167): GC_EXPLICIT freed 1442K, 28% free 30459K/42088K,
    paused 6ms+5ms, total 65ms 04-14 21:08:50.604: D/dalvikvm(24167):
    GC_EXPLICIT freed 1349K, 28% free 30444K/42088K, paused 3ms+5ms,
    total 57ms 04-14 21:08:50.834: D/dalvikvm(24167): GC_EXPLICIT freed
    1207K, 28% free 30444K/42088K, paused 5ms+6ms, total 53ms 04-14
    21:08:51.104: D/dalvikvm(24167): GC_EXPLICIT freed 1265K, 28% free
    30449K/42088K, paused 3ms+6ms, total 68ms 04-14 21:08:51.374:
    D/dalvikvm(24167): GC_EXPLICIT freed 1326K, 28% free 30461K/42088K,
    paused 3ms+5ms, total 52ms 04-14 21:08:51.634: D/dalvikvm(24167):
    GC_EXPLICIT freed 1337K, 28% free 30458K/42088K, paused 3ms+5ms,
    total 51ms 04-14 21:08:51.864: D/dalvikvm(24167): GC_EXPLICIT freed
    1224K, 28% free 30443K/42088K, paused 3ms+5ms, total 51ms 04-14
    21:08:52.114: D/dalvikvm(24167): GC_EXPLICIT freed 1275K, 28% free
    30458K/42088K, paused 10ms+6ms, total 60ms 04-14 21:08:52.354:
    D/dalvikvm(24167): GC_EXPLICIT freed 1267K, 28% free 30462K/42088K,
    paused 4ms+6ms, total 53ms 04-14 21:08:52.614: D/dalvikvm(24167):
    GC_EXPLICIT freed 1272K, 28% free 30459K/42088K, paused 3ms+6ms,
    total 52ms 04-14 21:08:52.824: D/dalvikvm(24167): GC_EXPLICIT freed
    1219K, 28% free 30445K/42088K, paused 4ms+6ms, total 54ms 04-14
    21:08:53.134: D/dalvikvm(24167): GC_EXPLICIT freed 1327K, 28% free
    30461K/42088K, paused 5ms+13ms, total 86ms 04-14 21:08:53.394:
    D/dalvikvm(24167): GC_EXPLICIT freed 1278K, 28% free 30458K/42088K,
    paused 5ms+6ms, total 54ms 04-14 21:08:53.644: D/dalvikvm(24167):
    GC_EXPLICIT freed 1288K, 28% free 30443K/42088K, paused 3ms+5ms,
    total 51ms 04-14 21:08:53.884: D/dalvikvm(24167): GC_EXPLICIT freed
    1206K, 28% free 30444K/42088K, paused 4ms+5ms, total 54ms 04-14
    21:08:54.184: D/dalvikvm(24167): GC_EXPLICIT freed 1397K, 28% free
    30443K/42088K, paused 3ms+6ms, total 51ms 04-14 21:08:54.414:
    D/dalvikvm(24167): GC_EXPLICIT freed 1208K, 28% free 30458K/42088K,
    paused 5ms+5ms, total 54ms 04-14 21:08:54.694: D/dalvikvm(24167):
    GC_EXPLICIT freed 1335K, 28% free 30458K/42088K, paused 3ms+6ms,
    total 52ms 04-14 21:08:54.894: D/dalvikvm(24167): GC_EXPLICIT freed
    1219K, 28% free 30445K/42088K, paused 4ms+6ms, total 52ms 04-14
    21:08:55.114: D/dalvikvm(24167): GC_EXPLICIT freed 1184K, 28% free
    30448K/42088K, paused 4ms+8ms, total 64ms 04-14 21:08:55.334:
    D/dalvikvm(24167): GC_EXPLICIT freed 1210K, 28% free 30460K/42088K,
    paused 3ms+6ms, total 51ms 04-14 21:08:55.594: D/dalvikvm(24167):
    GC_EXPLICIT freed 1272K, 28% free 30460K/42088K, paused 3ms+6ms,
    total 54ms 04-14 21:08:55.874: D/dalvikvm(24167): GC_EXPLICIT freed
    1334K, 28% free 30447K/42088K, paused 3ms+6ms, total 53ms 04-14
    21:08:56.124: D/dalvikvm(24167): GC_EXPLICIT freed 1321K, 28% free
    30448K/42088K, paused 4ms+5ms, total 53ms 04-14 21:08:56.384:
    D/dalvikvm(24167): GC_EXPLICIT freed 1280K, 28% free 30459K/42088K,
    paused 5ms+5ms, total 53ms 04-14 21:08:56.634: D/dalvikvm(24167):
    GC_EXPLICIT freed 1328K, 28% free 30460K/42088K, paused 3ms+6ms,
    total 56ms 04-14 21:08:56.834: D/dalvikvm(24167): GC_EXPLICIT freed
    1162K, 28% free 30443K/42088K, paused 3ms+6ms, total 52ms 04-14
    21:08:57.064: D/dalvikvm(24167): GC_EXPLICIT freed 1259K, 28% free
    30445K/42088K, paused 3ms+6ms, total 53ms 04-14 21:08:57.354:
    D/dalvikvm(24167): GC_EXPLICIT freed 1296K, 28% free 30445K/42088K,
    paused 3ms+6ms, total 52ms 04-14 21:08:57.654: D/dalvikvm(24167):
    GC_EXPLICIT freed 1280K, 28% free 30458K/42088K, paused 3ms+6ms,
    total 55ms 04-14 21:08:57.964: D/dalvikvm(24167): GC_EXPLICIT freed
    1333K, 28% free 30459K/42088K, paused 4ms+5ms, total 51ms 04-14
    21:08:58.234: D/dalvikvm(24167): GC_EXPLICIT freed 1346K, 28% free
    30447K/42088K, paused 8ms+6ms, total 58ms 04-14 21:08:58.484:
    D/dalvikvm(24167): GC_EXPLICIT freed 1268K, 28% free 30447K/42088K,
    paused 5ms+6ms, total 57ms 04-14 21:08:58.784: D/dalvikvm(24167):
    GC_EXPLICIT freed 1341K, 28% free 30458K/42088K, paused 3ms+5ms,
    total 55ms 04-14 21:08:59.034: D/dalvikvm(24167): GC_EXPLICIT freed
    1272K, 28% free 30458K/42088K, paused 4ms+6ms, total 55ms 04-14
    21:08:59.354: D/dalvikvm(24167): GC_EXPLICIT freed 1405K, 28% free
    30445K/42088K, paused 4ms+6ms, total 55ms 04-14 21:08:59.604:
    D/dalvikvm(24167): GC_EXPLICIT freed 1257K, 28% free 30445K/42088K,
    paused 5ms+7ms, total 56ms 04-14 21:08:59.864: D/dalvikvm(24167):
    GC_EXPLICIT freed 1270K, 28% free 30460K/42088K, paused 4ms+7ms,
    total 58ms 04-14 21:09:00.184: D/dalvikvm(24167): GC_EXPLICIT freed
    1396K, 28% free 30460K/42088K, paused 5ms+6ms, total 57ms 04-14
    21:09:00.414: D/dalvikvm(24167): GC_EXPLICIT freed 1225K, 28% free
    30443K/42088K, paused 5ms+6ms, total 61ms 04-14 21:09:00.654:
    D/dalvikvm(24167): GC_EXPLICIT freed 1272K, 28% free 30443K/42088K,
    paused 4ms+6ms, total 55ms 04-14 21:09:00.914: D/dalvikvm(24167):
    GC_EXPLICIT freed 1208K, 28% free 30443K/42088K, paused 4ms+6ms,
    total 53ms 04-14 21:09:01.184: D/dalvikvm(24167): GC_EXPLICIT freed
    1239K, 28% free 30459K/42088K, paused 4ms+6ms, total 56ms 04-14
    21:09:01.454: D/dalvikvm(24167): GC_EXPLICIT freed 1337K, 28% free
    30458K/42088K, paused 3ms+5ms, total 51ms 04-14 21:09:01.684:
    D/dalvikvm(24167): GC_EXPLICIT freed 1223K, 28% free 30443K/42088K,
    paused 4ms+5ms, total 55ms 04-14 21:09:01.944: D/dalvikvm(24167):
    GC_EXPLICIT freed 1263K, 28% free 30444K/42088K, paused 5ms+5ms,
    total 54ms 04-14 21:09:02.204: D/dalvikvm(24167): GC_EXPLICIT freed
    1330K, 28% free 30463K/42088K, paused 3ms+8ms, total 54ms 04-14
    21:09:02.474: D/dalvikvm(24167): GC_EXPLICIT freed 1338K, 28% free
    30461K/42088K, paused 4ms+6ms, total 67ms 04-14 21:09:02.734:
    D/dalvikvm(24167): GC_EXPLICIT freed 1274K, 28% free 30458K/42088K,
    paused 3ms+5ms, total 52ms 04-14 21:09:02.964: D/dalvikvm(24167):
    GC_EXPLICIT freed 1208K, 28% free 30458K/42088K, paused 4ms+5ms,
    total 52ms 04-14 21:09:03.224: D/dalvikvm(24167): GC_EXPLICIT freed
    1266K, 28% free 30461K/42088K, paused 3ms+6ms, total 54ms 04-14
    21:09:03.444: D/dalvikvm(24167): GC_EXPLICIT freed 1209K, 28% free
    30475K/42088K, paused 4ms+5ms, total 54ms 04-14 21:09:03.724:
    D/dalvikvm(24167): GC_EXPLICIT freed 1322K, 28% free 30478K/42088K,
    paused 4ms+8ms, total 56ms

2 个答案:

答案 0 :(得分:2)

这可能是您在代码中的问题 jsonParam.put(result,true);

您输入结果参数作为键布尔值作为值,这意味着每当您的键值不同时,您必须在设置时发送密钥你的PHP代码。

我希望它可能对你有用。

答案 1 :(得分:0)

看起来你错过了帖子数据的KeyName。试试这个

.....
String postData="json="+jsonParam.toString();
.....

 os.write(postData);
...