簡體   English   中英

如何使用PHP和Android從Web服務發送和檢索數據

[英]How to send and retrieve data from web service using PHP and Android

我正在開發一個應用程序,在該應用程序中,我將從Web服務獲取數據並在Android移動設備中顯示。 但是問題是在獲取土耳其數據時,它無法正確顯示。

我已經在PHP中設置了utf-8 但是對於字符串Açık Öğretim仍然獲得特殊字符符號,例如A?ık ?&#287

這是我的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>

在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 文檔

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM