【发布时间】:2015-04-29 17:31:53
【问题描述】:
我实现了将 JSON 字符串发送到服务器的 android 代码。我在 doInBackground 方法中得到这个 json 字符串 '{"mac": "23:A5:J0:06:C6:E9", "latitude":84.16898451,"longitude":3.16561387,"route": 1}' 作为输出,并得到输出 getResponsecode: The output of getResponsecode: 200 在客户端的输出。
我在本地主机中检查了我的 php 文件,当我刷新 index.php 主页时,如果我更改 JSON 字符串中的某些值,则会创建总线表并更新数据。因为我使用的是公共热点,所以我无法将我的 S4 设备直接连接到本地主机来检查它。
我已经将index.php文件上传到byethost.com服务器上htdocs目录下的在线文件管理器,正在创建表总线但没有插入记录并更新。
HttpURLConnection 是否可以检查bus 表中的数据是否已成功插入/更新?我应该为file_get_contents 方法的第一个参数添加另一个路径,否则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";
}
一些 Logcat 输出:
04-29 22:18:28.541: W/System.err(32348): java.net.ProtocolException: cannot write request body after response has been read
04-29 22:18:28.541: W/System.err(32348): at com.android.okhttp.internal.http.HttpURLConnectionImpl.getOutputStream(HttpURLConnectionImpl.java:214)
04-29 22:18:28.551: W/System.err(32348): at com.bustracker.MyAsyncTask.doInBackground(PostData.java:61)
04-29 22:18:28.551: W/System.err(32348): at com.bustracker.MyAsyncTask.doInBackground(PostData.java:1)
04-29 22:18:28.551: W/System.err(32348): at android.os.AsyncTask$2.call(AsyncTask.java:288)
04-29 22:18:28.551: W/System.err(32348): at java.util.concurrent.FutureTask.run(FutureTask.java:237)
04-29 22:18:28.551: W/System.err(32348): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
04-29 22:18:28.551: W/System.err(32348): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
04-29 22:18:28.551: W/System.err(32348): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
04-29 22:18:28.551: W/System.err(32348): at java.lang.Thread.run(Thread.java:818)
04-29 22:18:28.561: I/System.out(32348): The output of : doInBackground {"mac":"89:GG:D0:06:86:M6","latitude":93.86900553,"longitude":25.66558334,"route":4}
04-29 22:18:28.591: I/System.out(32348): (HTTPLog)-Static: isSBSettingEnabled false
04-29 22:18:28.591: I/System.out(32348): KnoxVpnUidStorageknoxVpnSupported API value returned is false
【问题讨论】:
-
看起来您在原始 PHP 代码 sn-p 中发布了真实的 MySQL 数据库登录凭据。如果是这种情况,请确保尽快更改您的 MySQL 用户密码。
-
conn.getResponseCode()并没有过多地说明 php 脚本做了什么。您没有阅读脚本的 echo(),那么您怎么知道发生了什么? -
@Nobu:我输入了错误的凭据,没有什么是对的 :)
-
I am getting this json string '{"mac": "23:A5:J0:06:C6:E9", "latitude":84.16898451,"longitude":3.16561387,"route": 1}' as output in the doInBackground method。不,您将其作为 doInBackground 的输入。然后您尝试将其发送到您的脚本。但是你收到了吗? php 脚本会收到吗?你怎么检查?你根本没有检查它。你可以回显它来检查。但你不是在读那些回声。你不知道这样会发生什么。 -
您应该发布 logcatt,因为您现在有一个 IOException。
标签: php android mysql json httpurlconnection