Android开发之号码归属地查询

data.xml文件


   
   

   
   
  
    
    
    
     
     
      
      
      
       
       $mobile
      
      
      
      
      
    
     
     
  
    
    

   
   

 

布局文件:searchaddress.xml


   
   

   
   

    
    
    

        
     
     

        
     
     
    
    
    

    
    
    

        
     
     

        
     
     
    
    
    

    
    
    

        
     
     

        
     
     
    
    
    

    
    
    

        
     
     

        
     
     
    
    
    


   
   

 

核心代码:SearchAddressActivity.java


public class SearchAddressActivity extends Activity {

	private EditText et_number;
	private Button bt_search;
	private TextView tv_number;
	private TextView tv_belong_to;
	private TextView tv_city;
	private ProgressDialog pd;

	private Handler handler = new Handler() {

		@Override
		public void handleMessage(Message msg) {
			super.handleMessage(msg);
			pd.dismiss();
			//13912345678:江苏 常州 江苏移动全球通卡(注意冒号是中文的冒号)
			String result=(String)msg.obj;
			if(result.length()<15){
				Toast.makeText(getApplicationContext(), "您输入的手机号码有误", 1).show();
				return;
			}
			et_number.setText("");
			
			String number=result.substring(0, result.indexOf(":"));
			tv_number.setText(number);
			
			String belong_to=result.substring(result.lastIndexOf(" ")+1);
			tv_belong_to.setText(belong_to);
			
			String city=result.substring(result.indexOf(":")+1, result.lastIndexOf(" "));
			tv_city.setText(city);
		}

	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.searchaddress);

		et_number = (EditText) this.findViewById(R.id.et_number);
		bt_search = (Button) this.findViewById(R.id.bt_search);
		tv_number = (TextView) this.findViewById(R.id.tv_number);
		tv_belong_to = (TextView) this.findViewById(R.id.tv_belong_to);
		tv_city = (TextView) this.findViewById(R.id.tv_city);
		pd=new ProgressDialog(this);
		

	}

	public void click(View v) {
		// 查询归属地的操作是耗时操作,要使用多线程。。。
		String number = et_number.getText().toString().trim();
		if (TextUtils.isEmpty(number)) {
			Toast.makeText(getApplicationContext(), "号码不能为空", 1).show();
			return;
		}
		if(number.length()!=11){
			Toast.makeText(getApplicationContext(), "手机号码应为11位数字", 1).show();
			return;
		}
		
		new Thread(new MyRunnable()).start();
		pd.setTitle("查询中....");
		pd.show();
	}

	class MyRunnable implements Runnable {
		public void run() {
			try {
				URL url = new URL(
						"http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx");
				HttpURLConnection conn = (HttpURLConnection) url
						.openConnection();
				conn.setConnectTimeout(5000);
				conn.setRequestMethod("POST");
				InputStream in = getClass().getClassLoader()
						.getResourceAsStream("data.xml");
				byte[] data = StreamTool.getBytes(in);
				String str = new String(data);

				String content = str.replace("$mobile", et_number.getText()
						.toString().trim());
				
				conn.setDoOutput(true);
				conn.setRequestProperty("Content-Type",
						"application/soap+xml; charset=utf-8");
				conn.setRequestProperty("Content-Length", content.length() + "");
				conn.getOutputStream().write(content.getBytes());
				
				InputStream response=conn.getInputStream();
				String result=parseXml(response);
				Message msg=new Message();
				msg.obj=result;
				handler.sendMessage(msg);
			} catch (Exception e) {
				pd.dismiss();
				//在子线程中使用Toast
				Looper.prepare();
				Toast.makeText(getApplicationContext(), "网络异常!请检查您的网络设置!", 1).show();
				Looper.loop();
				e.printStackTrace();
			}
		}
	}
	
	private String parseXml(InputStream is) throws Exception{
		XmlPullParser parser=Xml.newPullParser();
		parser.setInput(is,"utf-8");
		int type=parser.getEventType();
		while(type!=XmlPullParser.END_DOCUMENT){
			switch (type) {
			case XmlPullParser.START_TAG:
				if("getMobileCodeInfoResult".equals(parser.getName())){
					return parser.nextText();
				}
				break;
			}
			type=parser.next();
		}
		return "查无此号";
	}

}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值