如何发送和检索使用PHP和Android从Web服务数据(How to send and retri

2019-10-17 15:32发布

我正在开发中,我从网络服务中获取数据,并在Android移动显示应用。 但问题是,当得到土耳其的数据没有正确显示。

我已经设置utf-8在PHP。 但仍然得到特殊字符符号像A?ık ?&#287的字符串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>

Answer 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 文档



文章来源: How to send and retrieve data from web service using PHP and Android