Php Android Java jSON UTF-8 httpResponse问题
我对utf-8中的德语字符有问题。我使用一个MySQL数据库,通过PHP从中获取数据。php脚本将数据转换为json对象并将其发送到应用程序。数据库包含双精度和字符串。首先,应用程序发送一个带有主题名称的字符串。php在db中搜索主题,将内容转换为json并发送给应用程序 我试图发送不带“ä,ü,ö”等字符的数据,结果成功了。当我使用这个德语字符时,它会停在线上Php Android Java jSON UTF-8 httpResponse问题,php,android,mysql,json,utf-8,Php,Android,Mysql,Json,Utf 8,我对utf-8中的德语字符有问题。我使用一个MySQL数据库,通过PHP从中获取数据。php脚本将数据转换为json对象并将其发送到应用程序。数据库包含双精度和字符串。首先,应用程序发送一个带有主题名称的字符串。php在db中搜索主题,将内容转换为json并发送给应用程序 我试图发送不带“ä,ü,ö”等字符的数据,结果成功了。当我使用这个德语字符时,它会停在线上 HttpResponse response =httpClient.execute(httppost); 我不知道为什么,我做错了什
HttpResponse response =httpClient.execute(httppost);
我不知道为什么,我做错了什么。
这是我的申请代码:
public void getData(String data){
String topic = data; //Parameter for PHP
String result = "";
InputStream isr = null;
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(); //PHP
nameValuePairs.add(new BasicNameValuePair("topic", topic)); //PHP
try{
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://192.168.179.20:80/PHP/getData.php"); //PHP-Script on localhost
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); //PHP
HttpResponse response = httpClient.execute(httppost);
HttpEntity entity = response.getEntity();
isr = entity.getContent();
}
catch(Exception e){
Log.e("log_tag", "Error in http connection "+e.toString());
resultView.setText("Could not connect to database");
}
//convert response to string
try{
BufferedReader reader = new BufferedReader(new InputStreamReader(isr,"iso-8859-1"),8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
isr.close();
result=sb.toString();
}
catch(Exception e){
Log.e("log_tag", "Error converting result "+e.toString());
}
try {
String s = "";
JSONArray jArray = new JSONArray(result);
for(int i=0; i<jArray.length();i++){
JSONObject json = jArray.getJSONObject(i);
double Lat = json.getDouble("Latitude");
double Lng = json.getDouble("Longitude");
String Title = new String(json.getString("Ueberschrift").getBytes("ISO-8859-1"),"UTF-8");
String ShortText = new String(json.getString("Kurzbeschreibung").getBytes("ISO-8859-1"),"UTF-8");
String LongText = new String(json.getString("Inhalt").getBytes("ISO-8859-1"),"UTF-8");
String Thema = new String(json.getString("Thema").getBytes("ISO-8859-1"),"UTF-8");
String Datum = json.getString("Date");
String Url = new String(json.getString("Url").getBytes("ISO-8859-1"),"UTF-8");
s = s +
"Latitude: "+Lat+", "+"Longitude: "+Lng+"\n"+
"Thema: "+Thema+"\n"+
"Titel: "+Title+"\n"+
"Kurzbezeichnung: "+ShortText+"\n"+
"Inhalt: "+LongText+"\n\n";
}
resultView.setText(s);
} catch (Exception e) {
// TODO: handle exception
Log.e("log_tag", "Error Parsing Data "+e.toString());
}
}
json如下所示:
[{"id":"5","Longitude":"11.0730833333333","Latitude":"49.4530833333333","Height":"10","Ueberschrift":"Henkersteg","Kurzbeschreibung":"Der Henkersteg, auch","Inhalt":"Stadtbefestigung\u00a0| Marthakirch","Thema":"D\u00fcrer","Thema_id":"3","Datetime":"15\/02\/2015","Url":"http:\/\/de.wikipedia.org\/wiki\/Henkersteg"}]JSON Error: 0
提前感谢您的帮助。我换了线路
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
到
我还更改了数据库的设置。更改后,我可以在应用程序中显示我的字符串,但不幸的是,我得到的是“?”而不是“ü” 因此,您在发布这些字符时遇到问题。从更改php脚本开始。不要对数据库执行任何操作。相反,只需回显接收到的数据。。测试您是否首先返回这些字符。也不要用php进行json编码。不要让它转到“unicode”,例如
\u00a0
。您可能需要utf8。字符串的其余部分是否在“?”之后丢失?(这可能是一条重要线索。)不,只有字符显示为“?”。字符串的其余部分打印正确。(例如:D?rer而不是Dürer)
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs, HTTP.UTF_8));