从android中的webservice wsdl获取数据

时间:2013-11-15 12:36:09

标签: java android web-services wsdl

当我运行AndroidWSClient时,只有运行main.xml文件但未与webservice wsdl文件连接而不生成输出时,不会生成数据。我该怎么办?

Hello.java:

package org.webapp.ws;

    public class Hello {
        public String sayHello()
        {
            return "Hello ";
        }
    }

Wsdl档案---

 <?xml version="1.0" encoding="UTF-8"?>
        -<wsdl:definitions targetNamespace="http://ws.webapp.org" xmlns:soap12="http://schemas.xmlsoap.org/wsdl/soap12/" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:mime="http://schemas.xmlsoap.org/wsdl/mime/" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:http="http://schemas.xmlsoap.org/wsdl/http/" xmlns:wsaw="http://www.w3.org/2006/05/addressing/wsdl" xmlns:ns="http://ws.webapp.org" xmlns:ns1="http://org.apache.axis2/xsd" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"> <wsdl:documentation> Please Type your service description here </wsdl:documentation> 
        -<wsdl:types> -<xs:schema targetNamespace="http://ws.webapp.org" elementFormDefault="qualified" attributeFormDefault="qualified"> 
        -<xs:element name="sayHello"> 
        -<xs:complexType> <xs:sequence/> </xs:complexType> </xs:element> -<xs:element name="sayHelloResponse"> -<xs:complexType> -<xs:sequence> <xs:element name="return" type="xs:string" nillable="true" minOccurs="0"/> </xs:sequence> </xs:complexType> </xs:element> </xs:schema> </wsdl:types> 
        -<wsdl:message name="sayHelloRequest"> <wsdl:part name="parameters" element="ns:sayHello"/> </wsdl:message> 
        -<wsdl:message name="sayHelloResponse"> <wsdl:part name="parameters" element="ns:sayHelloResponse"/> </wsdl:message> 
        -<wsdl:portType name="HelloPortType"> 
        -<wsdl:operation name="sayHello"> <wsdl:input wsaw:Action="urn:sayHello" message="ns:sayHelloRequest"/> <wsdl:output wsaw:Action="urn:sayHelloResponse" message="ns:sayHelloResponse"/> </wsdl:operation> </wsdl:portType> 
        -<wsdl:binding name="HelloSoap11Binding" type="ns:HelloPortType"> <soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/> -<wsdl:operation name="sayHello"> <soap:operation style="document" soapAction="urn:sayHello"/> 
        -<wsdl:input> <soap:body use="literal"/> </wsdl:input> 
        -<wsdl:output> <soap:body use="literal"/> </wsdl:output> </wsdl:operation> </wsdl:binding> -<wsdl:binding name="HelloSoap12Binding" type="ns:HelloPortType"> <soap12:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/>
         -<wsdl:operation name="sayHello"> <soap12:operation style="document" soapAction="urn:sayHello"/> -<wsdl:input> <soap12:body use="literal"/> </wsdl:input>
         -<wsdl:output> <soap12:body use="literal"/> </wsdl:output> </wsdl:operation> </wsdl:binding> -<wsdl:binding name="HelloHttpBinding" type="ns:HelloPortType"> <http:binding verb="POST"/> -<wsdl:operation name="sayHello"> <http:operation location="sayHello"/>
         -<wsdl:input> <mime:content type="application/xml" part="parameters"/> </wsdl:input> -<wsdl:output> <mime:content type="application/xml" part="parameters"/> </wsdl:output> </wsdl:operation> </wsdl:binding> 
        -<wsdl:service name="Hello"> 
        -<wsdl:port name="HelloHttpSoap11Endpoint" binding="ns:HelloSoap11Binding">
         <soap:address location="http://localhost:8080/WebApp/services/Hello.HelloHttpSoap11Endpoint/"/> </wsdl:port> 
        -<wsdl:port name="HelloHttpSoap12Endpoint" binding="ns:HelloSoap12Binding"> <soap12:address location="http://localhost:8080/WebApp/services Hello.HelloHttpSoap12Endpoint/"/> </wsdl:port> 
        -<wsdl:port name="HelloHttpEndpoint" binding="ns:HelloHttpBinding"> 
        <http:address location="http://localhost:8080/WebApp/services/Hello.HelloHttpEndpoint/"/> </wsdl:port> </wsdl:service> </wsdl:definitions>

AndroidWSClientActivity.java:

    package com.android.ws;

    import org.ksoap2.SoapEnvelope;
    import org.ksoap2.serialization.PropertyInfo;
    import org.ksoap2.serialization.SoapObject;
    import org.ksoap2.serialization.SoapPrimitive;
    import org.ksoap2.serialization.SoapSerializationEnvelope;
    import org.ksoap2.transport.HttpTransportSE;

    import android.view.View;
    import android.widget.TextView;
    import android.widget.Toast;
    import android.app.Activity;
    import android.os.Bundle;

    public class AndroidWSClientActivity extends Activity {

        private static final String SOAP_ACTION = "http://ws.webapp.org/sayHello";
           private static final String METHOD_NAME = "sayHello";
    private static final String NAMESPACE ="http://ws.webapp.org"; 
    private static final String URL ="http://10.0.2.2:8080/WebApp/services/Hello?wsdl";
      private String Webresponse;
            private View handler;

        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
            webAccess();
        }
            public void webAccess(){
                 thread = new Thread(){

                public void run(){
                    try{
                      SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
                         SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
                         envelope.dotNet = true;
                         envelope.setOutputSoapObject(request);
                         HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
                         androidHttpTransport.call(SOAP_ACTION, envelope);
                            SoapPrimitive response = (SoapPrimitive)envelope.getResponse();
                            Webresponse = response.toString();
                            Toast.makeText(getApplicationContext(), " "+Webresponse, Toast.LENGTH_LONG).show();
                    }

                    catch(Exception e){
                     e.printStackTrace();
                    }

                   handler.post(createUI);
                   }
                  };

                  thread.start();
                 }


                 final Runnable createUI = new Runnable() {
                      private TextView textView;

                        public void run(){
                          textView.setText(Webresponse);
                 }
                 };

    }




 and in **main.xml**


    <TextView
          android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="hello1"/>

我下载了ksoap并将其添加到项目中。 Web服务器运行成功,android项目也运行,但是wsdl返回了值。 Wsdl文件在localhost上,所以10.0.2.2使用了,而且我也做了干净的项目。但没有给定输出。

我使用了minsdkversion-8和targetsdkversion-17。是否必须sdkversion是“17”? 在Hello.java文件中,我只返回了hello字符串。在这里,Webapp和AndroidWsClient都是不同的项目。

错误是 handler.post(createUI); 的Nullpointer异常 AndroidWSClient.java页面中的行。

0 个答案:

没有答案