【问题标题】:Android Java jSON UTF-8 httpResponse ProblemsAndroid Java JSON UTF-8 httpResponse 问题
【发布时间】:2015-05-24 18:14:11
【问题描述】:

我在 utf-8 中遇到了德语字符的问题。我使用 MySQL 数据库,我将使用 PHP 从中获取数据。 php 脚本将数据转换为 json 对象并将其发送到应用程序。数据库包含双精度和字符串。首先,应用程序发送一个带有主题名称的字符串。 php 在数据库中搜索主题,将内容转换为 json 并将其发送到应用程序。

我尝试发送不带“ä,ü,ö”之类的字符的数据,它可以工作。当我使用这个德语字符时,它会在线停止

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());
   }

    }

这是我的 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);

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

提前感谢您的帮助。

【问题讨论】:

  • 所以您在发布这些字符时遇到了问题。从更改您的 php 脚本开始。对数据库不做任何事情。取而代之的是回显接收到的数据。测试是否先取回这些字符。也不要在 php 中进行 json 编码。
  • 不要让它去“unicode”比如\u00a0。相反,您可能需要 utf8。

标签: php android mysql json utf-8


【解决方案1】:

我换行了

 httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

到

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

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

【讨论】:

  • “?”后面的其余字符串是否丢失? (这可能是一个重要的线索。)
  • 不,只有字符显示为“?”。字符串的其余部分打印正确。 (例如:D?rer 而不是 Dürer)
猜你喜欢
  • 2012-02-22
  • 1970-01-01
  • 1970-01-01
  • 2017-07-30
  • 2011-01-11
  • 2017-03-07
  • 2013-11-12
  • 2014-10-13
  • 1970-01-01
相关资源
最近更新 更多