1

我的本地主机中有一个网络服务有这个链接

http://localhost:8085/firstWS/services/getSum?wsdl

其中包含此 xml 代码

<wsdl:definitions xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:ns1="http://org.apache.axis2/xsd" xmlns:ns="http://omar" xmlns:wsaw="http://www.w3.org/2006/05/addressing/wsdl" xmlns:http="http://schemas.xmlsoap.org/wsdl/http/" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:mime="http://schemas.xmlsoap.org/wsdl/mime/" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:soap12="http://schemas.xmlsoap.org/wsdl/soap12/" targetNamespace="http://omar">
<wsdl:documentation>Please Type your service description here</wsdl:documentation>
<wsdl:types>
<xs:schema attributeFormDefault="qualified" elementFormDefault="qualified" targetNamespace="http://omar">
<xs:element name="sum">
<xs:complexType>
<xs:sequence>
<xs:element minOccurs="0" name="a" type="xs:int"/>
<xs:element minOccurs="0" name="b" type="xs:int"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="sumResponse">
<xs:complexType>
<xs:sequence>
<xs:element minOccurs="0" name="return" type="xs:int"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
</wsdl:types>
<wsdl:message name="sumRequest">
<wsdl:part name="parameters" element="ns:sum"/>
</wsdl:message>
<wsdl:message name="sumResponse">
<wsdl:part name="parameters" element="ns:sumResponse"/>
</wsdl:message>
<wsdl:portType name="getSumPortType">
<wsdl:operation name="sum">
<wsdl:input message="ns:sumRequest" wsaw:Action="urn:sum"/>
<wsdl:output message="ns:sumResponse" wsaw:Action="urn:sumResponse"/>
</wsdl:operation>
</wsdl:portType>
<wsdl:binding name="getSumSoap11Binding" type="ns:getSumPortType">
<soap:binding transport="http://schemas.xmlsoap.org/soap/http" style="document"/>
<wsdl:operation name="sum">
<soap:operation soapAction="urn:sum" style="document"/>
<wsdl:input>
<soap:body use="literal"/>
</wsdl:input>
<wsdl:output>
<soap:body use="literal"/>
</wsdl:output>
</wsdl:operation>
</wsdl:binding>
<wsdl:binding name="getSumSoap12Binding" type="ns:getSumPortType">
<soap12:binding transport="http://schemas.xmlsoap.org/soap/http" style="document"/>
<wsdl:operation name="sum">
<soap12:operation soapAction="urn:sum" style="document"/>
<wsdl:input>
<soap12:body use="literal"/>
</wsdl:input>
<wsdl:output>
<soap12:body use="literal"/>
</wsdl:output>
</wsdl:operation>
</wsdl:binding>
<wsdl:binding name="getSumHttpBinding" type="ns:getSumPortType">
<http:binding verb="POST"/>
<wsdl:operation name="sum">
<http:operation location="sum"/>
<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="getSum">
<wsdl:port name="getSumHttpSoap11Endpoint" binding="ns:getSumSoap11Binding">
<soap:address location="http://localhost:8085/firstWS/services/getSum.getSumHttpSoap11Endpoint/"/>
</wsdl:port>
<wsdl:port name="getSumHttpSoap12Endpoint" binding="ns:getSumSoap12Binding">
<soap12:address location="http://localhost:8085/firstWS/services/getSum.getSumHttpSoap12Endpoint/"/>
</wsdl:port>
<wsdl:port name="getSumHttpEndpoint" binding="ns:getSumHttpBinding">
<http:address location="http://localhost:8085/firstWS/services/getSum.getSumHttpEndpoint/"/>
</wsdl:port>
</wsdl:service>
</wsdl:definitions>

我正在使用这个java代码从服务器获取数据

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.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.TextView;

public class MainActivity extends Activity {
    TextView tv;
     private static final String NAMESPACE = "http://omar/";
     private static String URL = "http://192.168.1.3:8085/firstWS/services/getSum?wsdl"; 
     private static final String METHOD_NAME = "sum";
     private static final String SOAP_ACTION =  "getSum";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        tv=(TextView)findViewById(R.id.hellotv);
        SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME); 


          PropertyInfo propInfo=new PropertyInfo();
          propInfo.name="a";
          propInfo.type=PropertyInfo.INTEGER_CLASS;

          request.addPropertyIfValue(propInfo, 5);  

          PropertyInfo propInfo2=new PropertyInfo();
          propInfo.name="b";
          propInfo.type=PropertyInfo.INTEGER_CLASS;

          request.addPropertyIfValue(propInfo, 6);

          SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11); 

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

          try {
           androidHttpTransport.call(SOAP_ACTION, envelope);

            SoapPrimitive  resultsRequestSOAP = (SoapPrimitive) envelope.getResponse();


           tv.setText(resultsRequestSOAP.toString());


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

          }

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }

}

但是当我运行应用程序时,我从 logcat 收到了这个错误

02-20 05:22:00.951: W/System.err(31571): java.lang.NullPointerException
02-20 05:22:00.961: W/System.err(31571):    at java.io.Writer.write(Writer.java:141)
02-20 05:22:00.961: W/System.err(31571):    at org.kxml2.io.KXmlSerializer.startTag(KXmlSerializer.java:412)
02-20 05:22:00.961: W/System.err(31571):    at org.ksoap2.serialization.SoapSerializationEnvelope.writeObjectBody(SoapSerializationEnvelope.java:594)
02-20 05:22:00.961: W/System.err(31571):    at org.ksoap2.serialization.SoapSerializationEnvelope.writeObjectBody(SoapSerializationEnvelope.java:573)
02-20 05:22:00.961: W/System.err(31571):    at org.ksoap2.serialization.SoapSerializationEnvelope.writeElement(SoapSerializationEnvelope.java:651)
02-20 05:22:00.961: W/System.err(31571):    at org.ksoap2.serialization.SoapSerializationEnvelope.writeBody(SoapSerializationEnvelope.java:555)
02-20 05:22:00.961: W/System.err(31571):    at org.ksoap2.SoapEnvelope.write(SoapEnvelope.java:205)
02-20 05:22:00.961: W/System.err(31571):    at org.ksoap2.transport.Transport.createRequestData(Transport.java:132)
02-20 05:22:00.961: W/System.err(31571):    at org.ksoap2.transport.HttpTransportSE.call(HttpTransportSE.java:139)
02-20 05:22:00.961: W/System.err(31571):    at org.ksoap2.transport.HttpTransportSE.call(HttpTransportSE.java:114)
02-20 05:22:00.961: W/System.err(31571):    at com.example.wsclient.MainActivity.onCreate(MainActivity.java:49)
02-20 05:22:00.961: W/System.err(31571):    at android.app.Activity.performCreate(Activity.java:5191)
02-20 05:22:00.961: W/System.err(31571):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)
02-20 05:22:00.961: W/System.err(31571):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2064)
02-20 05:22:00.961: W/System.err(31571):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2125)
02-20 05:22:00.961: W/System.err(31571):    at android.app.ActivityThread.access$600(ActivityThread.java:140)
02-20 05:22:00.961: W/System.err(31571):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1227)
02-20 05:22:00.961: W/System.err(31571):    at android.os.Handler.dispatchMessage(Handler.java:99)
02-20 05:22:00.961: W/System.err(31571):    at android.os.Looper.loop(Looper.java:137)
02-20 05:22:00.961: W/System.err(31571):    at android.app.ActivityThread.main(ActivityThread.java:4898)
02-20 05:22:00.961: W/System.err(31571):    at java.lang.reflect.Method.invokeNative(Native Method)
02-20 05:22:00.961: W/System.err(31571):    at java.lang.reflect.Method.invoke(Method.java:511)
02-20 05:22:00.961: W/System.err(31571):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1008)
02-20 05:22:00.961: W/System.err(31571):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:775)
02-20 05:22:00.961: W/System.err(31571):    at dalvik.system.NativeStart.main(Native Method)
4

2 回答 2

1

代替

http://192.168.1.3:8085/firstWS/services/getSum?wsdl  

利用

http://10.0.2.2:8085/firstWS/services/getSum?wsdl

在您的 PC 本身中运行 Web 服务时使用“10.0.2.2”而不是“localhost”

于 2013-02-20T03:24:06.227 回答
0

问题在于您添加请求参数的方式。好的代码是:

PropertyInfo propInfo=new PropertyInfo();
propInfo.name="a";
propInfo.type=PropertyInfo.INTEGER_CLASS;

request.addPropertyIfValue(propInfo, 5);  

PropertyInfo propInfo2=new PropertyInfo();
propInfo2.name="b";
propInfo2.type=PropertyInfo.INTEGER_CLASS;

request.addPropertyIfValue(propInfo2, 6);
于 2013-02-21T01:35:44.250 回答