Ajax 查詢手機號碼歸屬地

最近做項目的時候、要用到這個功能!

在網上找了很多、如果根據網絡提供的API直接JS Ajax查詢會出問題:拒絕訪問

網上說是跨域了、解決辦法就是java後臺訪問這個API地址。下面羅列一些網絡上的API地址。

淘寶網

API地址: http://tcc.taobao.com/cc/json/mobile_tel_segment.htm?tel=15850781443
參數:

  • tel:手機號碼
  • 返回:JSON

拍拍

API地址: http://virtual.paipai.com/extinfo/GetMobileProductInfo?mobile=15850781443&amount=10000&callname=getPhoneNumInfoExtCallback
參數:

  • mobile:手機號碼
  • callname:回調函數
  • amount:未知(必須)
  • 返回:JSON

財付通

API地址: http://life.tenpay.com/cgi-bin/mobile/MobileQueryAttribution.cgi?chgmobile=15850781443
參數:

  • chgmobile:手機號碼
  • 返回:xml

百付寶

API地址: https://www.baifubao.com/callback?cmd=1059&callback=phone&phone=15850781443
參數:

  • phone:手機號碼
  • callback:回調函數
  • cmd:未知(必須)
  • 返回:JSON

115

API地址: http://cz.115.com/?ct=index&ac=get_mobile_local&callback=jsonp1333962541001&mobile=15850781443
參數:

  • mobile:手機號碼
  • callback:回調函數
  • 返回:JSON

有道

API地址: http://www.youdao.com/smartresult-xml/search.s?jsFlag=true&type=mobile&q=13985046628
參數:

  • type:mobile(表示查詢手機號碼)
  • q:手機號碼
  • 返回:JSON

我用的是有道的API、下面是代碼:

頁面代碼:

  1. function findPhoneAddres(){   
  2.                    var mobile = $("#usermobil").val();     
  3.                    var urlAction = "<%=path %>/customermanage/listcustomerinfo!findPhoneAddres.action";   
  4.                    $.get(urlAction, {phoneStr:mobile}, function (data){   
  5.                         if(data==''||data==null){   
  6.                             alertMsg.info("找不到您輸入的手機號碼歸屬地!");       
  7.                         }else{   
  8.                             var json = eval("("+data+")");       
  9.                             var phoneStr = json.location ;   
  10.                             $("#userAddres").val(phoneStr.split(" ")[1]);    
  11.                             $("#userAddresByPhone").val(phoneStr.split(" ")[1]);       
  12.                             $("#userAddresLabel").html("手機號歸屬地:"+phoneStr.split(" ")[1])   
  13.                         }     
  14.                     });        

後臺Action方法:

  1.  /*  
  2.  * 手機號碼歸屬地查詢地址  
  3.  */   
  4. private final String urlAddres = "http://www.youdao.com/smartresult-xml/search.s?" +   
  5.                                  "jsFlag=true&type=mobile&q=";   
  6.   /**  
  7.  * 查詢手機號碼歸屬地  
  8.  * @return  
  9.  * @throws Exception  
  10.  */   
  11. public String findPhoneAddres() throws Exception{   
  12.     String phone = request.getParameter("phoneStr");   
  13.     String url = urlAddres+phone;       
  14.     String result = ActionURL.callUrlByGet(url, "GBK");     
  15.     request.setCharacterEncoding("UTF-8");   
  16.     response.setContentType("text/html;charset=UTF-8");   
  17.     PrintWriter out = response.getWriter();     
  18.     out.print(result);     
  19.     out.close();        
  20.     return null;   
  21. }   

ActionURL靜態類的callUrlByGet方法:

  1. public static String callUrlByGet(String callurl,String charset){     
  2.         String result = "";     
  3.         try {     
  4.             URL url = new URL(callurl);     
  5.             URLConnection connection = url.openConnection();     
  6.             connection.connect();     
  7.             BufferedReader reader = new BufferedReader(new    
  8.                     InputStreamReader(connection.getInputStream(),charset));     
  9.             String line;     
  10.             while((line = reader.readLine())!= null){      
  11.                 result += line;     
  12.                 result += "\n";     
  13.             }     
  14.         } catch (Exception e) {     
  15.             e.printStackTrace();     
  16.             return "";     
  17.         }     
  18.         if(result!=null&&!"".equals(result)){   
  19.             result = result.substring(result.indexOf("{" +   
  20.             ""), (result.indexOf("}")+1) );     
  21.         }    
  22.         return result;       
  23.     }   

Test測試方法:

  1. public static void main(String[] args) {   
  2.         String url = "http://www.youdao.com/smartresult-xml/search.s?" +   
  3.                      "jsFlag=true&type=mobile&q=13985046628";   
  4.         String result = callUrlByGet(url,"GBK");   
  5.         System.out.println(result);      
  6.     }   

輸出的結果:

{'product':'mobile','phonenum':'13985046628','location':'貴州 貴陽'}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章