Project——手机号码归属地查询

本文介绍了一个使用SOAP协议的Web服务来查询中国移动电话号码的归属地信息。该服务通过发送特定格式的XML请求到指定的Web服务接口,并接收响应XML来获取手机号码的详细地址。文中提供了一个完整的Android应用程序示例,包括发起HTTP POST请求、处理响应数据及UI交互。

1、

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

2、

package cn.itcast.service;

import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;

import org.xmlpull.v1.XmlPullParser;

import android.util.Xml;

import cn.itcast.utils.StreamTool;

public class MobileService {
 
 public static String getMobileAddress(String mobile) throws Exception{
  InputStream inStream = MobileService.class.getClassLoader().getResourceAsStream("mobilesoap.xml");
  byte[] data = StreamTool.readInputStream(inStream);  
  String xml = new String(data);
  String soap = xml.replaceAll("\\$mobile", mobile);
  
  String path = "http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx";  
  data = soap.getBytes();//得到了xml的实体数据
  URL url = new URL(path);
  HttpURLConnection conn = (HttpURLConnection)url.openConnection();
  conn.setConnectTimeout(5 *1000);
  conn.setRequestMethod("POST");
  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){
   InputStream responseStream = conn.getInputStream();  
   return parseXML(responseStream);
  }
  return null;
 }
 /**
  * 解析返回xml数据
  * @param responseStream
  * @return
  * @throws Exception
  */
 private static String parseXML(InputStream responseStream) throws Exception {
  XmlPullParser parser = Xml.newPullParser();
  parser.setInput(responseStream, "UTF-8");
  int event = parser.getEventType();
  while(event!=XmlPullParser.END_DOCUMENT){
   switch (event) {
   case XmlPullParser.START_TAG:
    if("getMobileCodeInfoResult".equals(parser.getName())){
     return parser.nextText();
    }
    break;
   }
   event = parser.next();
  }
  return null;
 }
}

3、

package cn.itcast.utils;

import java.io.ByteArrayOutputStream;
import java.io.InputStream;

public class StreamTool {

 /**
  * 从输入流读取数据
  * @param inStream
  * @return
  * @throws Exception
  */
 public static byte[] readInputStream(InputStream inStream) throws Exception{
  ByteArrayOutputStream outSteam = new ByteArrayOutputStream();
  byte[] buffer = new byte[1024];
  int len = 0;
  while( (len = inStream.read(buffer)) !=-1 ){
   outSteam.write(buffer, 0, len);
  }
  outSteam.close();
  inStream.close();
  return outSteam.toByteArray();
 }
}

4、

package cn.itcast.mobile;

import cn.itcast.service.MobileService;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
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 static final String TAG = "MainActivity";
 private EditText mobileText;
    private TextView resultView;
   
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
       
        mobileText = (EditText) this.findViewById(R.id.mobile);
        resultView = (TextView) this.findViewById(R.id.result);
        Button button = (Button) this.findViewById(R.id.button);
        button.setOnClickListener(new View.OnClickListener() {   
   @Override
   public void onClick(View v) {
    String mobile = mobileText.getText().toString();
    try {
     String address = MobileService.getMobileAddress(mobile);
     resultView.setText(address);
    } catch (Exception e) {
     Log.e(TAG, e.toString());
     Toast.makeText(MainActivity.this, R.string.error, 1).show();
    }
   }
  });
    }
}

 

代码转载自:https://pan.quark.cn/s/133311188eb6 ### C# DllImport功能说明及路径选取问题分析 #### 一、DllImport核心原理 `DllImport`是.NET Framework内的一种技术,用于执行平台调用服务(Platform Invoke, 简称P/Invoke),该机制使得.NET应用程序能够调用非托管代码中的函数,例如Windows API或其他非托管库中的函数。这对于增强.NET应用程序的功能性非常关键,因为许多高级系统级操作(例如文件操作、进程控制等)通常由非托管库负责实现。 `DllImport`特性包含在`System.Runtime.InteropServices`命名空间中,它的主要功能是向CLR(Common Language Runtime)指示如何定位并调用非托管库中的特定函数。 #### 二、DllImport特性包含的主要元素 `DllImport`特性所包含的主要元素有: - **DllName**:必需的字符串参数,用于表明需要导入的非托管库的名称。 - **CallingConvention**:可选参数,用于设定调用协议。在默认情况下,其值为`CallingConvention.Cdecl`。 - **CharSet**:可选参数,用于定义字符集的类型。在默认情况下,其值为`CharSet.Auto`,即根据函数的签名自动决定字符集。 - **EntryPoint**:可选参数,用于指定非托管库中的函数名称。若未提供,则默认使用应用程序的方法名称作为函数名称。 - **ExactSpelling**:可选布尔值,用于确定函数名称是否必须与非托管库中的完全一致。...
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值