码迷,mamicode.com
首页 > 移动开发 > 详细

Android---56---Android应用调用Web Service/号码归属地查询

时间:2015-06-14 18:37:35      阅读:173      评论:0      收藏:0      [点我收藏+]

标签:android   web service   

使用Android应用调用Web Service

需要工具:

ksoap2-android

 

下载地址:http://pan.baidu.com/s/1jGL6b10


build path将ksoap2-android 添加到项目工程中

先将ksoap2-android 包导入 libs目录下,右键build path -> add to build path
点击项目工程名,右键 build path -> configure build path 在ksoap2-android这个包前面打上对号,点击OK

完成

 

 

使用Android应用调用Web Service 步骤:

 

1.创建HttpTransportSE对象,该对象用于调用Web Service操作

通过构造方法可以指定WebService的WSDL文档的URL

HttpTransportSE transportSE = new HttpTransportSE(ServiceURL);


 

 


2.创建SoapSerializationEnvelope对象,生成调用Web Service方法的SOAP请求信息

SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER10);


 

 

3.创建SoapObject对象,该对象创建时需要传入所要调用的Web Service的命名空间、Web Service方法名

SoapObject rpc = new SoapObject(namespace, methodName);


 

 


4.如果有参数需要传递给Web Service服务器端,需要调用SoapObject对象的addProperty(String name ,Object value)方法来设置参数

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


 

 

5.调用SoapSerializationEnvelope的setOutputSoapObject()方法,或者直接对bodyOut属性赋值,将前两步创建的
SoapObject对象设为SoapSerializationEnvelope的传出SOAP消息体

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


 

 

6.调用对象的call方法,并以SoapSerializationEnvelope作为参数调用远程Web Service

transportSE.call(soapAction, envelope);


 

 

7.调用完成后,访问SoapSerializationEnvelope对象的bodyIn属性,该属性返回一个SoapObject对象,该对象就代表了Web Service的返回消息
解析该SoapObject对象,即可获取调用web Service 的返回值

SoapObject object = (SoapObject) envelope.bodyIn;


 

 

号码归属地查询:

 

布局文件:

 

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="输入一个手机号" />

    <EditText
        android:id="@+id/phone_num"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:inputType="textPhonetic"
        android:singleLine="true" />

    <Button
        android:id="@+id/query"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="查询" />

    <TextView
        android:id="@+id/show"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" />

</LinearLayout>


 

 

 

 

Activity:

 

public class MainActivity extends Activity {

	private EditText PhoneNum;
	private Button query;
	private TextView show;
	private String result;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		PhoneNum = (EditText) findViewById(R.id.phone_num);
		query = (Button) findViewById(R.id.query);
		show = (TextView) findViewById(R.id.show);

		final Handler handler = new Handler() {
			@Override
			public void handleMessage(Message msg) {
				if (msg.what == 0x123) {
					show.setText(result);
				}
			}
		};

		query.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				final String phoneNum = PhoneNum.getText().toString().trim();
				new Thread() {
					public void run() {
						// 命名空间
						String namespace = "http://WebXml.com.cn/";
						// 方法名称
						String methodName = "getMobileCodeInfo";
						// ServiceURL
						String ServiceURL = "http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx";
						// soap Action
						String soapAction = "http://WebXml.com.cn/getMobileCodeInfo";
						// 指定WebService 的命名空间和调用方法
						SoapObject rpc = new SoapObject(namespace, methodName);
						// 需要传入参数
						rpc.addProperty("mobileCode", phoneNum);
						rpc.addProperty("userId", "");
						// 生成调用WebService方法的SOAP请求信息,并指定SOAP的版本
						SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
								SoapEnvelope.VER10);
						// envelope.bodyOut = rpc;
						// 设置是否调用的是dotNet开发的WebService
						envelope.dotNet = true;
						envelope.setOutputSoapObject(rpc);

						HttpTransportSE transportSE = new HttpTransportSE(
								ServiceURL);

						try {
							transportSE.call(soapAction, envelope);
						} catch (Exception e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
						SoapObject object = (SoapObject) envelope.bodyIn;
						// 这个方法得到的是一个键值对,
						// result = object.toString();
						result = object.getProperty(0).toString();
						handler.sendEmptyMessage(0x123);
					};
				}.start();
			}
		});
	}
}


 

 

技术分享

Android---56---Android应用调用Web Service/号码归属地查询

标签:android   web service   

原文地址:http://blog.csdn.net/u013476556/article/details/46492345

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!