android使用ksoap2调用webservice接口实现电话号码查询功能

来源:互联网 发布:wis水润面膜知乎 编辑:程序博客网 时间:2024/05/18 01:44

最近在研究android调用webservice接口的问题,在网上查找了很多资料,现在把自己的整理出来分享一下,感谢其他一些开源的朋友,提供了很多的帮助

首先来看一下活动类的布局文件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"
    android:paddingLeft="5dip"
    android:paddingRight="5dip"
    android:paddingTop="5dip" >

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="手机号码(段):" />

    <EditText
        android:id="@+id/editText1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:hint="这里显示查询的结果"
        android:inputType="textPhonetic"
        android:singleLine="true" />
    <EditText
        android:id="@+id/editText2"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:hint="在这里输入要查询的手机号码"
        android:inputType="textPhonetic"
        android:singleLine="true" />

    <Button
        android:id="@+id/button2"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:onClick="onClick"
        android:text="退出" />

    <Button
        android:id="@+id/button1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="right"
        android:onClick="onClick"
        android:text="查询" />
</LinearLayout>

在这里将调用webservice接口的方法写到一个独立的类里面,然后再主活动里面调用这个方法

package com.example.webservice;

import java.io.IOException;
import java.util.Map;

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

import android.content.Context;

/**
 * 这是一个用于请求webservice的工具类
 *
 * @author xxs
 *
 */
public class WebRequest {
 /**
  * @param namespace
  *            命名空间
  * @param methodname
  *            要调用的方法
  * @param url
  *            要访问的url
  * @param soapaction
  *            接口调用地址
  * @param paras
  *            要传入的参数
  * @return 返回的SoapObject对象
  */
 public static String GetRequest(String namespace, String methodname,
   String url, String soapaction, Map<String, String> paras) {
  String s = null;
  // 创建SoapObject对象,并指定WebService的命名空间和调用的方法名
  SoapObject rpc = new SoapObject(namespace, methodname);
  // 设置WebService方法的参数
  for (Map.Entry<String, String> entry : paras.entrySet()) {
   rpc.addProperty(entry.getKey(), entry.getValue());
  }
  // 创建SoapSerializationEnvelope对象,并指定WebService的版本
  SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
    SoapEnvelope.VER11);
  // 设置bodyout属性
  envelope.bodyOut = rpc;
  envelope.dotNet = true;
  envelope.setOutputSoapObject(rpc);
  // 创建HttpTransportSE对象,并指定WSDL文档的URL
  HttpTransportSE ht = new HttpTransportSE(url);
  try {// 调用WebService
   ht.call(soapaction, envelope);
   SoapObject object = (SoapObject) envelope.bodyIn;//取得返回值
   if (object != null) {
    s = object.getProperty(0).toString();//将返回的值转化为String
   }else{
    s = "没有查询到相关信息";
   }
  } catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  } catch (XmlPullParserException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
  return s;

 }
}

接下来就是在活动类调用上面工具类中GetRequest方法就是了

package com.example.webservice;

import java.util.HashMap;

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

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.EditText;
/**
 * 访问webservice
 * @author xxs
 *
 */
public class WebServiceActivity extends Activity {

 private EditText txt1, txt2;//定义输入框
 private String nameSpace = "http://WebXml.com.cn/";//命名空间
 private String methodName = "getMobileCodeInfo";//要使用的方法
 private String endPoint = "http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx";//要访问的wsdl的url地址
 private String soapAction = "http://WebXml.com.cn/getMobileCodeInfo";//接口地址
 private HashMap<String, String>paras = new HashMap<String, String>();//传入参数
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  // TODO Auto-generated method stub
  super.onCreate(savedInstanceState);
  setContentView(R.layout.main);//填充xml布局文件
  FindWidget();//调用查找控件的方法
 }
 /**
  * 此方法用于查找xml文件中的控件
  */
public void FindWidget(){
 txt1 = (EditText) findViewById(R.id.editText1);
 txt2 = (EditText) findViewById(R.id.editText2);
}
/**
 * Button触发事件监听
 * @param v
 */
 public void onClick(View v) {
  // TODO Auto-generated method stub
  switch (v.getId()) {
  case R.id.button1:
   //传入参数
   paras.put("mobileCode", txt2.getText().toString().trim());
   paras.put("userID", null);
   handler.sendEmptyMessage(1);
   break;
  case R.id.button2:
   finish();
   break;
  default:
   break;
  }
 }

 Handler handler = new Handler() {
  String result = null;

  @Override
  public void handleMessage(Message msg) {
   super.handleMessage(msg);
   switch (msg.what) {
   case 1:
    new Thread(new Runnable() {
     @Override
     public void run() {
      //调用请求webservice的方法
      result = WebRequest.GetRequest(nameSpace, methodName,endPoint,soapAction,  paras);
      handler.sendEmptyMessage(2);
     }
    }).start();

    break;
   case 2:
    txt1.setText(result);//将处理结果显
   default:
    break;
   }
  }

 };
}

运行截图: