使用Android WebService Client和PHP服务器传递参数时出现问题

时间:2011-05-09 00:12:40

标签: php android web-services

我有以下代码调用PHP Web服务。代码连接到服务,但参数传递不好。该方法只是返回参数,对于下面的代码,它返回“mm”(基本上它返回第一个参数的第一个字母两次)。

    String SOAP_ACTION = "urn:server#userAuth"; 
    String METHOD_NAME = "userAuth";

    SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);

    PropertyInfo p1 = new PropertyInfo();
    p1.type = PropertyInfo.STRING_CLASS;
    p1.setName("usr");
    p1.setValue("myuser");
    p1.setNamespace(NAMESPACE);
    request.addProperty(p1);

    PropertyInfo p2 = new PropertyInfo();
    p2.type = PropertyInfo.STRING_CLASS;
    p2.setName("pass");
    p2.setValue("xxxxxxxxxxx");
    p2.setNamespace(NAMESPACE);
    request.addProperty(p2);      

    SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);

    envelope.setOutputSoapObject(request);
    HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
    androidHttpTransport.call(SOAP_ACTION, envelope);

    Object result = (Object)envelope.getResponse();
    return result.toString();

这是php服务器代码......

$server = new nusoap_server;
$server->configureWSDL('server', 'urn:server');
$server->wsdl->schemaTargetNamespace = 'urn:server';
$server->register('userAuth',
            array('usr' => 'xsd:string', 'pass' => 'xsd:string'),
            array('return' => 'xsd:string'),
            'urn:server',
            'urn:server#userAuth');

function userAuth($value){

    $a=$value['usr'].$value['pass'];

        return $a;    
}

$HTTP_RAW_POST_DATA = isset($HTTP_RAW_POST_DATA) ? $HTTP_RAW_POST_DATA : '';

$server->service($HTTP_RAW_POST_DATA);

2 个答案:

答案 0 :(得分:1)

好的,所以我终于通过在SoapObject的另一个实例中添加参数来实现它的工作将由请求的SoapObject调用。最终的代码是:

String SOAP_ACTION =“userAuth”;         字符串METHOD_NAME =“userAuth”;

    SoapObject Request = new SoapObject(NAMESPACE, METHOD_NAME);

    SoapObject parameters = new SoapObject(NAMESPACE, METHOD_NAME);
    parameters.addProperty("usr",  "myuser");
    parameters.addProperty("pass", "xxxxxxxxxxxxxxx");

    Request.addProperty(METHOD_NAME, parameters); 



    SoapSerializationEnvelope soapEnvelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
    soapEnvelope.setOutputSoapObject(Request);


    HttpTransportSE androidHttpTransportSE = new HttpTransportSE(URL);
    androidHttpTransportSE.call(SOAP_ACTION, soapEnvelope);


    Object result = (Object)soapEnvelope.getResponse();
    return result.toString(); 

答案 1 :(得分:0)

SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME); 

request.addProperty("name1", values1);
request.addProperty("name2", values2);



SoapSerializationEnvelope soapEnvelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
soapEnvelope.setOutputSoapObject(request);


HttpTransportSE androidHttpTransportSE = new HttpTransportSE(URL);
androidHttpTransportSE.call(SOAP_ACTION, soapEnvelope);
Object result = (Object)soapEnvelope.getResponse();
final String str =  result.toString();