Android基础知识Android知识点滴Android开发知识

Android调用WebService

2016-04-06  本文已影响371人  哇楼主

 Web Services(Web服务)是一个用于支持网络间不同机器互操作的软件系统,它是一种自包含、自描述和模块化的应用程序,它可以在网络中被描述、发布和调用,可以将它看作是基于网络的、分布式的模块化组件。

Web Services是建立在通用协议的基础之上,如HTTP、SOAP、UDDI、WSDL等,这些协议在操作系统、编程语言和对象模型的选择上没有任何倾向,因此有着很强的生命力。

Web Services的优势在于提供了不同应用程序平台之间的互操作,它使得基于组件的开发和Web相结合的效果达到最佳。它是基于HTTP协议的,调用请求和回应消息都可以穿过防火墙,不需要更改防火墙的设置,这样就避免了使用特殊端口进行通信时无法穿越防火墙的问题。

SOAP(Simple Object Access Protocol,简单对象访问协议)是一种轻量级的、简单的、基于XML的协议,被设计用于在分布式环境中交换格式化和固化信息的简单协议。也就是说,要进行通信,进行数据访问传输,就必须依赖于一定的协议,而SOAP正是WebService通信中所依赖的一种协议。目前经常使用的SOAP协议有两个版本:SOAP 1.1 和 SOAP 1.2。

WSDL(Web Services Description Language,即Web服务描述语言)是一种用来描述Web服务的XML语言,它描述了Web服务的功能、接口、参数、返回值等,便于用户绑定和调用服务。它以一种和具体语言无关的方式定义了给定Web服务调用和应答的相关操作和消息。

案例:手机号码归属地查询

activity_main.xml

<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="例如:1875337"

/>

<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.example.telphone;

import org.ksoap2.SoapEnvelope;

import org.ksoap2.serialization.SoapObject;

import org.ksoap2.serialization.SoapSerializationEnvelope;

import org.ksoap2.transport.HttpTransportSE;

import android.support.v7.app.ActionBarActivity;

import android.support.v7.app.ActionBar;

import android.support.v4.app.Fragment;

import android.app.DownloadManager.Query;

import android.os.Bundle;

import android.os.Handler;

import android.os.Message;

import android.view.LayoutInflater;

import android.view.Menu;

import android.view.MenuItem;

import android.view.View;

import android.view.View.OnClickListener;

import android.view.ViewGroup;

import android.widget.Button;

import android.widget.EditText;

import android.widget.TextView;

import android.os.Build;

public class MainActivity extends ActionBarActivity {

private EditText phoneSecEditText;

private TextView resultView;

private Button queryButton;

String result;

Handler handler = new Handler(){

public void handleMessage(android.os.Message msg) {

if(msg.what==0x123){

resultView.setText(result);

}

}

};

@Override

protected 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) {

// 手机号码(段)

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

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

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

// 给出错误提示

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

phoneSecEditText.requestFocus();

// 将显示查询结果的TextView清空

resultView.setText("");

return;

}

// 查询手机号码(段)信息

new Thread(){

public void run() {

getRemoteInfo(phoneSec);

};

}.start();

}

});

}

/**

* 手机号段归属地查询

*

* @param phoneSec 手机号段

*/

public void getRemoteInfo(String phoneSec)  {

// 命名空间

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

// 调用的方法名称

String methodName = "getMobileCodeInfo";

// EndPoint

String endPoint = " http://ws.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.VER10);

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;

// 获取返回的结果

result = object.getProperty(0).toString();

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

//resultView.setText(result);

// Message message = new Message();

handler.sendEmptyMessage(0x123);

}

}

、、、

上一篇下一篇

猜你喜欢

热点阅读