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

来源:互联网 发布:小说朗读软件 编辑:程序博客网 时间:2024/05/04 16:35

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();
    }
   }
  });
    }
}

 

原创粉丝点击