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

    评论

报告相同问题?

悬赏问题

  • ¥50 树莓派安卓APK系统签名
  • ¥15 maple软件,用solve求反函数出现rootof,怎么办?
  • ¥65 汇编语言除法溢出问题
  • ¥15 Visual Studio问题
  • ¥15 state显示变量是字符串形式,但是仍然红色,无法引用,并显示类型不匹配
  • ¥20 求一个html代码,有偿
  • ¥100 关于使用MATLAB中copularnd函数的问题
  • ¥20 在虚拟机的pycharm上
  • ¥15 jupyterthemes 设置完毕后没有效果
  • ¥15 matlab图像高斯低通滤波