1

我正在开发一个应用程序,在该应用程序中我从 Web 服务获取数据并在 Android 手机中显示。但问题是在获取土耳其语数据时显示不正确。

我已经utf-8在 PHP 中设置了。但仍然得到特殊字符符号A?ık ?&#287,如 string Açık Öğretim

这是我的PHP代码:

<?php
include("Netbrut_class.php");
header('Content-type: text/xml; charset: utf-8');

    $webservice = new Netbrut;

    $content='<?xml version="1.0" encoding="utf-8"?><salary_comparision>';

    $education = $webservice->select_education();

    for($i=0; $i<count($education); $i++){

        $string = html_entity_decode($education[$i]['comparision_name'], ENT_QUOTES, "utf-8");
        $content.="<education id='".$education[$i]['id']."'>".$string."</education>";
    }

    $content .='</salary_comparision>';

echo $content;
?>

我的 Java 代码是 / 在这里我编写 Java 代码只是为了测试:

public class testing {

    static String[] attributes;

    public static void main(String[] args)
    {
        URL url;
        try {
            url = new URL("http://192.168.1.106/netBrut/webservices/testing.php");

        HttpURLConnection  connection = (HttpURLConnection) url.openConnection();
        connection = (HttpURLConnection) url.openConnection();
        java.io.InputStream is  = connection.getInputStream();

        InputStreamReader reader = new InputStreamReader(is, "utf-8");


        StringWriter sw = new StringWriter();

        char [] buffer = new char[1024 * 8];
        int count ;

        while( (count = reader.read(buffer)) != -1){
          sw.write(buffer, 0, count);
        }

        System.out.println(sw.toString());
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
      }
}

从 Web 服务返回的 XML 是:

<?xml version="1.0" encoding="utf-8"?>
<salary_comparision>
    <education id='128'>A?&#305;k ?&#287;retim</education>//Check here
    <education id='5'>Doktora</education>
    <education id='3'>Master</education>
    <education id='4'>MBA</education>
    <education id='2'>?niversite</education>
</salary_comparision>
4

1 回答 1

1

在 PHP 代码中只需更改以下行

 $string = html_entity_decode($education[$i]['comparision_name'], ENT_QUOTES, "utf-8");
 $content.="<education id='".$education[$i]['id']."'>".$string."</education>";

$string = $webservice->unhtmlspecialchars($education[$i]['comparision_name']);
$content.="<education id='".$education[$i]['id']."'>".$string."</education>";

文档htmlspecialchars

于 2013-01-28T06:18:26.533 回答