基于axis2的webservice和android简单的本地数据交互

服务端具体生成(具体内容):
http://blog.sina.com.cn/s/blog_674522cd01010v66.html

PC客户端测试
package cn.ac.iscas.gz.sems.test;

import java.rmi.RemoteException;

import org.apache.axis2.AxisFault;

import cn.ac.iscas.gz.sems.webservicedemo.HelloWorldStub;

public class webserviceClientTest {
	
	public static void main(String[] args){
		try {
			HelloWorldStub hss = new HelloWorldStub();
			
			HelloWorldStub.SayHello request = new HelloWorldStub.SayHello();
			
			request.setName("萧_瑟");//为sayHello设置参数
			
			HelloWorldStub.SayHelloResponse response = hss.sayHello(request);
			
			String res = response.get_return();//得到返回结果
			
			System.out.println(res);
		} catch (AxisFault e) {
			e.printStackTrace();
		} catch (RemoteException e) {
			e.printStackTrace();
		}
	}

}


android客户端测试
package gz.iscas.ac.cn.webService;

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

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

public class WebServiceDemoAndroidActivity extends Activity {
	private static String NAMESPACE = "http://webServiceDemo.sems.gz.iscas.ac.cn";
	// webService地址
//	private static String URL = "http://10.0.2.2:8090/axis2/services/helloWorld?wsdl";
	private static String URL = "http://192.168.17.76:8090/axis2/services/helloWorld?wsdl";
	private static final String METHOD_NAME = "sayHello";

	private Button okButton;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		okButton = (Button) findViewById(R.id.ok);
		okButton.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				showTest();
			}
		});
	}

	private void showTest() {
		// (1) 指定webservice的命名空间和调用的方法名
		SoapObject rpc = new SoapObject(NAMESPACE, METHOD_NAME);
		/**
		 * (2) 设置调用方法的参数值,如果没有参数,可以省略。 要注意的是,addProperty方法的第1个参数虽然表示调用方法的参数名,
		 * 但该参数值并不一定与服务端的WebService类中的方法参数名一致,只要设置参数的顺序一致即可。
		 */
		rpc.addProperty("name", "萧_瑟");
		// (3) 生成调用Webservice方法的SOAP请求信息。该信息由SoapSerializationEnvelope对象描述
		SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
				SoapEnvelope.VER11);
		envelope.bodyOut = rpc;
		envelope.dotNet = true;
		envelope.setOutputSoapObject(rpc);
		// (4)创建HttpTransportsSE对象。通过AndroidHttpTransport类的构造方法可以指定WebService的WSDL文档的URL
		HttpTransportSE ht = new HttpTransportSE(URL);
		try {
			// (5)使用call方法调用WebService方法
			// Call方法的第一个参数一般为null,第2个参数就是在第3步创建的SoapSerializationEnvelope对象
			// ht.call(SOAP_ACTION, envelope);
			ht.call(null, envelope);
			// (6)使用getResponse方法获得WebService方法的返回结果
			String result = String.valueOf(envelope.getResponse());
			System.out.println("result " + result.toString());
			Toast.makeText(WebServiceDemoAndroidActivity.this, result,
					Toast.LENGTH_LONG).show();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}


AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="gz.iscas.ac.cn.webService"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="7" />

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:label="@string/app_name"
            android:name=".WebServiceDemoAndroidActivity" >
            <intent-filter >
                <action android:name="android.intent.action.MAIN" />

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

    <uses-permission android:name="android.permission.INTERNET" />

</manifest>




http://115.com/file/c2b72qy5

你可能感兴趣的:(android,webservice,axis2)