2.需要透過PHP的網頁程式先抓取 MYSQL在phpMyAdmin網頁管理介面的資料。
3.在自設伺服器上放上該功能網頁程式。
3.在自設伺服器上放上該功能網頁程式。
header("refresh:60;url=get_mysql_5D.php"); //每分鐘重整一次
$mysqli = new mysqli('localhost', '使用者名稱', '密碼', 'DB名稱');//登入phpMyAdmin
mysqli_set_charset($mysqli, "UTF8"); //使中文能正確存取
date_default_timezone_set("Asia/Shanghai"); //設定時區
//echo "目前時間為 " . (int)date("H"). ":". (int)date("i"). "<br>";
//echo (int)date('Y') ."-" .(int)date('n')."-" . (int)date('j');//今日日期
//echo (int)date('Y') ."-" .(int)date('n')."-" . ((int)date('j')-2);//前日日期
$nOfD=0; //計數器
if($_GET['string1']){ //獲取拜訪者傳入變數後的處理
}
if($_GET['string2']){
}
//$sql = "SELECT * FROM 資料表名稱 ORDER BY id ASC LIMIT 1";//取第一行資料
//$sql = "SELECT * FROM 資料表名稱 where 欄位名稱=你要的值";//取最全部資料
$sql = "SELECT * FROM 資料表名稱 where (欄位名稱甲 LIKE '你要的值' OR 欄位名稱甲 is null) AND (欄位名稱乙 LIKE '你要的值' OR 欄位名稱乙 is null) "; //條件選取
if (!$result = $mysqli->query($sql)) { //印出錯誤訊息
// Oh no! The query failed.
echo "Sorry, the website is experiencing problems.";
// Again, do not do this on a public site, but we'll show you how
// to get the error information
echo "Error: Our query failed to execute and here is why: \n";
echo "Query: " . $sql . "\n";
echo "Errno: " . $mysqli->errno . "\n";
echo "Error: " . $mysqli->error . "\n";
exit;
}
else if ($result->num_rows === 0) { //印出錯誤訊息
// Oh, no rows! Sometimes that's expected and okay, sometimes
// it is not. You decide. In this case, maybe actor_id was too
// large?
echo "We could not find any data, sorry about that. Please try again.";
exit;
}
if ($result = $mysqli->query($sql)) {
/*echo '<table border="0" cellspacing="2" cellpadding="2"> //用漂亮格式印出
<tr>
<td> <font face="Arial">欄位名稱甲</font> </td>
<td> <font face="Arial">欄位名稱乙</font> </td>
<td> <font face="Arial">欄位名稱丙</font> </td>
</tr>';*/
$data =array();
$nOfD=0;
while ($row = $result->fetch_assoc()) { //存取各欄位數值(放進$row)
/*$field1name = $row['欄位名稱甲'];
$field2name = $row["欄位名稱乙"];
$field3name = $row["欄位名稱丙"];
*/
//echo '<b>'.$field1name.$field2name.'</b><br />'; //用漂亮格式印出
/*echo '<tr>
<td>'.$field1name.'</td>
<td>'.$field2name.'</td>
<td>'.$field3name.'</td>
</tr>';*/
array_push($data,array('username'=>$row['username'],'userid'=>$row['userid'],'longitude'=>$row['longitude'],'latitude'=>$row['latitude'],'date'=>$row['date'],'time'=>$row['time'],'m_code'=>$row['m_code'],'ap_ip'=>$row['ap_ip'],'ap_ssid'=>$row['ap_ssid'],'ap_rssi'=>$row['ap_rssi']));
//存入陣列$data
$nOfD++;
}
for($i=0;$i<$nOfD;$i++){ //將陣列轉為urlencode,為使中文編碼正確顯示
foreach ( $data[$i] as $key => $value ) {
$data[$i][$key]= urlencode ( $value );
}
}
echo urldecode (json_encode ( $data )) ; //將陣列轉為JSON格式資料並印出,以便日後運用
/*freeresultset*/ //釋放記憶體
$result->free();
}
/*echo strcoll("Hello World!","Hello World!");
echo "<br>";
//字串比較
*/
?>
4.android studio 中抓取該網頁之echo,也就是該網頁之body。
try{
URL url = new URL("localhost/get_mysql_Data.php); //放置於主機的功能網頁網址
urlConn = (HttpURLConnection) url.openConnection();
urlConn.setRequestMethod("POST");
urlConn.connect();
is = urlConn.getInputStream();
}catch (Exception e){
Log.d("Fail 1",e.toString()); //印出錯誤訊息
}
try{
BufferedReader reader = new BufferedReader(new InputStreamReader(is,"UTF-8"));
StringBuilder stringBuilder = new StringBuilder();
while ((line=reader.readLine())!=null){
stringBuilder.append(line+"\n");
}
is.close();
tvdata.setText(stringBuilder.toString()); //將所得結果印出
}catch (Exception e){
Log.d("Fail 2",e.toString());
}
5.網址後綴變數格式
URL url = new URL("網址?string1="+變數A+"&string2="+ 變數B);
沒有留言:
張貼留言