dtu15253 2015-04-29 19:55
浏览 35

使用HttpURLConnection中的getInputStream在PHP脚本中读取echo

I want to output the echo of the PHP script in the logcat but nothing comes back. The JSON String {"mac": "10:A5:D0:06:C6:E9", "latitude":84.16898451,"longitude":3.16561387,"route": 1} is there in the doInBackground method I can see it in the output. The output of the getResponseCode is 200 but I am trying now to read the echo of the PHP script but nothing is appearing the Logcat.

On the server side the table is being created but no data is being inserted into it.

doInbackground method:

protected Void doInBackground(String... params) {
        // TODO Auto-generated method stub
        BufferedReader reader = null;

        try {
            System.out.println("The output of : doInBackground " + params[0]);

            URL myUrl = new URL("http://byethost.com/index.php");
            HttpURLConnection conn = (HttpURLConnection) myUrl.openConnection();
            conn.setRequestMethod("POST");
            conn.setRequestProperty("Content-Type", "application/json");
            conn.connect();

            System.out.println("The output of getResponsecode: "
                    + conn.getResponseCode());

            DataOutputStream wr = new DataOutputStream(conn.getOutputStream());
            wr.writeBytes(params[0]);

            StringBuilder sb = new StringBuilder();
            reader = new BufferedReader(new InputStreamReader(
                    conn.getInputStream()));
            String line;

            while ((line = reader.readLine()) != null) {
                sb.append(line + "
");

            }

            System.out.println("The output of the StringBulder: "
                    + sb.toString());

            wr.close();

        } catch (IOException e) {

            e.printStackTrace();
            return null;
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                    return null;
                } catch (Exception e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }

        return null;

    }

PhP script:

<?php
 $data = json_decode ( file_get_contents ( 'php://input', true ) );
 $con = new mysqli ( "domin.com", "username", "password", "database" );


$mac = $data->{'mac'};
$latitude = $data->{'latitude'};
$longitude = $data->{'longitude'};
$route =   $data->{'route'};

// check whether route's table exist.
$results = $con->query ( "SHOW TABLES like 'bus' " ) or die ( mysqli_error () );

if (($results->num_rows) == 1) {$sql = "REPLACE INTO bus(mac, route, latitude, longitude)
          VALUES( ?, ?, ? , ? )";
  $stmt = $con->prepare($sql);

  if(false === $stmt){
    echo "prepare()  failed: ";
  }

  $rc = $stmt->bind_param("ssss",$mac,$route, $latitude,$longitude);
 echo $rc;
  if ( false===$rc ) {
  echo "bind_param() failed: ";
}

  $rc = $stmt->execute();
echo "execute failed.";
  if ( false===$rc ) {
  die('execute() failed: ' . htmlspecialchars($stmt->error));
  }

  $stmt->close();

  echo "Table exist";
} else {
  $create =  "CREATE TABLE bus
       (id INT(11) NOT NULL AUTO_INCREMENT PRIMARY KEY,
        mac VARCHAR(30) NOT NULL UNIQUE,
        route int(11) ,
     latitude FLOAT(10,6) NOT NULL , 
     longitude FLOAT(10,6) NOT NULL,
     created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP)" ;
   $stmt = $con->prepare($create) or die ( $con->error );
  $stmt->execute();

  $stmt->close();

}
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 ubuntu子系统密码忘记
    • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
    • ¥15 保护模式-系统加载-段寄存器
    • ¥15 matlab求解平差
    • ¥15 电脑桌面设定一个区域禁止鼠标操作
    • ¥15 求NPF226060磁芯的详细资料
    • ¥15 使用R语言marginaleffects包进行边际效应图绘制
    • ¥20 usb设备兼容性问题
    • ¥15 错误(10048): “调用exui内部功能”库命令的参数“参数4”不能接受空数据。怎么解决啊
    • ¥15 安装svn网络有问题怎么办