根据ip查询ip归属地

 http://www.oschina.net/code/snippet_944819_33978

http://www.jb51.net/article/54287.htm

 

	public String getAddressByIP(String strIP)
	{ 
	  try
	  {
	    //String strIP = "61.132.31.61";
	    URL url = new URL( "http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=json&ip=" + strIP); 
	    URLConnection conn = url.openConnection(); 
	    BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF8")); 
	    String line = null; 
	    StringBuffer result = new StringBuffer(); 
	    while((line = reader.readLine()) != null)
	    { 
	      result.append(line); 
	    } 
	    reader.close(); 
	    System.out.println(result.toString());
        
        JSONObject obj = JSONObject.fromObject(result.toString());
        int code = (Integer) obj.get("ret");
        String resout="";
        if(code==1){
            resout =  obj.get("country") +" "+obj.get("province")+obj.get("city") +obj.get("district") +obj.get("isp");
        }else{
        	//resout=strIP;
        }
	    return resout;
	  }
	  catch( IOException e)
	  { 
	    return "读取失败"; 
	  }catch(Exception e){
		  return strIP;
	  }
	}

 

posted @ 2016-02-04 16:07  JIN__JIN  阅读(297)  评论(0编辑  收藏  举报