我發現一個數據庫搜索結果的問題。如果,某些領域有多餘的字符,如「ü」,現場回報空,因此它出現在搜索爲空。我的代碼是這樣的: PHP腳本MySQL PHP JSON null text
$q=mysql_query("SELECT * FROM PRODFAR WHERE ARTI LIKE '%".$_REQUEST['search']."%'");
while($e=mysql_fetch_assoc($q))
$output[]=$e;
print(json_encode($output));
mysql_close();
的JSON分析器構造:
public class JsonParser {
static InputStream is = null;
static JSONObject json_data = null;
static String result = "";
// constructor
public JsonParser() {
}
public JSONArray getJSONFromUrl(ArrayList<NameValuePair> nameValuePairs, String url) {
//http post this will keep the same way as it was (it's important to do not forget to add Internet access to androidmanifest.xml
InputStream is = null;
String result ="";
JSONArray jArray = null;
try{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(url);
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
}
catch(Exception e){
Log.e("log_tag", "Error in http connection "+e.toString());
}
//convert response that we receive from the php file into a String()
try{
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();
}catch(Exception e){
Log.e("log_tag", "Error converting result "+e.toString());
}
// try parse the string to a Json object
try {
//json_data = new JSONObject(result);
jArray = new JSONArray(result);
} catch (JSONException e) {
Log.e("JSON Parser", "Error parsing data " + e.toString());
}
// return Json String
return jArray;
}
}
的我怎麼能解決這個任何暗示?
更新:只要我不能通過它與JSON,因爲它只承認UTF-8字符。我認爲可能的解決方案之一是通過PHP將文本轉換爲UTF-8編碼存檔,另一種是使用支持其他編碼的JSON的替代方案。所以我想嘗試第一個。所以如果有任何知道一個好的算法來將文本的編碼轉換爲使用PHP的UTF-8將有所幫助。此外,其它的提示或可能的方向提示,以找到一個解決辦法,歡迎請在這篇文章發表評論任何想法是值得歡迎的
解決 我解決了它,它編碼成UTF-8,它改變了我的字符,如「ü」到像u \ 000f這樣的東西,但是當它顯示在屏幕上時,java編輯器將它顯示爲iso-8859-1。編輯的PHP代碼有以下幾行查詢後:
$q=mysql_query("SELECT * FROM PRODFAR WHERE ARTI LIKE '%$search1%'");
while($e=mysql_fetch_assoc($q)){
$e['ARTI'] = utf8_encode ($e['ARTI']);
$e['DESC'] = utf8_encode ($e['DESC']);
$e['PRESENT'] = utf8_encode ($e['PRESENT']);
$output[]=$e;
}
print(json_encode($output));
你知道嗎,如果MySQL上有一個選項用UTF-8編碼數據非區分大小寫。那也與JSON兼容? – 2012-02-28 15:27:22
我想讓我的字符串變小,而不是爲了UTF-8而盲目逃避一切。有沒有辦法用他們的UTF8等價物/最接近的替代品替換非UTF8字符?另外,如何將utf8_enode影響我的MySQL WHERE語句,我必須對我的條件進行編碼? – Angad 2012-09-11 19:40:53