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

使用Android应用调用Web Service

需要工具:

ksoap2-android

下载地址:

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=""xmlns: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" ><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="输入一个手机号" /><EditTextandroid:id="@+id/phone_num"android:layout_width="fill_parent"android:layout_height="wrap_content"android:inputType="textPhonetic"android:singleLine="true" /><Buttonandroid:id="@+id/query"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="查询" /><TextViewandroid: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;@Overrideprotected 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() {@Overridepublic void handleMessage(Message msg) {if (msg.what == 0x123) {show.setText(result);}}};query.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {final String phoneNum = PhoneNum.getText().toString().trim();new Thread() {public void run() {// 命名空间String namespace = "";// 方法名称String methodName = "getMobileCodeInfo";// ServiceURLString ServiceURL = "";// soap ActionString soapAction = "";// 指定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开发的WebServiceenvelope.dotNet = true;envelope.setOutputSoapObject(rpc);HttpTransportSE transportSE = new HttpTransportSE(ServiceURL);try {transportSE.call(soapAction, envelope);} catch (Exception e) {// TODO Auto-generated catch blocke.printStackTrace();}SoapObject object = (SoapObject) envelope.bodyIn;// 这个方法得到的是一个键值对,// result = object.toString();result = object.getProperty(0).toString();handler.sendEmptyMessage(0x123);};}.start();}});}}

生活不是等待风暴过去,而是学会在雨中翩翩起舞。

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

相关文章:

你感兴趣的文章:

标签云: