繁体   English   中英

适用于Android的Web服务客户端

[英]web service client for android

我使用本教程http://www.ibm.com/developerworks/webservices/tutorials/ws-eclipse-javase1/ws-eclipse-javase1-pdf.pdf编写了一个简单的eclipse webservice。 现在,我必须为此Web服务编写客户端。 这是我的代码:

public class MainActivity extends Activity {
/** Called when the activity is first created. */
private static final String SOAP_ACTION = "http://wsServer.myfirst.com/getGreeting";
private static final String METHOD_NAME = "getGreeting";
private static final String NAMESPACE = "http://wsServer.myfirst.com/";
private static final String URL = "http://10.0.2.2:8080/wsServerExample?wsdl";

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    try {
        SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
        request.addProperty("arg0","Nicola");            

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

        HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);           

        androidHttpTransport.call(SOAP_ACTION, envelope);
        SoapObject response = (SoapObject)envelope.getResponse();
        String r =  (response.getProperty(0).toString());           
    }
    catch (Exception e) {
        System.out.println(e.getMessage());
    }
    }

当我执行“ SoapObject response =(SoapObject)envelope.getResponse();”时,我遇到此异常“ org.ksoap2.serialization.SoapPrimitive”。 我有以下消息:

com.sun.xml.internal.ws.transport.http.HttpAdapter fixQuotesAroundSoapAction
Received WS-I BP non-conformant Unquoted SoapAction HTTP header: http://ws.myfirst.com/getGreeting/

问题出在哪儿? 这是我的Web服务的代码:SayHello.java

package com.myfirst.wsServer;
import javax.jws.WebService;

@WebService
public class SayHello { 
    private static final String SALUTATION = "Hello"; 
public String getGreeting( String name ) { 
    return SALUTATION + " " + name; 
}
public String get() { 
    return "ciao!"; 
}
}

RunService.java

package com.myfirst.wsServer;
import javax.xml.ws.Endpoint;

public class RunService { 
public static void main(String[] args) { 
    System.out.println("SayHello Web Service started."); 
    Endpoint.publish("http://localhost:8080/wsServerExample", new SayHello()); 
}
}

GetGreeting.java

package com.myfirst.wsServer.jaxws;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;

@XmlRootElement(name = "getGreeting", namespace = "http://wsServer.myfirst.com/")
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "getGreeting", namespace = "http://wsServer.myfirst.com/")
public class GetGreeting {
    @XmlElement(name = "arg0", namespace = "")
    private String arg0;

    public String getArg0() {
        return this.arg0;
    }

    public void setArg0(String arg0) {
        this.arg0 = arg0;
    }
}

GetGreetingResponse.java

package com.myfirst.wsServer.jaxws;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;

@XmlRootElement(name = "getGreetingResponse", namespace = "http://wsServer.myfirst.com/")
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "getGreetingResponse", namespace = "http://wsServer.myfirst.com/")
public class GetGreetingResponse {

    @XmlElement(name = "return", namespace = "")
    private String _return;

    public String getReturn() {
        return this._return;
    }

    public void setReturn(String _return) {
        this._return = _return;
    }
}

尝试这个。 (加上添加的envelope.dotNet = true;

SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.dotNet = true;
envelope.setOutputSoapObject(request);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM