我实现了android代码,将JSON字符串发送到服务器。我在doInBackground方法中将此json字符串'{"mac": "23:A5:J0:06:C6:E9", "latitude":84.16898451,"longitude":3.16561387,"route": 1}'
作为输出,并在客户端获取输出getResponsecode:The output of getResponsecode: 200
的输出。
我在localhost中检查了我的php文件,当我刷新index.php主页时,如果我更改JSON字符串中的某些值,则会创建总线表并更新数据。我无法直接将我的S4设备连接到localhost,因为我正在使用公共热点。
我已将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
答案 0 :(得分:0)
您在输入中使用了两次json_decode()
。
您的语句cannot write request body after response has been read
导致IOException System.out.println("The output of getResponsecode: "+conn.getResponseCode());
。如果你还没发送任何内容,你不能要求回复。或者更好:在声明之后你不能再写任何东西了。把这句话放在wr.close()
之后。然后阅读回复表格conn.getInputStream()
。