我知道这是一个习惯性的问题;但我真的很奇怪。我在另一个上下文中完成了相同的代码,但不是在
String result = "";
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://192.168.1.145/test/getActivities.php");
//httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
InputStream is = entity.getContent();
//convert response to string
BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
result=sb.toString();
try{
JSONArray jArray = new JSONArray(result);
Log.e("log_tag", "lool");
for(int i=0;i<jArray.length();i++){
JSONObject json_data = jArray.getJSONObject(i);
if (json_data.getString("friendly_url").toString().length() == 2){
Sectors.add(new BasicNameValuePair(json_data.getString("title").toString(),json_data.getString("friendly_url").toString()));
}
else if(json_data.getString("friendly_url").toString().length() == 3){
Branchs.add(new BasicNameValuePair(json_data.getString("title").toString(),json_data.getString("friendly_url").toString()));
}
else if(json_data.getString("friendly_url").toString().length() == 4){
SBranches.add(new BasicNameValuePair(json_data.getString("title").toString(),json_data.getString("friendly_url").toString()));
}
else{
SBranches.add(new BasicNameValuePair(json_data.getString("title").toString(),json_data.getString("friendly_url").toString()));
}
String[] lesSec = new String[Sectors.size()];
for (int u = 0; u < Sectors.size(); u ++)
lesSec[u] = jArray.getJSONObject(u).getString("title");
setSectors(lesSec);
Log.e("log_tag", lesSec[1]);
}
}catch(JSONException e){
Log.e("log_tag", "Error parsing data "+e.toString());
}
}catch(Exception e){
Log.e("log_tag", "Error in http connection "+e.toString());
}
所以这是PHP文件
<?php
$response = array();
$response["success"] = 0;
$response["message"] = "No products found";//*/
@mysql_connect("host","user","");
@mysql_select_db("db");
$q=@mysql_query("SELECT title, friendly_url FROM table");
while($e=@mysql_fetch_assoc($q))
$OUTPUT[]=$e;
print(json_encode($OUTPUT));
print_r ($OUTPUT);
@mysql_close();
?>
有人能看到问题吗? 我试图打印$ OUTPUT,这是一种结果
Array ( [0] => Array ( [title] => Activités de services autres et institutions
[friendly_url] => 15
)
[1] => Array ( [title] => Batiment, travau ...etc
答案 0 :(得分:0)
在发送任何输出之前尝试header('Content-Type: application/json');
。
正如您在答案中所示,您获得的结果实际上是不是 JSON!
了解JSON的外观:
{
"glossary": {
"title": "example glossary",
"GlossDiv": {
"title": "S",
"GlossList": {
"GlossEntry": {
"ID": "SGML",
"SortAs": "SGML",
"GlossTerm": "Standard Generalized Markup Language",
"Acronym": "SGML",
"Abbrev": "ISO 8879:1986",
"GlossDef": {
"para": "A meta-markup language, used to create markup languages such as DocBook.",
"GlossSeeAlso": ["GML", "XML"]
},
"GlossSee": "markup"
}
}
}
}
}
来源:http://json.org/example.html
数组如下所示:[ "one", "two", "three" ]
从PHP脚本中删除print_r
,在JSON字符串使输出无效后,它不会生成有效的JSON格式和后续文本。
在纠正该问题之后,您还应该检查PHP函数mb_internal_encoding
和mb_http_output
返回什么,并将其与java中的预期编码进行比较。
答案 1 :(得分:0)
好;如果有人有特殊字符的问题,我找到了解决方案 它在PHP中;所有你用的特殊项目编码结果:
while($e=@mysql_fetch_assoc($q)){
$e['title'] = mb_convert_encoding($e['title'], 'UTF-8', 'pass');
$OUTPUT[] = $e;
}
我不知道为什么,但它确实有效!