Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/190.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby-on-rails-4/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php HttpURLConnection检查JSON字符串是否已成功插入mysql_Php_Android_Mysql_Json_Httpurlconnection - Fatal编程技术网

Php HttpURLConnection检查JSON字符串是否已成功插入mysql

Php HttpURLConnection检查JSON字符串是否已成功插入mysql,php,android,mysql,json,httpurlconnection,Php,Android,Mysql,Json,Httpurlconnection,我实现了android代码,将JSON字符串发送到服务器。我将这个json字符串'{“mac”:“23:A5:J0:06:C6:E9”,“纬度”:84.16898451,“经度”:3.16561387,“路由”:1}'作为doInBackground方法中的输出,并将getResponsecode:的输出作为客户端中getResponsecode:200的输出 我在localhost中检查了我的php文件,当我刷新index.php主页时,如果我更改JSON字符串中的一些值,就会创建总线表并更新

我实现了android代码,将JSON字符串发送到服务器。我将这个json字符串
'{“mac”:“23:A5:J0:06:C6:E9”,“纬度”:84.16898451,“经度”:3.16561387,“路由”:1}'
作为doInBackground方法中的输出,并将getResponsecode:
的输出作为客户端中getResponsecode:200的输出

我在localhost中检查了我的php文件,当我刷新index.php主页时,如果我更改JSON字符串中的一些值,就会创建总线表并更新数据。我无法将我的S4设备直接连接到本地主机进行检查,因为我正在使用公共热点

我已将
index.php
文件上载到byethost.com服务器上的
htdocs
目录中的在线文件管理器中,正在创建表总线,但没有插入并更新任何记录

HttpURLConnection是否可以检查数据是否已成功插入/更新到
总线
表中?我是否应该为
文件的第一个参数\u get\u contents
方法else
php://input

doInBackground
方法:

protected Void doInBackground(String... params) {
    // TODO Auto-generated method stub

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

        URL myUrl = new URL("http://username.byethost8.com/index.php");
        HttpURLConnection conn = (HttpURLConnection) myUrl
                .openConnection();
        conn.setRequestMethod("POST");
        conn.setDoOutput(true);
        conn.setConnectTimeout(10000);
        conn.setReadTimeout(10000);
        conn.setRequestProperty("Content-Type",
                "application/json");
        conn.connect();
        System.out.println("The output of getResponsecode: "+conn.getResponseCode());
        // create data output stream
        DataOutputStream wr = new DataOutputStream(
                conn.getOutputStream());
        // write to the output stream from the string
        wr.writeBytes(params[0]);
        wr.close();

    } catch (IOException e) {

        e.printStackTrace();
    }
    return null;

}
index.php:

    <?php
     $json = json_decode ( file_get_contents ( 'php://input', true ) );
     $con = new mysqli ( "domain.com", "user_name", "password", "database_name" );
    // I tried with this string to test the index.php file and everything works
// $json = '{"mac": "23:A5:J0:06:C6:E9", "latitude":84.16898451,"longitude":3.16561387,"route": 1}';

    $data = json_decode ( $json );

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


    require 'connection.php';

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

    if (($results->num_rows) == 1) {
      //"UPDATE MyGuests SET lastname='Doe' WHERE id=2"
     $sql = "REPLACE INTO bus(mac, route, latitude, longitude)
              VALUES( ?, ?, ? , ? )";
      $stmt = $con->prepare($sql) or die ( $con->error );
      $stmt->bind_param("ssss",$mac,$route, $latitude,$longitude);
      $stmt->execute();

      $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) NOT NULL,
         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();

      echo "table was created";
    }

由于
mysqli_stmt::execute
返回一个布尔值,因此可以使用它使PHP页面响应一些指示操作是否成功的内容(例如状态代码)(在本例中,您还可以提供错误的解释)。 请注意,在这两种情况下,HTTPURLConnection都是“成功的”(这就是为什么您会得到getResponsecode:200的
输出),因为您的设备已经能够到达服务器,并且错误已经在服务器端生成

编辑: 您得到的异常是因为您试图在收到响应后写入输出流(即服务器)。如果要将某些内容发送到服务器,则需要在收到响应之前写入内容。 因此,如果要打印响应,则需要替换此行

DataOutputStream wr = new DataOutputStream(conn.getOutputStream());
用这个

DataOutputStream wr = new DataOutputStream(conn.getInputStream());

此外,服务器端出现的问题是由于您没有向服务器发送任何内容,因此您需要修复您的android应用程序代码,因为您在输入中使用了两次json_decode()


由于Y语句
System.out.println(“getResponsecode的输出:“+conn.getResponsecode()”)导致读取响应后,您有一个IOException
无法写入请求正文。如果您尚未发送任何邮件,则无法要求回复。或者更好:在那句话之后,你不能再写任何东西了。将该语句放在
wr.close()之后。然后,阅读响应表单
conn.getInputStream()

,如何在eclipse Android输出中看到此状态代码?我如何将其集成到代码中,以便在eclipse中看到它?服务器将以某种方式响应(无论您是
echo
还是
print
,例如此“状态代码”),并且您将能够通过与
conn
关联的
inputStream
读取此响应(请参阅)。然后,您可以像往常一样使用
InputStream
(例如
BufferedInputStream
)读取它,如果您想。。。我显然需要更多的咖啡。我删除了我所说的,以减少进一步的混乱:-D对此表示抱歉@没人担心,没问题;-)@但是数据是存在的,为什么没有发送到服务器?看起来您在原始PHP代码片段中发布了真实的MySQL数据库登录凭据。如果是这种情况,请确保尽快更改您的MySQL用户密码。
conn.getResponseCode()
没有详细说明php脚本的功能。你没有阅读脚本的echo(),那么你怎么知道发生了什么呢?@Nobu:我把错误的凭证放错了,没有什么是对的:)
我得到了这个json字符串“{”mac:“23:A5:J0:06:C6:E9”,“纬度”:84.16898451,“经度”:3.16561387,“路由”:1}”,作为doInBackground方法的输出。不,你得到的是作为你工作背景的输入。然后你试着把它发送到你的脚本中。但是你收到了吗?php脚本收到了吗?你怎么检查?你根本没有检查它。你可以回音检查一下。但你不是在读那些回音。你不知道以这种方式发生了什么。你应该发布logcatt,因为你现在有一个IOException。我添加了一次用于测试,因为我无法将我的设备连接到本地主机,但这不是问题。我删除了它,错误仍然存在。那你为什么把答案标记为正确?每个人都会认为你的问题解决了!请对我的评论做出反应。我在日志中添加了一些。
DataOutputStream wr = new DataOutputStream(conn.getInputStream());