doubi2228 2015-03-21 10:37
浏览 93

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.

  • 写回答

1条回答 默认 最新

  • du8791069 2015-03-21 12:09
    关注

    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 "ü".

    评论

报告相同问题?

悬赏问题

  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置