mainActivity:
database_connector wp_terms = new database_connector("SELECT * FROM `dse120071750`.`wp_terms` ",progressDialog,this);
wp_terms.execute();
wp_terms.onPreExecute();
这些是在database_connector内的doInBackground中的代码扩展Asynctask:
httpPost.setEntity(new UrlEncodedFormEntity(nameValuePair, "UTF-8"));
Log.e("TEST", "006");
HttpResponse response = httpClient.execute(httpPost);
httpEntity = response.getEntity();
String entityResponse = EntityUtils.toString(httpEntity);
Log.e("Entity Response ", entityResponse);
jsonArray = new JSONArray(entityResponse);
myPHP:
<?php
header('Content-type: text/plain; charset=utf-8');
$con = $con = mysql_connect("localhost","ac","pw");
if (!$con)
{
die('Could not connect: '.mysql_error());
}
mysql_select_db("dse120071750", $con);
$result = mysql_query("SET NAMES 'UTF8'");
$result = mysql_query("SELECT * FROM Tenant_Data");
while($row = mysql_fetch_assoc($result)){
$output[] = $row;
}
print(json_encode($output, JSON_UNESCAPED_UNICODE));
mysql_close($con);
?>
这是JSONArray的字符串(包含一些中文。它们不是乱码文本):
[{"term_id":"1","name":"未分类","slug":"uncategorized","term_group":"0"},{"term_id":"2","name":"Comfort","slug":"comfort","term_group":"0"},{"term_id":"3","name":"Luxury","slug":"luxury","term_group":"0"},{"term_id":"4","name":"Market Updates","slug":"market-updates","term_group":"0"},{"term_id":"5","name":"Sales","slug":"sales","term_group":"0"},{"term_id":"6","name":"beach","slug":"beach","term_group":"0"},{"term_id":"7","name":"Custom","slug":"custom","term_group":"0"},{"term_id":"8","name":"garden","slug":"garden","term_group":"0"},{"term_id":"9","name":"interior","slug":"interior","term_group":"0"},{"term_id":"10","name":"Trend","slug":"trend","term_group":"0"},{"term_id":"14","name":"15分钟內","slug":"15","term_group":"0"},{"term_id":"51","name":"未租出","slug":"unsold","term_group":"0"},{"term_id":"27","name":"香港科技大学","slug":"hkust","term_group":"0"},{"term_id":"29","name":"20分钟內","slug":"20","term_group":"0"},{"term_id":"30","name":"香港城市大学","slug":"cityu","term_group":"0"},{"term_id":"59","name":"30分钟內","slug":"30","term_group":"0"},{"term_id":"37","name":"5分钟內","slug":"05","term_group":"0"},{"term_id":"38","name":"10分钟內","slug":"10","term_group":"0"},{"term_id":"40","name":"香港大学","slug":"hku","term_group":"0"},{"term_id":"41","name":"香港中文大学","slug":"cuhk","term_group":"0"},{"term_id":"42","name":"Main Menu","slug":"main-menu","term_group":"0"},{"term_id":"43","name":"Image","slug":"post-format-image","term_group":"0"},{"term_id":"44","name":"Gallery","slug":"post-format-gallery","term_group":"0"},{"term_id":"45","name":"Video","slug":"post-format-video","term_group":"0"},{"term_id":"46","name":"香港浸会大学","slug":"hkbu","term_group":"0"},{"term_id":"47","name":"香港理工大学","slug":"polyu","term_group":"0"},{"term_id":"50","name":"已租出","slug":"sold","term_group":"0"},{"term_id":"52","name":"男生宿舍","slug":"male","term_group":"0"},{"term_id":"53","name":"女生宿舍","slug":"female","term_group":"0"},{"term_id":"54","name":"simple","slug":"simple","term_group":"0"},{"term_id":"55","name":"grouped","slug":"grouped","term_group":"0"},{"term_id":"56","name":"variable","slug":"variable","term_group":"0"},{"term_id":"57","name":"external","slug":"external","term_group":"0"},{"term_id":"58","name":"25分钟內","slug":"25","term_group":"0"}]
例外:
08-03 11:57:55.499 4888-5127/mobilehoome W/System.err﹕ org.json.JSONException: Value of type java.lang.String cannot be converted to JSONArray
我可以知道为什么会出现这种情况吗?
有些东西刚刚想通了。当我将字符串复制到网站http://www.freeformatter.com/json-formatter.html#ad-output
时,我发现[
前面有两个字符。我怎么能确定?我按下字符串开头的delete
按钮三次。第三次将删除[
。这就是为什么我确信字符串前面出现两个未知字符。我不知道为什么会出现这两个角色。它们可能是\n
,\t
之类的东西。
答案 0 :(得分:2)
答案 1 :(得分:0)
请参阅此http://www.json.org/javadoc/org/json/JSONArray.html
JSONArray
public JSONArray(java.lang.String source)
throws JSONException
Construct a JSONArray from a source JSON text.
Parameters:
source - A string that begins with [ (left bracket) and ends with ] (right bracket).
Throws:
JSONException - If there is a syntax error.
source - 以[(左括号)开头并以](右括号)开头的字符串。
所以我认为你应该替换
jsonArray = new JSONArray(entityResponse);
与
jsonArray = new JSONArray("[" + entityResponse + "]");