android Web Service

本文介绍如何使用ksoap2-android库调用WebService查询手机号码的归属地信息,并提供了具体的代码实现步骤,包括设置WebService的命名空间、URL、方法名、传输对象等。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

                

                               Web Service

Web Service简介

   Web Service用于消除不同平台、不同语言之间的实现差异,将现有的应用程序发布成开放式服务,从而允许互联网上的任何地方、任何平台、任何语言的应用程序来访问该服务。

 

 

www.webxml.com.cn

 

通过ksoap2-android-assembly-2.5.2-jar-with-dependencies.jar

一、

1、定义Web Service的命名空间

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

2、定义Web Service提供服务的URL

String  SERVICE_URL = "http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx";

3、设置调用的方法

         String methodName = "getMobileCodeInfo";

4、创建HttpTransportSE传输对象

HttpTransportSE ht = new HttpTransportSE(SERVICE_URL);

                   ht.debug = true;

5、使用SOAP1.1协议创建Envelop对象

         SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(

                                     SoapEnvelope.VER11);

6、实例化SoapObject对象

                   SoapObject  soapObject = new SoapObject(SERVICE_NS, methodName);

7、添加一个请求参数

         soapObject.addProperty("mobileCode",mobile.getText().toString());

    envelope.bodyOut = soapObject;

8、设置与.Net提供的Web Service保持较好的兼容性

                   envelope.dotNet = true;

9、调用Web Service

                            ht.call(SERVICE_NS + methodName, envelope);

10、获取服务器响应返回的SOAP消息                        

                                     if (envelope.getResponse() != null) {

                                               // 获取服务器响应返回的SOAP消息

                                               SoapObject result = (SoapObject) envelope.bodyIn;

                                               //解析服务器响应的SOAP消息。

                                               return result.getProperty(0).toString();

                                     }

11、解析服务器响应的SOAP消息

result.getProperty(0).toString();

 

例:

package com.example.haoma;

 

import java.io.IOException;

         private String getServiceMobile(){

                   // 定义Web Service的命名空间

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

                   // 定义Web Service提供服务的URL

                   String SERVICE_URL = "http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx";

                   // 调用的方法

                   String methodName = "getMobileCodeInfo";

                   // 创建HttpTransportSE传输对象

                   HttpTransportSE ht = new HttpTransportSE(SERVICE_URL);

                   ht.debug = true;

                  

                   // 使用SOAP1.1协议创建Envelop对象

                   SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(

                                      SoapEnvelope.VER11);

                   // 实例化SoapObject对象

                   SoapObject soapObject = new SoapObject(SERVICE_NS, methodName);

                   // 添加一个请求参数

                   soapObject.addProperty("mobileCode",mobile.getText().toString());

                   //soapObject.addProperty("userID","");

                   envelope.bodyOut = soapObject;

                   // 设置与.Net提供的Web Service保持较好的兼容性

                   envelope.dotNet = true;

                   try {

                            // 调用Web Service

                                      ht.call(SERVICE_NS + methodName, envelope);

                                      if (envelope.getResponse() != null) {

                                               // 获取服务器响应返回的SOAP消息

                                               SoapObject result = (SoapObject) envelope.bodyIn;

                                      //      SoapObject detail = (SoapObject) result.getProperty(methodName      + "Result");

                                               // 解析服务器响应的SOAP消息。

                                               return result.getProperty(0).toString();

                                      }

                            } catch (IOException e) {

                                      // TODO Auto-generated catch block

                                      e.printStackTrace();

                                      Toast.makeText(getApplicationContext(), "shucuo", 1).show();

                            }catch (XmlPullParserException e) {

                                      // TODO: handle exception

                                      e.printStackTrace();

                            }

                   return null;

          }

 

 


自己实现XML的解析等操作

1、              拷贝Web Service中的SOAP

 

package com.example.webserviceweather;

 

 

import java.io.ByteArrayOutputStream;

import java.io.InputStream;

import java.net.HttpURLConnection;

import java.net.URL;

 

import org.xmlpull.v1.XmlPullParser;

 

import android.util.Xml;

 

 

 

public class AdressService {   //获取手机号归属地

    //返回手机号对应的归属地  

         public static String getAddress(String mobile)throws Exception{  

                   String soap = readSoap();

                   //替换掉mobile

                   soap = soap.replaceAll("\\$mobile", mobile);

                   //得到字节数据

                   byte[] entity = soap.getBytes();

                   // 确定要请求的Web Service API所在的路径

                   String path ="http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx";

             //包装成一个URL对象

                   URL url = new URL(path);

                   //打开一个HTTP协议的

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

                   conn.setConnectTimeout(5000);

                   conn.setRequestMethod("POST");

                   conn.setDoOutput(true);

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

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

                   conn.getOutputStream().write(entity);

                  

                   if(conn.getResponseCode() == 200){

                            return parseSOAP(conn.getInputStream());

                   }

                   return null;

         }

 

 

 

                  

                  

         private static String parseSOAP(InputStream xml) throws Exception{

                   XmlPullParser pullParser = Xml.newPullParser();

                   pullParser.setInput(xml,"UTF-8");

                   int event = pullParser.getEventType();

                   while(event != XmlPullParser.END_DOCUMENT){

                            switch(event){

                            case XmlPullParser.START_TAG:

                                     if("getMobileCodeInfoResult".equals(pullParser.getName())){

                                               return pullParser.nextText();

                                     }

                                     break;

                            }

                            event = pullParser.next();

                   }

                   return null;

                  

         }

 

 

         //读取SOAP协议的内容

         private static String readSoap() throws Exception {

                   InputStream inStream = AdressService.class.getClassLoader().getResourceAsStream("soap12.xml");

                   ByteArrayOutputStream outputStream = new ByteArrayOutputStream();

                   byte[] buffer = new byte[1024];

                   int len = 0;

                   while( (len = inStream.read(buffer)) != -1){

                            outputStream.write(buffer, 0, len);

                   }

                   inStream.close();

                   byte[] data = outputStream.toByteArray();

                   return new String(data);

         }

}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值