1

Я создал веб-сервис для выполнения добавления. Я создал приложение для Android, чтобы использовать этот веб-сервис и отображать вывод в TextView. Пожалуйста, найдите ниже WSDL и кодAndroid: вызов веб-службы с помощью ksoap2

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> 

Я чувствую вебсервис не вызывался. Я использовал текстовое представление для вывода вывода. Пожалуйста, помогите мне с этим. Я не получаю никакого вывода :(

Даже после добавления Log.e ("Объект ответа", response.toString());

Я не могу увидеть что-нибудь в LogCat

! Большое спасибо !!

+0

Я полагаю, ваш андроид и сервер? находятся на одной машине (потому что вы используете localhost)? если да, localhost будет считаться эмулятором Android, вы должны использовать IP 10.0.2.2 вместо localhost. SEE http://developer.android.com/tools/devices/emulator.html#networkaddresses – shadesco

ответ

0

URL является HTTP: // локальными: 8080/Мониш/TestWebservice WSDL Это означает, что веб-сервис прослушивает андроид устройства

+0

да! Но webserive не получилось назвать, я верю. Я не могу видеть ответ в LogCat либо – shockwave

+1

. В любом случае вы должны использовать URL-адрес службы (местоположение в WSDL), а не URL-адрес WSDL. – esentsov

+0

И как выглядит код серверного приложения для Android? –

Смежные вопросы