android php mysql json

<?php  
$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 ("这个数据库不能被选: " . 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==============================<br/>";
while($row = mysql_fetch_assoc($result))  
  {    
  //print_r(json_encode($row)); 
  //print(json_encode($row)); 
  echo json_encode($row); 
  echo "<br/>=====================row================================<br/>"; 
  }  
  
mysql_close($con);  
?> 

mysql数据库权限,只允许localhost连接,不能通过Internet连接

字符集转换

查询结果集不能直接输出,print($result),紧紧输出资源代码Resource id #2

输出了3个JSON字符串,3对大括弧


 

3,android客户端如何解码?

DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost("htttp:localhost/mysql.php");
httpPost.setEntity(new UrlEncodedFormEntity(params,HTTP.UTF_8));
//params:List<NameValuePair> 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);

http://www.cnblogs.com/LIANQQ/archive/2012/11/14/2769911.html

 


                
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值