android手机归属

来源:互联网 发布:linux 发送广播包命令 编辑:程序博客网 时间:2024/05/10 00:32

   最近做了一款在线查询手机号码归属地的应用,把主要代码记录下来

//运用progressDialog加载

 progressDialog = new AlertDialog.Builder(this)
        .setTitle("读取数据中")
        .setMessage("正在加载数据,请稍等!")
        .create();

//handler线程
        tHandler = new Handler(){
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
                final String num = input_txt.getText().toString();
                Matcher m = p_num.matcher(num);
                if (m.matches() && netinfo.isAvailable()){
                    String urlString="http://coosola.com/api/mobile/?q="+input_txt.getText();
                    getResult(urlString);
                    progressDialog.hide();
                }
                else {
                    Toast.makeText(PhoneSource.this, "没有网络", Toast.LENGTH_LONG).show();
                }
            }
        };
       
       
        submit.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                progressDialog.show();
                timer.schedule(new TimerTask(){

                    @Override
                    public void run() {
                        Message message = new Message();
                        message.setTarget(tHandler);
                        message.sendToTarget();
                       
                    }
                   
                },100);
               
            }
        });
    }
    private void  getResult(String urlString){
        URL url;
          try {
            url = new URL(urlString);
            URLConnection connection = url.openConnection();
            HttpURLConnection httpConnection = (HttpURLConnection)connection;
            int responseCode = httpConnection.getResponseCode();
            if (responseCode == HttpURLConnection.HTTP_OK) {
                InputStream in = httpConnection.getInputStream();
                DocumentBuilderFactory dbfactory = DocumentBuilderFactory.newInstance();
                DocumentBuilder db = dbfactory.newDocumentBuilder();
                Document dom = db.parse(in);     
                Element docEle = dom.getDocumentElement();
                NodeList nl = docEle.getElementsByTagName("result");
                if (nl != null && nl.getLength() > 0) {
                    Element root = (Element)nl.item(0);
                    String show= "查询结果如下:/n手机号码:" + getTagValue(root,"tel") + "/n归属地: "
                    + getTagValue(root,"province")+getTagValue(root,"city")+ "/n运营商: "
                    +getTagValue(root,"provider")+"/n区号:"+getTagValue(root, "area_code")
                    +"/n邮编:"+getTagValue(root, "postcode")
                    + "/n手机卡类型: "+getTagValue(root,"type");
                    //result_txt.setText(show);
                     new AlertDialog.Builder(PhoneSource.this)
                    .setTitle("手机号码归属地查询")
                    .setMessage(show)
                    .setPositiveButton("确定", new DialogInterface.OnClickListener(){

                        public void onClick(DialogInterface dialog, int which) {
                           
                        }
                       
                    }).show();
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        } 
    }
   
    private String getTagValue(Element parent,String tags){
        return parent.getElementsByTagName(tags).item(0).getFirstChild().getNodeValue();
    }

原创粉丝点击