Android调用.net Webservice例子

samool 发布暂无评论14071 阅读

AI摘要

本文提供了一个Android平台调用.net Webservice的示例,通过ksoap2库实现手机号码归属地查询功能。示例中包含了Android端的界面设计和事件处理,以及WebService的调用过程,包括创建SoapObject、设置参数、生成SOAP请求信息、调用WebService和处理返回结果等步骤。

package com.liufeng.ws.activity;

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.view.View;

import android.view.View.OnClickListener;

import android.widget.Button;

import android.widget.EditText;

import android.widget.TextView;

 

/** * Android平台调用WebService(手机号码归属地查询) *  * @author liufeng * @date 2011-05-18 */

 

public class MainActivity extends Activity {

private EditText phoneSecEditText;

private TextView resultView;

private Button queryButton;

 

@Overridepublic void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

 

phoneSecEditText = (EditText) findViewById(R.id.phone_sec);

resultView = (TextView) findViewById(R.id.result_text);

queryButton = (Button) findViewById(R.id.query_btn);

 

queryButton.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {

// 手机号码(段)

String phoneSec = phoneSecEditText.getText().toString().trim();

 

// 简单判断用户输入的手机号码(段)是否合法

if ("".equals(phoneSec) || phoneSec.length() < 7) {

phoneSecEditText.setError("您输入的手机号码(段)有误!");

phoneSecEditText.requestFocus();

resultView.setText("");

return;

}

// 查询手机号码(段)信息getRemoteInfo(phoneSec);}});}

/** * 手机号段归属地查询 *  * @param phoneSec 手机号段 */

 

public void  getRemoteInfo(String phoneSec) {

//命名空间

String nameSpace = "http://WebXml.com.cn/";

//调用的方法名称

String methodName = "getMobileCodeInfo";

// EndPointString

endPoint = "http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx";

// SOAP 

ActionString soapAction = "http://WebXml.com.cn/getMobileCodeInfo";

// 指定WebService的命名空间和调用的方法名

SoapObject rpc = new SoapObject(nameSpace, methodName);

// 设置需调用WebService接口需要传入的两个参数mobileCode、userId

rpc.addProperty("mobileCode", phoneSec);

rpc.addProperty("userId", "");

// 生成调用WebService方法的SOAP请求信息,并指定SOAP的版本

SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);

envelope.bodyOut = rpc;

// 设置是否调用的是dotNet开发的WebService

envelope.dotNet = true;

// 等价于

envelope.bodyOut = rpc;envelope.setOutputSoapObject(rpc);

HttpTransportSE transport = new HttpTransportSE(endPoint);

try {

// 调用WebService

transport.call(soapAction, envelope);

} 

catch (Exception e) 

{

e.printStackTrace();

}

 

// 获取返回的数据

SoapObject object = (SoapObject) envelope.bodyIn;

// 获取返回的结果

String result = object.getProperty("getMobileCodeInfoResult").toString();

// 将WebService返回的结果显示在TextView中

resultView.setText(result);

}

}  


  1. 01android平台获取手机IMSI、IMEI 、序列号和手机号的方法2016-01-07
  2. 02Android TextView添加滚动条2014-05-09
  3. 03Android TextView换行问题2014-03-25
  4. 04android 删除文件2013-07-26
  5. 05android bitmap的放大缩小2013-07-26
  6. 06Android MediaPlayer使用方法2013-02-16
  7. 07Android软件图标的几种大小2013-01-07
  8. 08谷歌发布Android 4.2系统2012-10-30
  9. 09Android 4.1.2源码今天将发布2012-10-10
  10. 10Android打开系统设置界面 2012-04-12
COMMENTS / 一纸留言

读者留言 0 条

一纸留言,便是山水间的一次相逢。

还没有留言,欢迎写下第一段交流。

WRITE A COMMENT

写下你的留言