Android基于ksoap2的WebService通信客户端demo

闻人嘉木
2023-12-01

瞎扯:出差内蒙闲来无事写写,干燥、、、每天要喝3瓶水,口味重的很、、、连奶茶都是咸的,第一次感觉到我的口语是这么不biu准。

瞎扯完毕、、、进入正文,

一、先贴wsdl

This XML file does not appear to have any style information associated with it. The document tree is shown below.
<!--
 Published by JAX-WS RI at http://jax-ws.dev.java.net. RI's version is JAX-WS RI 2.1.3-hudson-390-. 
-->
<!--
 Generated by JAX-WS RI at http://jax-ws.dev.java.net. RI's version is JAX-WS RI 2.1.3-hudson-390-. 
-->
<definitions xmlns="http://schemas.xmlsoap.org/wsdl/" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns="http://ws.test.mars/" xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" xmlns:xsd="http://www.w3.org/2001/XMLSchema" name="TServiceService" targetNamespace="http://ws.test.mars/">
<span style="white-space:pre">	</span><types>
<span style="white-space:pre">		</span><xsd:schema>
<span style="white-space:pre">			</span><xsd:import namespace="http://ws.test.mars/" schemaLocation="http://localhost:8080/webservice_t01_wp/TServicePort?xsd=1"/>
<span style="white-space:pre">		</span></xsd:schema>
<span style="white-space:pre">	</span></types>
<span style="white-space:pre">	</span><message name="test">
<span style="white-space:pre">		</span><part element="tns:test" name="parameters"/>
<span style="white-space:pre">	</span></message>
<span style="white-space:pre">	</span><message name="testResponse">
<span style="white-space:pre">		</span><part element="tns:testResponse" name="parameters"/>
<span style="white-space:pre">	</span></message>
<span style="white-space:pre">	</span><portType name="TServiceDelegate">
<span style="white-space:pre">		</span><operation name="test">
<span style="white-space:pre">			</span><input message="tns:test"/>
<span style="white-space:pre">			</span><output message="tns:testResponse"/>
<span style="white-space:pre">		</span></operation>
<span style="white-space:pre">	</span></portType>
<span style="white-space:pre">	</span><binding name="TServicePortBinding" type="tns:TServiceDelegate">
<span style="white-space:pre">		</span><soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/>
<span style="white-space:pre">		</span><operation name="test">
<span style="white-space:pre">			</span><soap:operation soapAction=""/>
<span style="white-space:pre">			</span><input>
<span style="white-space:pre">				</span><soap:body use="literal"/>
<span style="white-space:pre">			</span></input>
<span style="white-space:pre">			</span><output>
<span style="white-space:pre">				</span><soap:body use="literal"/>
<span style="white-space:pre">			</span></output>
<span style="white-space:pre">		</span></operation>
<span style="white-space:pre">	</span></binding>
<span style="white-space:pre">	</span><service name="TServiceService">
<span style="white-space:pre">		</span><port binding="tns:TServicePortBinding" name="TServicePort">
<span style="white-space:pre">			</span><soap:address location="http://localhost:8080/webservice_t01_wp/TServicePort"/>
<span style="white-space:pre">		</span></port>
<span style="white-space:pre">	</span></service>
</definitions>

This XML file does not appear to have any style information associated with it. The document tree is shown below.
<!--
 Published by JAX-WS RI at http://jax-ws.dev.java.net. RI's version is JAX-WS RI 2.1.3-hudson-390-. 
-->
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:tns="http://ws.test.mars/" targetNamespace="http://ws.test.mars/" version="1.0">
	<xs:element name="test" type="tns:test"/>
	<xs:element name="testResponse" type="tns:testResponse"/>
	<xs:complexType name="test">
		<xs:sequence>
			<xs:element minOccurs="0" name="arg0" type="xs:string"/>
		</xs:sequence>
	</xs:complexType>
	<xs:complexType name="testResponse">
		<xs:sequence>
			<xs:element minOccurs="0" name="return" type="xs:string"/>
		</xs:sequence>
	</xs:complexType>
</xs:schema>

二、上MainActivity  Const  WsControler代码

MainActivity  

package com.example.wsandroidclient.activity;

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

import com.example.wsandroidclient.R;
import com.example.wsandroidclient.controler.WsControler;
/**
 * 
 * @author Administrator
 *
 */
public class MainActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
	}
	
	
	public void connect(View view){ 
		String returnvalue=WsControler.test("123112");
		Toast.makeText(getApplication(), returnvalue, 1).show();
	}
}

Const

package com.example.wsandroidclient.comment;

public class Const {

	public class WSConst {
		public class t01 {
			public static final String URL="http://192.168.11.124:8080/webservice_t01_wp/TServicePort?wsdl";
			
			public static final String nameSpace="http://ws.test.mars/";
			
			public static final String test_mathName="test";
		}

	}
}

WsControler

package com.example.wsandroidclient.controler;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

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.util.Log;

import com.example.wsandroidclient.comment.Const;


/**
 * webserviceControler
 * @author Administrator
 *
 */
public class WsControler {
	
	public static final String Tag="WSCONTROLER";
	
	public static String test(String str){
		
		//1  HttpTransportSE
		final HttpTransportSE httpTransportSE = new HttpTransportSE(Const.WSConst.t01.URL);
		httpTransportSE.debug=true;
		
		//2 SoapObject
		SoapObject soapObject = new SoapObject(Const.WSConst.t01.nameSpace, Const.WSConst.t01.test_mathName);
		//3  添加请求参数    
//		soapObject.addProperty("arg0", str);
		PropertyInfo propertyInfo=new PropertyInfo();
		propertyInfo.setName("arg0");
		propertyInfo.setValue(str);  
		soapObject.addProperty(propertyInfo);
		Log.d(Tag, str); 
		//4 SoapSerializationEnvelope  
		final SoapSerializationEnvelope soapSerializationEnvelope = new SoapSerializationEnvelope(SoapEnvelope.VER10);
		soapSerializationEnvelope.bodyOut=soapObject;
		soapSerializationEnvelope.setOutputSoapObject(soapObject);
		soapSerializationEnvelope.dotNet= false;//这里如果设置为TRUE,那么在服务器端将获取不到参数值(如:将这些数据插入到数据库中的话) 
		
		//线程接口
		FutureTask<String> futureTask = new FutureTask<String>(new Callable<String>() {
			
			@Override
			public String call() throws Exception {
				String returnvalue="";
				// 调用  //Const.WSConst.t01.nameSpace+Const.WSConst.t01.test_mathName
				httpTransportSE.call(null, soapSerializationEnvelope);
				// 获取返回信息  
				if(soapSerializationEnvelope.getResponse()!=null){ 
					 SoapPrimitive primitive =  (SoapPrimitive) soapSerializationEnvelope.getResponse();
					 Log.d(Tag, primitive.toString()+""); 
					 returnvalue=primitive.toString(); 
				}
				return returnvalue;
			}
		});
		
		//开启线程
		new Thread(futureTask).start();
		
		try {
			//线程完成回调
			return futureTask.get();
		} catch (InterruptedException | ExecutionException e) {
			e.printStackTrace();
		}
		
		return str;
		
	}
}


三、注意加权限:

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

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="19" />

    <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.example.wsandroidclient.activity.MainActivity"
            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>


四、请求及响应数据  有点乱码将就看了

POST /webservice_t01_wp/TServicePort?wsdl HTTP/1.1
User-Agent: ksoap2-android/2.6.0+
SOAPAction: ""
Content-Type: text/xml;charset=utf-8
Accept-Encoding: gzip
Host: 127.0.0.1:8081
Connection: Keep-Alive
Content-Length: 362

<v:Envelope xmlns:i="http://www.w3.org/1999/XMLSchema-instance" xmlns:d="http://www.w3.org/1999/XMLSchema" xmlns:c="http://schemas.xmlsoap.org/soap/encoding/" xmlns:v="http://schemas.xmlsoap.org/soap/envelope/">
	<v:Header />
	<v:Body>
		<n0:test id="o0" c:root="1" xmlns:n0="http://ws.test.mars/">
			<arg0 i:type="d:string">123112</arg0>
		</n0:test>
	</v:Body>
</v:Envelope>

HTTP/1.1 200 OK
Server: Apache-Coyote/1.1
Content-Type: text/xml;charset=utf-8
Transfer-Encoding: chunked
Date: Thu, 16 Jun 2016 08:42:17 GMT

f0
<?xml version="1.0" ?>
<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
	<S:Body>
		<ns2:testResponse xmlns:ns2="http://ws.test.mars/">
			<return>鏉ヨ嚜鏈嶅姟绔殑杩斿洖1111锛�23112</return>
		</ns2:testResponse>
	</S:Body>
</S:Envelope>
0



下载:

客户端:http://download.csdn.net/detail/zlin3007/9551763

服务端:http://download.csdn.net/detail/zlin3007/9551738

 类似资料: