当调用一个调用PHP函数的Web服务时,J2ME会引发异常,从而选择MySQL表列中突出显示的字母

时间:2012-01-16 07:45:09

标签: php web-services java-me wsdl ksoap2

我想从webservice应用程序中调用J2ME

public String alimenterBaseDeDonneesEtapeProsp( String login, String password,String adresse,String site)
    {
        Object  resultatRequeteSOAP = null;
        SoapObject objetSOAPEtapeProsp;
        HttpTransport connexionServeur;
        SoapSerializationEnvelope envelope;
        String nomService = "urn:Client";
        String urlService = adresse+"/"+site+"/webservice/InterfaceTransfererClient.php";
        String methodeChoisie = "getEtapeProsp";
        try
        {
                    connexionServeur = new HttpTransport(urlService );
                    connexionServeur.debug = true;
                    objetSOAPEtapeProsp = new SoapObject(nomService, methodeChoisie );
                    objetSOAPEtapeProsp.addProperty("user", login);
                    objetSOAPEtapeProsp.addProperty("motpasse",password);
                    System.out.println(objetSOAPEtapeProsp);
                    envelope = new SoapSerializationEnvelope(SoapSerializationEnvelope.VER11);
                    envelope.bodyOut = objetSOAPEtapeProsp;
                    connexionServeur.call(null,envelope);
                    resultatRequeteSOAP = envelope.getResponse();
                    return String.valueOf(resultatRequeteSOAP);
        }
        catch (Exception aE)
        {
             return aE.getMessage();
        }
    }

ksoap2-j2me-core-2.1.2.jar文件已添加到J2ME项目中。

wsdl文件是:

<?xml version="1.0" encoding="utf-8"?>
<definitions name="Client" 
        targetNamespace="urn:Client" 
        xmlns:typens="urn:Client" 
        xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
        xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" 
        xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" 
        xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" 
        xmlns="http://schemas.xmlsoap.org/wsdl/">

    <!-- partie 2 : Types-->
    <types>
        <xsd:schema xmlns="http://www.w3.org/2001/XMLSchema" 
            targetNamespace="urn:Client">
        </xsd:schema>
    </types>
        ...
    <message name="getEtapeProspRequest">
        <part name="user" type="xsd:string"/>
        <part name="motpasse" type="xsd:string"/>
    </message>
    <message name="getEtapeProspResponse">
        <part name="return" type="xsd:anyType"/>
    </message>
        ...
    <portType name="ClientPort">
             ...
        <operation name="getEtapeProsp">
            <input message="typens:getEtapeProspRequest"/>
            <output message="typens:getEtapeProspResponse"/>
        </operation>
    </portType>
    <binding name="ClientBinding" type="typens:ClientPort">
        <soap:binding style="rpc" transport="http://schemas.xmlsoap.org/soap/http"/>
        ...
        <operation name="getEtapeProsp">
            <soap:operation soapAction="EtapeProspAction"/>
            <input name="getEtapeProspRequest">
                <soap:body  use="encoded"   namespace="urn:Client" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"/>
            </input>
            <output name="getEtapeProspResponse">
                <soap:body  use="encoded" namespace="urn:Client" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"/>
            </output>
        </operation>
    </binding>

    <!-- partie 7 : Service -->
    <service name="ClientService">
        <documentation></documentation>
        <!-- partie 8 : Port -->
        <port name="ClientPort" binding="typens:ClientBinding">
            <soap:address location="http://192.168.1.123/imfmobile/webservice/InterfaceTransfererClient.php"/>
        </port>
    </service>
</definitions>

包含webservice调用的函数的PHP代码:

<?php
    header('Content-Type: text/plain; charset=utf-8');
    ini_set("soap.wsdl_cache_enabled", "0");
    $serveurSOAP = new SoapServer('Client.wsdl');
    $serveurSOAP->addFunction('getClient');
    $serveurSOAP->addFunction('getCredit');
    $serveurSOAP->addFunction('getEcheancier');
    $serveurSOAP->addFunction('getUrlPhotos');
    $serveurSOAP->addFunction('getModificationClient');
    $serveurSOAP->addFunction('getModificationCredit');
    $serveurSOAP->addFunction('getModificationEcheance');
    $serveurSOAP->addFunction('getAllPhotoNames');
    $serveurSOAP->addFunction('getEtapeProsp');
    if ($_SERVER['REQUEST_METHOD'] == 'POST')
    {
         $serveurSOAP->handle();
    }
    else
    {
         echo 'désolé, je ne comprends pas les requêtes GET, veuillez seulement utiliser POST';
    }
        ...
        function verifyUser($user, $motpasse) {
        $ok = null ;
        $connec = mysql_connect("192.168.1.123:3306", "root", "mysqlroot");
        mysql_select_db("finance",$connec);
        $query=mysql_query("SELECT  user_code FROM utilisateur WHERE user_login = '".$user."' AND user_passwd = md5('".$motpasse."')");
        if (mysql_num_rows($query)== 1) 
        {
            $ligne = mysql_fetch_array($query);
            $ok = $ligne['user_code'] ;
        }   
        return $ok;
    }
        ...
        function getEtapeProsp($user,$motpasse)
    {
        $user_code    = verifyUser($user, $motpasse) ;
        $resultat="";
        if ( $user_code != null) 
        {
            $datejour = date("Y-m-d");
            $connec   = mysql_connect("192.168.1.123:3306", "root", "mysqlroot");
            mysql_select_db("finance",$connec);
            $query    = mysql_query("SELECT * FROM etape_prospection INNER JOIN type_prospection ON etape_prospection.type_prosp_id = type_prospection.type_prosp_id WHERE prosp_id IN (SELECT prosp_id FROM transfert WHERE user_code ='".$user_code ."' AND date_transfert='".$datejour."') order by etape_prospection.prosp_id");
            while($ligne = mysql_fetch_array($query))
                {
                     $resultat .= $ligne['etape_prosp_id'].';';
                     $resultat .= $ligne['type_prosp_lib'].';';
                     $resultat .= convertDateFormatHH($ligne['etape_prosp_date']).';';
                     $resultat .= $ligne['etape_prosp_comment'].';'; // this is the column containing the accentuated letters
                     $resultat .= $ligne['prosp_id'].';';
                     $resultat .= "\r\n";
                }
        }
        else
        {
            $resultat = "Login ou mot de passe incorrect" ;
        }
        return $resultat;
    }
?>

如您所见,我在wsdlPHP中都设置了编码/字符集。

为什么在运行J2ME应用程序时会引发此异常:

expected: END_TAG {http://schemas.xmlsoap.org/soap/envelope/}Body (position:END_TAG </{http://schemas.xmlsoap.org/soap/envelope/}SOAP-ENV:Fault>@2:299 in java.io.InputStreamReader@e5125d64)

1 个答案:

答案 0 :(得分:1)

您需要告诉您的mysql数据库/客户端您希望具有UTF-8编码值(因为您将它们作为UTF-8插入到UTF-8编码的SOAP响应中)。

否则SOAP客户端会看到有问题的字母的无效字节序列并抛出异常。请参考您的mysql客户端库手册以指定编码。

$connec = mysql_connect("192.168.1.123:3306", "root", "mysqlroot");
mysql_set_charset('utf8', $connec);

mb_convert_encoding功能只是一种解决方法,您无法解决根本原因,它可能会产生您不想要的副作用。