android中的webservice调用方式与java中差异较大,经过查找些资料和例子,自己这边儿就做了个总结,写了个例子,谈不上原创(惭愧ing...),不过这方面的例子确实都大体相似,因为大家能写成博文的,发布成例子的,调用的接口大抵是网上公开的接口,如天气啊,手机号码段啊之类的,公司内部的接口,肯定是不能外透的,但是调用的方式确实一样,所以记录下来,方便大家研究,或者自己今后用到。
老习惯,先上效果图:
图一
图二
再上项目结构图:
第一步,加入调用webservice的jar包,ksoap2-android-assembly-2.5.4-jar-with-dependencies.jar,下面会提供附件下载。
第二步,编辑activity_main.xml布局出效果图一所示效果,代码如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:paddingTop="5dip"
android:paddingLeft="5dip"
android:paddingRight="5dip"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="手机号码(段):"
/>
<EditText android:id="@+id/phone_sec"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:inputType="textPhonetic"
android:singleLine="true"
android:hint="例如:1398547"
/>
<Button android:id="@+id/query_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:text="查询"
/>
<TextView android:id="@+id/result_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal|center_vertical"
/>
</LinearLayout>
第三步,编辑MainActivity.java,代码如下:
package com.android.ws;
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;
public class MainActivity extends Activity {
private EditText phoneSecEditText;
private TextView resultView;
private Button queryButton;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_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() {
@Override
public void onClick(View v) {
// 手机号码(段)
String phoneSec = phoneSecEditText.getText().toString().trim();
// 简单判断用户输入的手机号码(段)是否合法
if ("".equals(phoneSec) || phoneSec.length() < 7) {
// 给出错误提示
phoneSecEditText.setError("您输入的手机号码(段)有误!");
phoneSecEditText.requestFocus();
// 将显示查询结果的TextView清空
resultView.setText("");
return;
}
// 查询手机号码(段)信息
getRemoteInfo(phoneSec);
}
});
}
/**
* 手机号段归属地查询
*
* @param phoneSec 手机号段
*/
public void getRemoteInfo(String phoneSec) {
// 命名空间
String nameSpace = "http://WebXml.com.cn/";
// 调用的方法名称
String methodName = "getMobileCodeInfo";
// EndPoint
String endPoint = "http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx";
// SOAP Action
String 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);
System.out.println("rpc:"+rpc);
System.out.println("enevlope:"+envelope);
try {
// 调用WebService
transport.call(soapAction, envelope);
} catch (Exception e) {
e.printStackTrace();
}
// 获取返回的数据
SoapObject object = (SoapObject) envelope.bodyIn;
// 获取返回的结果
String result = object.getProperty(0).toString();
// 将WebService返回的结果显示在TextView中
resultView.setText(result);
}
}
第四步,在Mainfest中提供网络权限
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.android.ws"
android:versionCode="1"
android:versionName="1.0" >
<!-- 添加网络权限 -->
<uses-permission android:name="android.permission.INTERNET" />
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="15" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/title_activity_main" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
最后,运行起来,就可看到上图的效果了,下面提供jar和项目的附件下载。
- 大小: 26.8 KB
- 大小: 27 KB
- 大小: 34.1 KB
分享到:
相关推荐
Android中调用Webservice实现号码归属地查询案例!
Android调用webservice查询手机归属地信息,可以直接运行,非常的简单好用
在android端使用了soap协议来调用网上的webservice接口,来获得手机号码的归属地信息
android调用.net的webservice的例子获取手机的归属地
两个实现了android调用webservice的代码示例,第一个用于查询天气预报所在的城市,第二个查询电话号码的归属地。
Android号码归属地查询源码,调用webservice方法来实现。
博客的源代码,博客地址为http://blog.csdn.net/pfe_nova/article/details/26147297,Android调用Webservice,实现了查某个具体地的天气预报,查手机的归属地
本案例旨在让读者了解如何调用Webservice的API进行号码归属地的查询,在网页http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx查得,getMobileCodeInfo其中需要的“号码”参数至少需要7位数字,在本...
public void onClick(View v) { String mobile = mobileText.getText().toString(); InputStream inStream = this.getClass().... Toast.makeText(MainActivity.this, "查询失败", 1).show(); } }
本程序代码是利用HTTP协议发送XML数据并调用webservice(soap),来实现手机号码的查询,是一个简单的应用程序,适合Android开发程序员。
手机归属地查询.调用webservice服务,进行手机归属地查询。
AndroidInteractWithWebService 通过网络WebService查询手机号码归属地的小示例 ServerForGETMethod 一些获取数据方法的演示GET方法 ServerForPOSTMethod 一些获取数据方法的演示POST方法 ...
主要介绍了在Android中调用WebService实例,有需要的朋友可以了解一下。
android端上传下载文件(需要下载服务器源代码),调用webservice查询手机号码归属地
第五天 1> 网络--通过HTTP协议发送XML数据,并调用webservice实现手机号归属地查询 2> 网络--通过HTTP协议实现多线程断点续传下载 3> 为应用添加新的Activity与参数传递 4> 意图 第六天 1> Activity的生命周期 ...
第五天 1> 网络--通过HTTP协议发送XML数据,并调用webservice实现手机号归属地查询 2> 网络--通过HTTP协议实现多线程断点续传下载 3> 为应用添加新的Activity与参数传递 4> 意图 第六天 1> Activity的生命周期 2> ...
Android访问webService,这是本人做的一个手机号码归属地查询.详情请见demo