|
android程序凋用php生成的json出错
服务器是php+mysql
php文件用记事本打开,另存为utf-8
MySQL数据库编码utf8
MySQL Users 结构
DROP TABLE IF EXISTS `dm0001`.`Users`;
CREATE TABLE `dm0001`.`Users` (
`Id` varchar(15) NOT NULL,
`Name` varchar(10) DEFAULT NULL,
`Pwd` varchar(32) DEFAULT NULL,
PRIMARY KEY (`Id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
json.php
<?php
// 将数组转换成Json格式,中文需要进行URL编码处理
function Array2Json($array) {
arrayRecursive($array, 'urlencode', true);
$json = json_encode($array);
$json = urldecode($json);
// ext需要不带引号的bool类型
$json = str_replace("\"false\"","false",$json);
$json = str_replace("\"true\"","true",$json);
return $json;
}
function arrayRecursive(&$array, $function, $apply_to_keys_also = false)
{
static $recursive_counter = 0;
if (++$recursive_counter > 1000) {
die('possible deep recursion attack');
}
foreach ($array as $key => $value) {
if (is_array($value)) {
arrayRecursive($array[$key], $function, $apply_to_keys_also);
} else {
$array[$key] = $function($value);
}
if ($apply_to_keys_also && is_string($key)) {
$new_key = $function($key);
if ($new_key != $key) {
$array[$new_key] = $array[$key];
unset($array[$key]);
}
}
}
$recursive_counter--;
}
?>
index.php代码:
<?php
include("common/json.php");
// header("content-type:text/HTML; charset=utf-8");
$link=mysql_connect("localhost","root","aaaaaa");
mysql_select_db("dm0001", $link);
mysql_query("set names 'utf8'", $link);
$sql="SELECT * FROM Users WHERE id='".$_REQUEST['id']."'";
$result=mysql_query($sql);
while($e=mysql_fetch_assoc($result))
$output[]=$e;
print(Array2Json($output));
mysql_close();
?>
http://dm0001.gotoip3.com/android/index.php?id=9900
index.php 在IE中访问得到json [{"Id":"9900","Name":"吕静","wd":"9463"}]
下面为安卓程序的代码:
String json = "";
String httpUrl = "http://dm0001.gotoip3.com/android/index.php";
//首先使用NameValuePair封装将要查询的年数和关键字绑定
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("id","9900"));
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(httpUrl);
//使用HttpPost封装整个SQL语句
//使用HttpClient发送HttpPost对象
try{
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
InputStream is = entity.getContent();
//将HttpEntity转化为String
BufferedReader reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line);
}
is.close();
json = sb.toString();
Log.i("log_tag", "json="+json); //这个地方可以正常得到结果json=[{"Id":"9900","Name":"吕静","Pwd":"9463"}]
JSONArray array = new JSONArray(json); //问题在这个地方报错 05-23 10:34:25.594: ERROR/log_tag(260): >>> org.json.JSONException: A JSONArray text must start with '[' at character 1 of ??[{"Id":"9900","Name":"吕静","Pwd":"9463"}]
for(int i=0;i<array.length();i++){
JSONObject json_data = array.getJSONObject(i);
Log.i("log_tag","id: "+json_data.getInt("Id")+
", name: "+json_data.getString("Name")+
", pwd: "+json_data.getInt("Pwd")
);
}
}catch(Exception e){
Log.e("log_tag", ">>> "+e.toString());
}
希望哪位大哥给指点一下 |
|