Android开发中Web Service网络编程研究

来源:互联网 发布:单片机浇花 编辑:程序博客网 时间:2024/06/05 19:23
 Web Service是一种面向服务架构(Service-oriented architecture,SOA)的技术,目的是实现不同平台的应用服务之间的相互调用。Android作为一个市场占有率第一的移动操作系统,其网络功能是最重要的特性之一。在Android开发中通过Web Service可以方便地实现不同平台之间的方法调用,从网上获取数据信息和实现功能扩展。Web Service通过标准的Web协议提供服务。
  通过Web Service实现远程方法调用,获取数据信息,最关键的问题是数据访问和传输的协议规范。
  SOAP协议(Simple Object Access Protocal,简单对象访问协议),它是一个分布式网络环境下用于信息交换的通讯协议。在此协议下,应用程序和软件组件可以通过标准的Web协议进行通讯。SOAP使用基于XML的可扩展消息格式,需同时绑定一个传输用协议。这个协议通常是HTTP或HTTPS,但也可以使用SMTP或XMPP。
  WSDL是一个XML格式文档,用以描述服务端口访问方式和使用协议的细节。通常用来辅助生成服务器和客户端代码及配置信息。
  UDDI是用来发布和搜索WEB服务的协议,应用程序可藉由此协议在设计或运行时找到目标WEB服务。
  Java开发中的Web Service有很多种实现方式,如XML-RPC、XFile、Axis等等,可是这些库并不适合资源有限的Android手机客户端。在Java ME版本中,广泛使用的是KSOAP。虽然Android并不使用Java ME,但是KSOAP也有Android下的可用版本ksoap2-Android。
  2.1 ksoap2-Android
  kSOAP是Enhydra.org的一个开源作品,是EnhydraME项目的一部分。ksoap2-Android是ksoap2在Android下的一个移植版本,利用它可以非常方便地访问Web Service。ksoap2的常用接口有:
  org.ksoap2. SoapObject
  org.ksoap2. SoapEnvelope
  org.ksoap2. SoapSerializationEnvelope
  org.ksoap2.transport. HttpTransport
  SoapObject用于创建SOAP对象,实现SOAP调用;
  SoapEnvelope实现了SOAP标准中的SOAP Envelope,封装了head对象和body对象。
  SoapSerializationEnvelope是ksoap2中对SoapEnvelope的扩展,支持SOAP序列化(Serialization)格式规范,可以对简单对象自动进行序列化(Simple object serialization)。
  HttpTransport用于进行Internet访问/请求,获取服务器SOAP。
  2.2 ksoap2-Android的编译配置
  图1
<TextView
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"  http://www.lekolite.cn/android/android_text.html

  android:id="@+id/EditTextPhoneNumber" android:layout_width="match_parent" android:layout_height="wrap_content" android:inputType="phone" >
  <requestFocus /></EditText><Button
  android:id="@+id/btnCheck"
  android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/btnCheck" />
  3.2查询的代码
  当输入手机号码,点击查询按钮时,访问Web Service,远程调用getMobileCodeInfo方法来获得查询结果。访问Web Service的步骤如下:
1)实例化SoapObject对象,指定webService的命名空间和调用的方法名称。
  String serviceNameSpace = "http://WebXml.com.cn/";
  String methodName = "getMobileCodeInfo";
  SoapObject request = new SoapObject(serviceNameSpace,methodName);
  2)设置调用方法的参数。根据服务提供方描述的参数要求,需要两个参数。第一个为mobileCode,字符串类型,表示手机号码,第二个为userID为商业用户ID,免费用户为空字符串。
  request.addProperty("mobileCode", strPhoneNumber);
  request.addProperty("userID", "");
  3)通过实例化Envelope对象,设置SOAP请求信息,并注册Envelope对象。对应于不同的SOAP规范,需要使用不同的常量作为参数。常量SoapEnvelope.VER10:对应于SOAP 1.0规范;常量SoapEnvelope.VER11:对应于SOAP 1.1规范;常量SoapEnvelope. VER12:对应于SOAP 1.2规范。
  SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
  envelope.bodyOut = request;
  (new MarshalBase64()).register(envelope);
  4)实例化传输对象,参数为传递SOAP数据的目标地址,即WSDL文档的URL。HttpTransportSE是一个强大的Http传输类,可以完成Http传输过程。它封装了网络请求的一切,可以完全不用考虑序列化消息。通过把它的debug属性设置为true来启用调试信息。
  String serviceURL = "http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx";
  HttpTransportSE transport = new HttpTransportSE(serviceURL);
  transport.debug = true;
    http://www.lekolite.cn/
  5)获取返回结果,并进行解析。
  返回数据为字符串(手机号码:省份城市手机卡类型)。
  if(envelope.getResponse()!=null){
  return (envelope.bodyIn.toString());
  }
  在Android开发中使用ksoap2-Android可以很方便地实现Web Service功能,通过Web Service远程调用来获取数据。但是在开发中发现,直接访问Web Service来获取数据,会由于网络的延时而造成界面响应的短暂停顿,应该研究更好的方法,如通过后台异步数据传输来解决。
安卓开发中调用WebService实例

某些情况下我们可能需要与Mysql或者Oracle数据库进行数据交互,有些朋友的第一反应就是直接在Android中加载驱动然后进行数据的增删改查。我个人不推荐这种做法,一是手机毕竟不是电脑,操作大量数据费时费电;二是流量贵如金那。我个人比较推荐的做法是使用Java或PHP等开发接口或者编写WebService进行数据库的增删该查,然后Android调用接口或者WebService进行数据的交互。本文就给大家讲解在Android中如何调用远程服务器端提供的WebService。

  既然是调用WebService,我们首先的搭建WebService服务器。为了便于操作,我们就使用网上免费的WebService进行学习。

  下面演示的就是如何通过该网站提供的手机号码归属地查询WebService服务查询号码归属地 

  首先,将请求消息保存在XML文件中,然后使用$替换请求参数,如下:

  mobilesoap.xml

  <?xml version="1.0" encoding="utf-8"?>

  <soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope">

  <soap12:Body>

  <getMobileCodeInfo xmlns="http://WebXml.com.cn/">

  <mobileCode>$mobile</mobileCode>

  <userID></userID>

  </getMobileCodeInfo>

  </soap12:Body>

  </soap12:Envelope>

  其次,设计MainActivity布局文件,

  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">

  <TextView

  android:layout_width="fill_parent"

  android:layout_height="wrap_content"

  android:text="手机号码" />

  <EditText

  android:id="@+id/mobileNum"

  android:layout_width="fill_parent"

  android:layout_height="wrap_content"

  android:text=""

  />

  <Button

  android:id="@+id/btnSearch"

  android:layout_width="wrap_content"

  android:layout_height="wrap_content"

  android:text="查询"

  />

  <TextView

  android:id="@+id/mobileAddress"

  android:layout_width="fill_parent"

  android:layout_height="wrap_content"

  />

  </LinearLayout>

  下面贴出MainActivity,

  在Android中调用WebService还是比较简单的:请求webservice,获取服务响应的数据,解析后并显示。

  package com.szy.webservice;

  import java.io.ByteArrayOutputStream;

  import java.io.InputStream;

  import java.io.OutputStream;

  import java.net.HttpURLConnection;

  import java.net.URL;

  import java.util.HashMap;

  import java.util.Map;

  import java.util.regex.Matcher;

  import java.util.regex.Pattern;

  import org.xmlpull.v1.XmlPullParser;

  import android.app.Activity;

  import android.os.Bundle;

  import android.util.Log;

  import android.util.Xml;

  import android.view.View;

  import android.widget.Button;

  import android.widget.EditText;

  import android.widget.TextView;

  import android.widget.Toast;

  public class MainActivity extends Activity

  {

  private EditText mobileNum;

  private TextView mobileAddress;

  private static final String TAG = "MainActivity";

  @Override

  public void onCreate(Bundle savedInstanceState)

  {

  super.onCreate(savedInstanceState);

  setContentView(R.layout.main);

  mobileNum = (EditText) this.findViewById(R.id.mobileNum);

  mobileAddress = (TextView) this.findViewById(R.id.mobileAddress);

  Button btnSearch = (Button) this.findViewById(R.id.btnSearch);

  btnSearch.setOnClickListener(new View.OnClickListener()

  {

  @Override

  public void onClick(View v)

  {

  // 获取电话号码

  String mobile = mobileNum.getText().toString();

  // 读取xml文件

  InputStream inStream = this.getClass().getClassLoader().getResourceAsStream("mobilesoap.xml");

  try

  {

  // 显示电话号码地理位置,该段代码不合理,仅供参考

  mobileAddress.setText(getMobileAddress(inStream, mobile));

  } catch (Exception e)

  {

  Log.e(TAG, e.toString());

  Toast.makeText(MainActivity.this, "查询失败", 1).show();

  }

  }

  });

  }

  /**

  * 获取电话号码地理位置

  *

  * @param inStream

  * @param mobile

  * @return

  * @throws Exception

  */

  private String getMobileAddress(InputStream inStream, String mobile) throws Exception

  {

  // 替换xml文件中的电话号码

  String soap = readSoapFile(inStream, mobile);

  byte[] data = soap.getBytes();

  // 提交Post请求

  URL url = new URL("http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx");

  HttpURLConnection conn = (HttpURLConnection) url.openConnection();

  conn.setRequestMethod("POST");

  conn.setConnectTimeout(5 * 1000);

  conn.setDoOutput(true);

  conn.setRequestProperty("Content-Type", "application/soap+xml; charset=utf-8");

  conn.setRequestProperty("Content-Length", String.valueOf(data.length));

  OutputStream outStream = conn.getOutputStream();

  outStream.write(data);

  outStream.flush();

  outStream.close();

  if (conn.getResponseCode() == 200)

  {

  // 解析返回信息

  return parseResponseXML(conn.getInputStream());

  }

  return "Error";

  }

  private String readSoapFile(InputStream inStream, String mobile) throws Exception

  {

  // 从流中获取文件信息

  byte[] data = readInputStream(inStream);

  String soapxml = new String(data);

  // 占位符参数

  Map<String, String> params = new HashMap<String, String>();

  params.put("mobile", mobile);

  // 替换文件中占位符

  return replace(soapxml, params);

  }

  /**

  * 读取流信息

  *

  * @param inputStream

  * @return

  * @throws Exception

  */

  private byte[] readInputStream(InputStream inputStream) throws Exception

  {

  byte[] buffer = new byte[1024];

  int len = -1;

  ByteArrayOutputStream outSteam = new ByteArrayOutputStream();

  while ((len = inputStream.read(buffer)) != -1)

  {

  outSteam.write(buffer, 0, len);

  }

  outSteam.close();

  inputStream.close();

  return outSteam.toByteArray();

  }

  /**

  * 替换文件中占位符

  *

  * @param xml

  * @param params

  * @return

  * @throws Exception

  */

  private String replace(String xml, Map<String, String> params) throws Exception

  {

  String result = xml;

  if (params != null && !params.isEmpty())

  {

  for (Map.Entry<String, String> entry : params.entrySet())

  {

  String name = "\\$" + entry.getKey();

  Pattern pattern = Pattern.compile(name);

  Matcher matcher = pattern.matcher(result);

  if (matcher.find())

  {

  result = matcher.replaceAll(entry.getValue());

  }

  }

  }

  return result;

  }

  /**

  * 解析XML文件

  * @param inStream

  * @return

  * @throws Exception

  */

  private static String parseResponseXML(InputStream inStream) throws Exception

  {

  XmlPullParser parser = Xml.newPullParser();

  parser.setInput(inStream, "UTF-8");

  int eventType = parser.getEventType();// 产生第一个事件

  while (eventType != XmlPullParser.END_DOCUMENT)

  {

  // 只要不是文档结束事件

  switch (eventType)

  {

  case XmlPullParser.START_TAG:

  String name = parser.getName();// 获取解析器当前指向的元素的名称

  if ("getMobileCodeInfoResult".equals(name))

  {

  return parser.nextText();

  }

  break;

  }

  eventType = parser.next();

  }

  return null;

  }

  }

  最后注意,由于需要访问网络,需要加上权限

 <uses-permission android:name="android.permission.INTERNET"/>

  通过上面简单的例子,相信大家已经学习了如何在Android中调用WebService


0 0
原创粉丝点击