Android Java jSON UTF-8 httpResponse问题

I've got a problem with german characters in utf-8. I work with a MySQL database from which I'll get my data with PHP. The php script converts the data into a json object and sent it to the application. The database contains doubles and strings. First the application send a string with the name of a topic. The php search in the db for the topic, convert the content into a json and send it to the application.

I tried to sent the data without characters like "ä,ü,ö" and it work. When I'm using this german characters it stop on line

HttpResponse response =httpClient.execute(httppost);

I don't know why and what I'm doing wrong. Here is my application code:

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 + "
");
            }
            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+"
"+
                   "Thema: "+Thema+"
"+
                   "Titel: "+Title+"
"+
                   "Kurzbezeichnung: "+ShortText+"
"+
                   "Inhalt: "+LongText+"

";


       }


       resultView.setText(s);

   } catch (Exception e) {
    // TODO: handle exception
       Log.e("log_tag", "Error Parsing Data "+e.toString());
   }

    }

Here a part of my php:

 mysql_select_db("database", $con);
       mysql_query('SET CHARACTER SET utf8');
      $thema = $_REQUEST['topic'];


       $result = mysql_query("SELECT * FROM locations WHERE Thema='$thema'") or die('Errant query:');


       while($row = mysql_fetch_assoc($result))
       {

       $output[]=$row;

       }
       //$output = serialize($output);       
       //$output = iconv('ISO-8859-1', 'UTF-8', $output);

    if (function_exists('json_encode')) 
    {
        echo json_encode($output);
        echo "JSON Error: ".json_last_error();
    }
    else { echo "json_encode() is not supported"; }


       mysql_close($con);

The json looks like this:

[{"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

Thanks in advance for any help.

doumeba0486
doumeba0486 不要让它去“unicode”,如\u00a0。你可能需要utf8。
5 年多之前 回复
dregvw1801
dregvw1801 所以发布这些字符时遇到问题。从更改PHP脚本开始。对数据库不执行任何操作。而只是回显收到的数据。测试你是否先获得这些字符。不要在php中使用json编码。
5 年多之前 回复

1个回答



我更改了行</ p>

  httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs))  ; 
</ code> </ pre>

</ p>

  httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs,HTTP.UTF_8)); 
</ code> </ pre>

我也更改了数据库的设置。 更改后,我可以在应用程序中显示我的字符串,但不幸的是我得到了一个“?” 而不是“ü”。 </ p>
</ div>

展开原文

原文

I changed the line

 httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

to

 httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs, HTTP.UTF_8));

I changed also the settings of my database. After changed them, I could display my string in the application, but unfortunately I get a "?" instead of a "ü".

dsms21398
dsms21398 不,只有字符显示为“?”。 正确打印字符串的其余部分。 (例如:D?rer而不是Dürer)
5 年多之前 回复
douqiao2471
douqiao2471 “?”之后是否缺少其余的字符串? (这可能是一个重要的线索。)
5 年多之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐