RSS订阅优然探索
你的位置:首页 » Android » 正文

Android 连接Webservice

选择字号: 超大 标准 发布时间:2011-3-25 14:57:47 | 作者:admin | 0个评论 | 人浏览


import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
import org.kxml2.kdom.Element;
import org.kxml2.kdom.Node;

import android.app.ProgressDialog;
import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.util.Log;

public class HttpThread extends Thread {
 private Handler handle = null;
 String url;
 String nameSpace;
 String methodName;
 String soapAction;
 String userName;
 String userPwd;

 HashMap<String, Object> params = null;
 ProgressDialog progressDialog = null;

 public HttpThread(Handler hander) {
  handle = hander;
 }

 public void doStart(Context context, String url, String nameSpace,
   String methodName, HashMap<String, Object> params) {
  this.url = url;
  this.nameSpace = nameSpace;
  this.methodName = methodName;
  this.soapAction = nameSpace + methodName;
  this.params = params;
  this.userName = null;
  this.userPwd = null;

  progressDialog = ProgressDialog.show(context, "提示", "请稍等...", true);
  this.start();
 }

 public void doStart(Context context, String url, String nameSpace,
   String methodName, HashMap<String, Object> params,
   boolean showProgressDialog) {
  this.url = url;
  this.nameSpace = nameSpace;
  this.methodName = methodName;
  this.soapAction = nameSpace + methodName;
  this.params = params;
  this.userName = null;
  this.userPwd = null;

  if (showProgressDialog) {
   progressDialog = ProgressDialog.show(context, "提示", "请稍等...", true);
  }
  this.start();
 }

 public void doStart(Context context, String url, String nameSpace,
   String methodName) {
  this.url = url;
  this.nameSpace = nameSpace;
  this.methodName = methodName;
  this.soapAction = nameSpace + methodName;
  this.params = null;
  this.userName = null;
  this.userPwd = null;

  // progressDialog=ProgressDialog.show(context, "提示", "请稍等...",true);
  this.start();
 }

 public void doStart(Context context, String url, String nameSpace,
   String methodName, HashMap<String, Object> params, String userName,
   String userPwd) {
  this.url = url;
  this.nameSpace = nameSpace;
  this.methodName = methodName;
  this.soapAction = nameSpace + methodName;
  this.params = params;
  this.userName = userName;
  this.userPwd = userPwd;

  progressDialog = ProgressDialog.show(context, "提示", "请稍等...", true);
  this.start();
 }

 /*
  * 线程运行
  *
  * @see java.lang.Thread#run()
  */
 @Override
 public void run() {
  super.run();
  try {

   SoapObject so = (SoapObject) CallWebService();
   String list = so.getProperty(methodName + "Result").toString();
   Message message = handle.obtainMessage();
   message.obj = list;
   handle.sendMessage(message);
  } catch (Exception e) {
   e.printStackTrace();
   Log.e("LifePayment", "run:" + e.getMessage());
  } finally {
   // 取消进度对话框
   if (null != progressDialog) {
    progressDialog.dismiss();
   }
  }
 }

 protected Object CallWebService() {
  // 创建SoapObject实例
  SoapObject soapObeject = new SoapObject(nameSpace, methodName);
  // 生成调用Webservice方法的Soap请求
  SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
    SoapEnvelope.VER11);// 1.0=10 1.1=11 1.2=12

  // 设置soapHeader
  if (null != userName && null != userPwd) {
   Element[] header = new Element[1];
   header[0] = new Element().createElement(nameSpace, "MySoapHeader");
   Element username = new Element().createElement(nameSpace,
     "UserName");
   username.addChild(Node.TEXT, this.userName);
   header[0].addChild(Node.ELEMENT, username);
   Element pass = new Element().createElement(nameSpace, "UserPwd");
   pass.addChild(Node.TEXT, this.userPwd);
   header[0].addChild(Node.ELEMENT, pass);
   envelope.headerOut = header;
  }

  // 设置.net webservice
  envelope.dotNet = true;
  // 发送请求
  envelope.setOutputSoapObject(soapObeject);

  // 设置请求参数
  if (params != null && !params.isEmpty()) {
   for (Iterator it = params.entrySet().iterator(); it.hasNext();) {
    Map.Entry e = (Entry) it.next();
    soapObeject.addProperty(e.getKey().toString(), e.getValue());
   }
  }
  HttpTransportSE transport = new HttpTransportSE(url);
  SoapObject result = null;

  try {
   // webservice 请求
   transport.call(soapAction, envelope);

   // 得到返回结果
   result = (SoapObject) envelope.bodyIn;

  } catch (Exception e) {
   e.printStackTrace();
   Log.e("LifePayment", "CallWebService:" + e.getMessage());
  }
  finally
  {
   transport=null;
   envelope=null;
   soapObeject=null;
  }
  return result;
 }

}
 

标签:

猜你喜欢

发表评论

必填

选填

选填

必填,不填不让过哦,嘻嘻。

记住我,下次回复时不用重新输入个人信息

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。