我想从Android应用程序启动位于我的linux webserver(/ var / www)上的php脚本并获取其输出(绘图图)。这是我的PHP脚本
<?php
$client = new Mosquitto\Client();
$client->onConnect('connect');
$client->onDisconnect('disconnect');
$client->onSubscribe('subscribe');
$client->onMessage('message');
$client->connect("localhost", 8080, 60);
$client->subscribe('+/#', 1);
while (true) {
$client->loop();
sleep(2);
}
$client->disconnect();
unset($client);
function connect($r) {
echo "I got code {$r}\n";
}
function subscribe() {
echo "Subscribed to a topic\n";
exec("Rscript script1.R");
}
function message($message) {
printf("\nGot a message on topic %s with payload:%s",
$message->topic, $message->payload);
$dbusername = "root"; // enter database username, I used "arduino" in step 2.2
$dbpassword = "qwertyazerty"; // enter database password, I used "arduinotest" in step 2.2
$server = "localhost:8008";
$dbconnect = mysql_connect($server, $dbusername, $dbpassword);
$dbselect = mysql_select_db("iotdatas",$dbconnect);
if ($message->topic == "home/temperature"){
$tempinter = $message->payload;
$sql = "INSERT INTO iotdatas.iotDataInter (internetemp) VALUES ('".$tempinter."')";
echo $sql;
mysql_query($sql);
}
if ($message->topic == "a020a61af8a0/temperature"){
$tempext = $message->payload;
$sql1 = "INSERT INTO iotdatas.iotDataExter (externetemp) VALUES ('".$tempext."')";
echo $sql1;
// Execute SQL statement
mysql_query($sql1);
}
$sql2= "SELECT externetemp from iotdatas.iotDataExter";
$sql2;
$req2=mysql_query($sql2);
while ($row= mysql_fetch_array($req2)){
$arr1 = $row['externetemp'];
echo $arr1;
}
$sql3= "SELECT internetemp from iotdatas.iotDataInter";
$sql3;
$req3= mysql_query($sql3);
while ($row = mysql_fetch_array($req3)){
$arr2 = $row['internetemp'];
echo $arr2;
}
$size1= "SELECT COUNT(*) FROM iotdatas.iotDataExter";
$rs1 = mysql_query($size1);
$size2="SELECT COUNT(*) FROM iotdatas.iotDataInter";
$rs2 = mysql_query($size2);
}
exec("Rscript script1.R");
function disconnect() {
echo "Disconnected cleanly\n";
}
这是我的Android代码,但我发现脚本没有执行。
public class lunchScript extends AsyncTask<String, String, String> {
@Override
protected String doInBackground(String... params) {
HttpURLConnection conn = null;
try {
URL url;
url = new URL(params[0]);
conn = (HttpURLConnection) url.openConnection();
if (conn.getResponseCode() == HttpURLConnection.HTTP_OK) {
InputStream is = conn.getInputStream();
Log.i("res ",conn.toString());
Log.i("res1 ",is.toString());
} else {
InputStream err = conn.getErrorStream();
}
return "Done";
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (conn != null) {
conn.disconnect();
}
}
return null;
}
}