•  

电话号码校验

2018-03-05

package com.ctl.utils;

import net.sf.json.JSONArray;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.ArrayList;
import java.util.List;

public class TelephoneCheck {
    public static Logger logger = LoggerFactory.getLogger(TelephoneCheck.class);
    public static String checkTel(String tels[]){
        List<String> list=new ArrayList<>();
        int length=tels.length;
        try{
            for(int i=0;i<length;i++){
                String returnStr= HttpClientUtil.httpGetRequest("http://tcc.taobao.com/cc/json/mobile_tel_segment.htm?tel="+tels[i]);
                returnStr=returnStr.replaceAll("\\s+","");
                returnStr=returnStr.replaceAll("'","");
                returnStr=returnStr.replaceAll("\\{","{\"");
                returnStr=returnStr.replaceAll("}","\"}");
                returnStr=returnStr.replaceAll(":","\":\"");
                returnStr=returnStr.replaceAll(",","\",\"");
                int index=returnStr.indexOf("{");
                String data=returnStr.substring(index);
                logger.debug(data);
                list.add(data);
            }
        }catch (Exception e){
            logger.error("",e);
        }
        return JSONArray.fromObject(list.toArray()).toString();
    }

    public static String checkTel(String tel){
        List<String> list=new ArrayList<>();
        String tels[]=tel.split(",");
        int length=tels.length;
        try{
            for(int i=0;i<length;i++){
                String returnStr= HttpClientUtil.httpGetRequest("http://tcc.taobao.com/cc/json/mobile_tel_segment.htm?tel="+tels[i]);
                returnStr=returnStr.replaceAll("\\s+","");
                returnStr=returnStr.replaceAll("'","");
                returnStr=returnStr.replaceAll("\\{","{\"");
                returnStr=returnStr.replaceAll("}","\"}");
                returnStr=returnStr.replaceAll(":","\":\"");
                returnStr=returnStr.replaceAll(",","\",\"");
                int index=returnStr.indexOf("{");
                String data=returnStr.substring(index);
                logger.debug(data);
                list.add(data);
            }
        }catch (Exception e){
            logger.error("",e);
        }
        return JSONArray.fromObject(list.toArray()).toString();
    }
    public static void main(String args[]){
        String returnStr= HttpClientUtil.httpGetRequest("http://tcc.taobao.com/cc/json/mobile_tel_segment.htm?tel="+"10515287138");
        returnStr=returnStr.replaceAll("\\s+","");
        returnStr=returnStr.replaceAll("'","");
        returnStr=returnStr.replaceAll("\\{","{\"");
        returnStr=returnStr.replaceAll("}","\"}");
        returnStr=returnStr.replaceAll(":","\":\"");
        returnStr=returnStr.replaceAll(",","\",\"");
        int index=returnStr.indexOf("{");
//        int index2=returnStr.indexOf("}");
//        logger.info(returnStr.substring(index1,index2)+"");
        logger.info(returnStr.substring(index));
        String result=null;
        result=checkTel(new String[]{"18515287138","13161167381"});
        result=checkTel("18515287138,13161167381");
        logger.info("result:"+result);
    }
}
申明:本文受法律保护,未经作者同意不得用于商业用途,如转载请说明出处!
评论