Android webservice 入门(2) -- 线程通信

Android webservice 入门(2) -- 线程通信

 

新建项目,直接将ksoap库复制到libs文件夹下面,如图

由于android 4.0 以上版本不能在主进程中进行通信,所以,我们必须创建一个线程,来进行数据通信。

重新定义类HttpThread 继承自 Thread

Run函数中内容为ksoap通信

然后,只要在oncreat()函数中新建线程,并启动线程,即可完成通信。

package baidumapsdk.demo;

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

import android.view.*;
import android.view.View.OnClickListener;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.*;

public class Login extends Activity {
	private String NameSpace = "http://tempuri.org/";
	private String MethodName = "LoginByUser"; //webservice调用的函数接口
	private String url = "http://115.199.65.89/Service1.asmx";  //webservice web服务器地址
	private String soapAction = NameSpace + MethodName;
	private EditText et;
	private EditText etPwd;
	private Button btLogin;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.login);
		ExitApplication.getInstance().addActivity(this);
		init();
	}
	@Override
	protected void onPause() {
		super.onPause();
	}
	private void init()
	{
		et = (EditText) findViewById(R.id.et);
		etPwd = (EditText) findViewById(R.id.etPwd);
		btLogin = (Button) findViewById(R.id.btLogin);
		btLogin.setOnClickListener(new OnClickListener() {
			public void onClick(View arg0)
			{
				if(validate()) 
				{
					ResponseOnClick();  //创建线程进行通信
				}
			}
		});
	}
	private void ResponseOnClick() {
		HttpThread thread = new HttpThread();
		thread.doStart();
	}


	private boolean validate()   //判定用户名密码是否为空
	{
		String username = et.getText().toString().trim();
		if (username.equals(""))
		{
			DialogUtil.showDialog(this, "用户账户是必填项!", false);
			return false;
		}
		String pwd = etPwd.getText().toString().trim();
		if (pwd.equals(""))
		{
			DialogUtil.showDialog(this, "用户口令是必填项!", false);
			return false;
		}
		return true;
	}

	private class HttpThread extends Thread{

		public HttpThread()
		{
		}

		/**
		 * 启动线程
		 */
		public void doStart(){
			this.start();
		}
		public void run() {
			String result = "false";
			try {
				//step1 指定WebService的命名空间和调用的方法名
				SoapObject request = new SoapObject(NameSpace, MethodName);	

				//step2 设置调用方法的参数值,这里的参数名称不一定和WebService一致
				request.addProperty("user", et.getText().toString());
				request.addProperty("pwd", etPwd.getText().toString());

				//step3 生成调用WebService方法的SOAP请求信息,并指定SOAP的版本
				SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
						SoapEnvelope.VER11);
				//设置是否调用的是dotNet下的WebService
				envelope.dotNet = true;
				//必须,等价于envelope.bodyOut = request; 
				envelope.setOutputSoapObject(request);

				//step4 创建HttpTransportSE对象
				HttpTransportSE ht = new HttpTransportSE(url);
				//step5 调用WebService
				ht.call(soapAction, envelope);

				//step6 使用getResponse方法获得WebService方法的返回结果
				if(envelope.getResponse()!=null){
					SoapPrimitive response = (SoapPrimitive) envelope.getResponse();
					result = response.toString();
				}

				if(!result.equals("false"))
				{
					Person p = new Person(et.getText().toString(),result,null);
					Bundle data =new Bundle();
					data.putSerializable("person", p);
					Intent intent = null;
					intent = new Intent(Login.this, Menu.class);
					
					//intent.setClass(Login.this, Menu.class);
					//intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
					
					intent.putExtras(data);
					startActivity(intent);
					//finish();
				}
				else
				{
					DialogUtil.showDialog(Login.this,"用户名或密码错误",false);   //对话框类,可以去掉
				}

			} catch (Exception e) {
				//result = e.getMessage();
				DialogUtil.showDialog(Login.this, "服务器响应异常,请稍后再试!", false);
			}

		}
	}

}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值