Android + Axis2

首先到Apache Axis2官网下载相应的War包,部署到Tomcat webApps文件夹下
下载地址: http://axis.apache.org/axis2

建立一个pojo,放到已经部署到tomcat的axis文件夹的WEB-INF的pojo文件夹下,如果没有自己建一个:
这个是pojo的java代码
import java.util.Random;

public class Test2 {

	public String sayHello(String name) {
		return name + " say: hello [axis2]";
	}
	
	public int getAge(int i) {
		return i + new Random().nextInt(100);
	}
}

下面是Android部分
下载最新的ksoap2-android-assembly-3.0.0-RC.2-jar-with-dependencies.jar包,
下载地址: http://code.google.com/p/ksoap2-android/wiki/HowToUse?tm=2

在Android Application Project 中建立一个文件夹,把Jar包放进去,并引入此Jar包
注意:一定要把这个Jar包所在的文件夹Build Path里设置为Use As Source Folder

接下来是Android Activity代码:

package com.example.testandroidwebservice;

import java.io.IOException;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.SoapFault;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
import org.xmlpull.v1.XmlPullParserException;



import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class LoginActivity extends Activity {
	
	private static final String NAME_SPACE = "http://ws.apache.org/axis2";
	private static final String WDSL_LINK = "http://192.168.1.102:8089/axis2/services/Test2?wsdl";
	private static final String METHOD_NAME = "sayHello";
	
	private Button call_soap;
	private EditText name_input;
	private TextView textView;
	
	private String resultStr;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        
        this.initUI();
        
        this.call_soap.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				callWebService();
			}
		});
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_login, menu);
        return true;
    }
    
    private void initUI() {
    	call_soap = (Button) this.findViewById(R.id.soap_button);
    	name_input = (EditText) this.findViewById(R.id.name_input);
    	textView = (TextView) this.findViewById(R.id.textView);
    }
    
    private void callWebService() {
    	
    	
		Thread thr = new Thread() {
			public void run() {
				try {
					String name = name_input.getText().toString();
					
					//Soap Object 是  SoapSerializationEnvelope的重要组成部分
					SoapObject request = new SoapObject(NAME_SPACE, METHOD_NAME);
					request.addProperty("name", name);
					
					SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
					envelope.bodyOut = request;
					envelope.dotNet = true;
					envelope.setOutputSoapObject(request);
					HttpTransportSE ht = new HttpTransportSE(WDSL_LINK);
					
					ht.call("", envelope);
					
					String ret = String.valueOf(envelope.getResponse());
					
					Log.d("resultStr = ", ret);
					setResultStr(ret);
					
					
				} catch (SoapFault e) {
					e.printStackTrace();
				} catch (IOException e) {
					e.printStackTrace();
				} catch (XmlPullParserException e) {
					e.printStackTrace();
				}
			}
		};
		
		thr.start();
		textView.setText(resultStr);
    	
    }

	public String getResultStr() {
		return resultStr;
	}

	public void setResultStr(String resultStr) {
		this.resultStr = resultStr;
	}
    
}


我发现调用WebService不能用本线程,另外,调用本机的WebService,不能用127.0.0.1和localhost,切记

猜你喜欢

转载自pskfire.iteye.com/blog/1706800