如何发送和检索使用PHP和Android从Web服务数据 [英] How to send and retrieve data from web service using PHP and Android

查看:174
本文介绍了如何发送和检索使用PHP和Android从Web服务数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

我已经设置了 UTF-8 在PHP。但仍然得到特殊字符符号如 A和?#305; K&放大器;?#287 字符串AçıkÖğretim

下面是我的PHP code:

 < PHP
包括(Netbrut_class.php);
标题(内容类型:文本/ xml的;字符集:utf-8');    $ web服务=新Netbrut;    $内容='< XML版本=1.0编码=UTF-8>< salary_comparision>';    $教育= $ webservice-> select_education();    为($ I = 0; $ I<计数($教育); $ I ++){        $字符串= html_entity_de code($教育[$ i] ['comparision_name'],ENT_QUOTES,UTF-8);
        $内容=<教育的id ='。$教育[$ i] ['身份证']'。>中$字符串< /教育>。
    }    $内容='< / salary_comparision>'。回声$内容;
?>

和我的Java code是/我在这里编写Java code只是用于测试:

 公共类的测试{    静态的String []属性;    公共静态无效的主要(字串[] args)
    {
        URL网址;
        尝试{
            URL =新的URL(http://192.168.1.106/netBrut/webservices/testing.php);        HttpURLConnection的连接=(HttpURLConnection类)url.openConnection();
        连接=(HttpURLConnection类)url.openConnection();
        的java.io.InputStream是= connection.getInputStream();        InputStreamReader的读者=新的InputStreamReader(是,UTF-8);
        StringWriter的SW =新的StringWriter();        的char []缓冲区=新的char [1024 * 8]。
        诠释计数;        而((计数= reader.read(缓冲液))!= - 1){
          sw.write(缓冲,0,计数);
        }        的System.out.println(sw.toString());
        }赶上(例外五){
            // TODO自动生成catch块
            e.printStackTrace();
        }
      }
}

和从Web服务返回的XML是:

 <?XML版本=1.0编码=UTF-8&GT?;
< salary_comparision>
    <教育ID ='128'> A和#305; K&放大器;?#287;&RETIM LT; /教育> //这里检查
    <教育ID ='5'> Doktora< /教育>
    <教育的id ='3'>硕士< /教育>
    <教育ID =4> MBA和LT; /教育>
    <教育ID ='2'> niversite< /教育>
< / salary_comparision>


解决方案

在PHP的code只是改变了低于线

  $字符串= html_entity_de code($教育[$ i] ['comparision_name'],ENT_QUOTES,UTF-8);
 $内容=<教育的id ='。$教育[$ i] ['身份证']'。>中$字符串< /教育>。

  $字符串= $ webservice-> unhtmlspecialchars($教育[$ i] ['comparision_name']);
$内容=<教育的id ='。$教育[$ i] ['身份证']'。>中$字符串< /教育>。

请参阅 用htmlspecialchars &害羞;的文件

I am developing an application in which I am getting data from web service and displaying in the Android mobile. But the problem is while getting Turkish data it is not displaying correctly.

I already set utf-8 in PHP. But still getting special character symbols like A?ık ?&#287 for the string Açık Öğretim.

Here is my PHP code:

<?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;
?>

And my Java code is / Here I am writing java code just for testing:

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

And returning XML from web service is:

<?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>

解决方案

In the Php code just change the below lines

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

to

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

See htmlspecialchars­Docs.

这篇关于如何发送和检索使用PHP和Android从Web服务数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆