android php mysql json 查询_android php mysql json
$con = mysql_connect("localhost", "db163810_f", "1e6969e2");
if (!$con)
{
die('不能建立連接: ' . mysql_error());
}
$db_selected = mysql_select_db("db163810",$con);
mysql_query("SET NAMES 'utf8'");
if (!$db_selected)
{
die ("這個(gè)數(shù)據(jù)庫(kù)不能被選: " . mysql_error());
}
$sql = "SELECT `ID` , `post_date` , `post_title` , `post_content` FROM `wp_posts` order by `post_date` desc LIMIT 0, 3 ";
$result = mysql_query($sql,$con);
//print_r($result); //Resource id #2
//print($result); //Resource id #2
//echo $result; //Resource id #2
//echo "===================result==============================
";
while($row = mysql_fetch_assoc($result))
{
//print_r(json_encode($row));
//print(json_encode($row));
echo json_encode($row);
echo "
=====================row================================
";
}
mysql_close($con);
?>
mysql數(shù)據(jù)庫(kù)權(quán)限,只允許localhost連接,不能通過(guò)Internet連接
字符集轉(zhuǎn)換
查詢結(jié)果集不能直接輸出,print($result),緊緊輸出資源代碼Resource id #2
輸出了3個(gè)JSON字符串,3對(duì)大括弧
3,android客戶端如何解碼?
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost("htttp:localhost/mysql.php");
httpPost.setEntity(new UrlEncodedFormEntity(params,HTTP.UTF_8));
//params:List php接收
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
BufferedReader reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
json = sb.toString();
return new JSONObject(json);
總結(jié)
以上是生活随笔為你收集整理的android php mysql json 查询_android php mysql json的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 树莓派移植SX1278 LoRa通信--
- 下一篇: mysql开方_MySQL数学函数的实际