1

我创建了一个webservice来执行添加。我创建了一个Android应用程序来使用该Web服务并在TextView中显示输出。请看以下WSDL和代码Android:使用kso​​ap2调用webservice

WSDL

<wsdl:definitions 
xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" 
xmlns:soapjms="http://www.w3.org/2010/soapjms/" 
xmlns:http="http://schemas.xmlsoap.org/wsdl/http/" 
xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" 
xmlns:tns="http://localhost:8080/Monish/TestWebservice" 
xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" 
xmlns:mime="http://schemas.xmlsoap.org/wsdl/mime/" 
xmlns:soap12="http://schemas.xmlsoap.org/wsdl/soap12/"name="TestWebservice" 
targetNamespace="http://localhost:8080/Monish/TestWebservice"> 
<wsdl:types> 
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
xmlns:tns="http://localhost:8080/Monish/TestWebservice" 
targetNamespace="http://localhost:8080/Monish/TestWebservice"> 
    <xsd:element name="myTest" type="tns:myTest"/> 
    <xsd:element name="myTestResponse" type="tns:myTestResponse"/> 
<xsd:complexType name="myTest"> 
    <xsd:sequence> 
    <xsd:element name="a" nillable="true" type="xsd:string"/> 
    <xsd:element name="b" nillable="true" type="xsd:string"/> 
    </xsd:sequence> 
    </xsd:complexType> 
<xsd:complexType name="myTestResponse"> 
<xsd:sequence> 
<xsd:element name="output" nillable="true" type="xsd:string"/> 
</xsd:sequence> 
</xsd:complexType> 
</xsd:schema> 
</wsdl:types> 
<wsdl:message name="TestWebservice_PortType_myTestResponse"> 
<wsdl:part name="parameters" element="tns:myTestResponse"></wsdl:part> 
</wsdl:message> 
<wsdl:message name="TestWebservice_PortType_myTest"> 
<wsdl:part name="parameters" element="tns:myTest"></wsdl:part> 
</wsdl:message> 
<wsdl:portType name="TestWebservice_PortType"> 
<wsdl:operation name="myTest"> 
<wsdl:input message="tns:TestWebservice_PortType_myTest"></wsdl:input> 
<wsdl:output message="tns:TestWebservice_PortType_myTestResponse"></wsdl:output> 
</wsdl:operation> 
</wsdl:portType> 
<wsdl:binding name="Monish_TestWebservice_Binder" type="tns:TestWebservice_PortType"> 
<soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/> 
<wsdl:operation name="myTest"> 
<soap:operation soapAction="Monish_TestWebservice_Binder_myTest" style="document"/> 
<wsdl:input> 
<soap:body parts="parameters" use="literal"/> 
</wsdl:input> 
<wsdl:output> 
<soap:body parts="parameters" use="literal"/> 
    </wsdl:output> 
    </wsdl:operation> 
    </wsdl:binding> 
<wsdl:service name="TestWebservice"> 
<wsdl:port name="Monish_TestWebservice_Port" binding="tns:Monish_TestWebservice_Binder"> 
<soap:address location="http://localhost:8080/ws/Monish:TestWebservice/Monish_TestWebservice_Port"/> 
    </wsdl:port> 
    </wsdl:service> 
</wsdl:definitions> 

代码

package com.webservice; 

public class WebServiceDemoActivity extends Activity { 
private final String NAMESPACE = "http://localhost:8080/Monish/TestWebservice/"; 
private final String URL = "http://localhost:8080/Monish/TestWebservice?WSDL"; 
private final String SOAP_ACTION = "Monish_TestWebservice_Binder_myTest"; 
private final String METHOD_NAME = "myTest"; 

    ArrayList<Guard> guardList=new ArrayList<Guard>(); 
    // Called when the activity is first created. 
    @TargetApi(Build.VERSION_CODES.GINGERBREAD) 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
     if (android.os.Build.VERSION.SDK_INT > 9) { 
      StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build(); 
      StrictMode.setThreadPolicy(policy); 
     } 
     SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME); 
     PropertyInfo propInfo=new PropertyInfo(); 
     propInfo.type=PropertyInfo.INTEGER_CLASS; 
        //adding parameters 
     request.addProperty("a", 10); 
     request.addProperty("b", 15); 
     SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11); 
     envelope.dotNet = false; 
     envelope.setOutputSoapObject(request); 
     HttpTransportSE androidHttpTransport = new HttpTransportSE(URL); 
     try { 
      androidHttpTransport.call(SOAP_ACTION, envelope); 
      SoapObject response = (SoapObject)envelope.getResponse(); 


      Log.e("Object response", response.toString()); 
      TextView tv = new TextView(this); 
      tv.setText("Output: "+response.toString()); 
      setContentView(tv); 
     } catch (Exception e) { 
       e.printStackTrace(); 
      } 
    } 
} 

main.xml中

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:orientation="vertical" > 

    <TextView 
    android:id="@+id/textView1" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="" /> 


</LinearLayout> 

AndroidManifest.xml中

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
package="com.webservice" 
android:versionCode="1" 
android:versionName="1.0" > 

<uses-sdk 
    android:minSdkVersion="8" 
    android:targetSdkVersion="18" /> 
<uses-permission android:name="android.permission.INTERNET"/> 

<application 
    android:allowBackup="true" 
    android:icon="@drawable/ic_launcher" 
    android:label="@string/app_name" 
    android:theme="@style/AppTheme" > 
    <activity 
     android:name="com.webservice.WebServiceDemoActivity" 
     android:label="@string/app_name" > 
     <intent-filter> 
      <action android:name="android.intent.action.MAIN" /> 

      <category android:name="android.intent.category.LAUNCHER" /> 
     </intent-filter> 
    </activity> 
</application> 

</manifest> 

我觉得web服务是没有得到调用。我用文本视图来显示输出。这个你能帮我吗。林没有得到任何输出:(

即使加入 Log.e后( “目标响应”,response.toString());

我不能看到任何在logcat的

!非常感谢!

+0

我假设你的Android和服务器?在同一台机器上(因为你使用的是本地主机)?如果是的话,localhost将被视为android的模拟器,你应该使用IP 10.0.2.2而不是localhost。请参阅http://developer.android.com/tools/devices/emulator.html#networkaddresses – shadesco

回答

0

网址为http:// 本地主机:8080/Monish/TestWebservice WSDL 这意味着,网络服务是Android设备上收听

+0

是的!但是webserive没有被调用,我相信。我无法在LogCat中看到响应 – shockwave

+1

在一种情况下,您应该使用服务URL(WSDL中的位置),而不是WSDL URL。 – esentsov

+0

如何看起来服务器端Android应用程序的代码? –

相关问题