这里用一个简单的Android例子来调用Java EE+axis2搭建的webservice服务。
说明:这里JavaEE+axis2搭建的webservice服务采用的是方法二生成aar包(http://blog.csdn.net/yuyinghua0302/article/details/50440506)
需要下载:ksoap2-android.jar
1 新建android项目,将ksoap2-android.jar包导入项目,对AndroidManifest.xml文件进行修改,添加网络访问权限,代码如下:
<uses-permission android:name="android.permission.INTERNET"/>
2 布局文件activity_main.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<EditText
android:id="@+id/name_editview"
android:layout_width="match_parent"
android:layout_height="52dp"
android:hint="请填入姓名"/>
<Button
android:id="@+id/people_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="获取名字" />
<TextView
android:id="@+id/show"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="AutoCompleteTextView" >
</TextView>
</LinearLayout>
3 新建负责请求webService的工具类WebServiceOpra.java,代码如下:
package com.example.utils;
import java.io.IOException;
import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
import org.xmlpull.v1.XmlPullParserException;
public class WebServiceOpra {
// 自定义类型参数
public static String people_name(String name) {
String SERVER_URL = "http://192.168.0.107:8080/axis2/services/TestNameSercive.TestNameSerciveHttpSoap12Endpoint/";
final HttpTransportSE httpSE = new HttpTransportSE(SERVER_URL);
String PACE = "http://method.example.com";
String M_NAME = "getName";
SoapObject soapObject = new SoapObject(PACE, M_NAME);
soapObject.addProperty("name", name);
final SoapSerializationEnvelope soapserial = new SoapSerializationEnvelope(SoapEnvelope.VER11);
soapserial.bodyOut = soapObject;
// 设置与.NET提供的Web service保持有良好的兼容性
soapserial.dotNet = true;
try {
httpSE.call(PACE + M_NAME, soapserial);
} catch (IOException e) {
e.printStackTrace();
} catch (XmlPullParserException e) {
e.printStackTrace();
}
SoapObject result = (SoapObject) soapserial.bodyIn;
System.out.println("result:" + result);
return result.toString();
}
}
其中SERVER_URL与PACE可以从从相关WSDL文档中查看(http://localhost:8080/axis2/services/TestNameSercive?wsdl)
注意:SERVER_URL中需要写的是ip地址,本地发布也不能写localhost
4 MainActivity.java文件代码如下
package com.example.testclient;
import com.example.utils.WebServiceOpra;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends Activity{
TextView show_tv;
Button people_btn;
EditText name_edit;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
show_tv = (TextView) findViewById(R.id.show);
people_btn = (Button) findViewById(R.id.people_button);
name_edit=(EditText)findViewById(R.id.name_editview);
people_btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
new Thread() {
@Override
public void run() {
super.run();
Looper.prepare();
String name=name_edit.getText().toString();
Log.i("name-----",name);
String result = WebServiceOpra.people_name(name);
Message msg = new Message();
msg.what = 0x12;
msg.obj = result;
mHandler.sendMessage(msg);
}
}.start();
}
});
}
Handler mHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
if (msg.what == 0x12) {
show_tv.setText(msg.obj.toString());
}
}
};
}
5 运行结果如下: