1.在项目导入ksoap2包([color=red]见附件[/color])
2.在AndrioidMainfest.xml中加入连接网络权限
3.Activity类
2.在AndrioidMainfest.xml中加入连接网络权限
<uses-permission android:name="android.permission.INTERNET"/>
3.Activity类
package com.apj;
import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.AndroidHttpTransport;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class WebServiceActivity extends Activity {
// /命名空间,要看wsdl文件确定,例如http://127.0.0.1:8080/seam3/VoteWebServiceImpl/VoteWebservice?wsdl的
// /命名空间为targetNamespace="http://apj.vote.webservice.com"
private static final String NAMESPACE = "http://apj.vote.com";
// /请求URL http://localhost:8080/seam3/VoteWebServoce/VoteWebService?wsdl
//android连接
private static String URL = "http://10.0.2.2:8080/seam3/VoteWebService/VoteWebService";
// /调用的方法
private static final String METHOD_NAME = "vote";
// /命名空间加方法名
private static String SOAP_ACTION = "http://apj.vote.com/vote";
TextView t1 =null;
Button button=null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
t1 =(TextView) findViewById(R.id.reTxt);
button =(Button)findViewById(R.id.botton);
button.setOnClickListener(new WebOnclick());
}
class WebOnclick implements OnClickListener {
@Override
public void onClick(View v) {
ResponseVote();
}
}
// //调用线程函数
public void ResponseVote() {
VoteThread thread = new VoteThread(loginHandler);
thread.dostart(1);
}
// //生成消息对象
Handler loginHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
// TODO Auto-generated method stub
super.handleMessage(msg);
switch (msg.what) {
case 1:
///从Message对象中得到WebService返回的信息
String ReturnMessage = msg.getData().getString("message");
int start = ReturnMessage.indexOf("=");
int end = ReturnMessage.indexOf(";");
// //截取字符串
String temp = ReturnMessage.substring(start+1, end);
t1.setText(temp);
System.out.println("ReturnMessage=" + ReturnMessage);
break;
case 2:
break;
case 3:
new AlertDialog.Builder(WebServiceActivity.this).setTitle("error")
.setMessage("网络连接出错").setNeutralButton("返回",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dlg,
int sumthin) {
}
}).show();
}
}
};
///连接线程类
public class VoteThread extends Thread{
public Handler handler = null;
public int vtId;
public ProgressDialog progressDialog;
///构造函数
public VoteThread(Handler handler)
{
this.handler=handler;
}
// /线程开始
public void dostart(int voteTeam) {
// /把参数传入
this.vtId=voteTeam;
progressDialog = new ProgressDialog(WebServiceActivity.this);
progressDialog.setTitle("网络连接");
progressDialog.setMessage("正在投票中,请稍后..");
progressDialog.setIndeterminate(true);
progressDialog.setButton("取消",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
progressDialog.cancel();
}
});
progressDialog
.setOnCancelListener(new DialogInterface.OnCancelListener() {
public void onCancel(DialogInterface dialog) {
}
});
progressDialog.show();
this.start(); // 线程开始了
}
// //调用webService
public void callWeb() {
try {
// /实例化soapObject对象
SoapObject rpc = new SoapObject(NAMESPACE, METHOD_NAME);
// /设置参数in0,传入
// rpc.setProperty(0, 1);
// /设置传入webservice的参数teamid
rpc.addProperty("teamid", 1);
// /Android传输对象
AndroidHttpTransport ht = new AndroidHttpTransport(URL);
ht.debug = true;
// /获得序列化的envelope
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
SoapEnvelope.VER11);
envelope.bodyOut = rpc;
envelope.dotNet = true;
envelope.setOutputSoapObject(rpc);
// 调用WebService的方法
ht.call(SOAP_ACTION, envelope);
//用SoapObject对象接受返回结果
SoapObject result = (SoapObject) envelope.bodyIn;
if (result != null) {
String str = result.toString();
System.out.println("return result--->"+str);
// //取消进去对话框
progressDialog.dismiss();
// /构造消息,验证通过
Message message = handler.obtainMessage();
Bundle bundle = new Bundle();
message.what = 1;// /这是消息的类型
bundle.putString("message", str);
//将数据封装到Message对象中传递给线程之外的Handler
message.setData(bundle);
// 传送消息
handler.sendMessage(message);
}
else
{
System.out.println(" vote faile");
}
return;
} catch (Exception e) {
System.out.println("IO error");
progressDialog.dismiss();
// /构造消息,验证通过
Message message = handler.obtainMessage();
Bundle bundle = new Bundle();
message.what = 3;// /这是消息的类型
bundle.putString("error", e.getMessage());
//将数据封装到Message对象中传递给线程之外的Handler
message.setData(bundle);
// 传送消息
handler.sendMessage(message);
e.printStackTrace();
}
}
@Override
public void run() {
// TODO Auto-generated method stub
super.run();
System.out.println("VoteThread----->"+ Thread.currentThread().getId());
callWeb();
}
}
}