public static String calcMobileCity ( String mobileNumber ) throws MalformedURLException { ObjectMapper objectMapper = new ObjectMapper ( ) ; String jsonString = null ; String urlString = "http://tcc.taobao.com/cc/json/mobile_tel_segment.htm?tel=" + mobileNumber ; StringBuffer sb = new StringBuffer ( ) ; BufferedReader buffer ; URL url = new URL ( urlString ) ; try { InputStream in = url . openStream ( ) ; //solve the garbage problem buffer = new BufferedReader ( new InputStreamReader ( in , "gb2312" ) ) ; String line = null ; while ( ( line = buffer . readLine ( ) ) != null ) { sb . append ( line ) ; } in . close ( ) ; buffer . close ( ) ; // System.out.println(sb.toString()); jsonString = sb . toString ( ) ; EntityHelper . print ( jsonString ) ; //replace \ jsonString = jsonString . replaceAll ( "^[__]\\w{14}+[_ = ]+" , "[" ) ; // System.out.println(jsonString+"]"); String jsonString2 = jsonString + "]" ; //json object into the STRING // array = JSONArray.fromObject(jsonString2); //Get JSONArray of JSONObject object , easy to read array of key-value pairs in // jsonObject = array.getJSONObject(0); } catch ( Exception e ) { e . printStackTrace ( ) ; } // return jsonObject.getString("province"); return null ; }
Test the phone number from which the city is using Taobao API