package com.util; import java.net.InetAddress; import java.net.NetworkInterface; import java.net.SocketException; import java.net.UnknownHostException; import java.nio.ByteBuffer; import java.nio.CharBuffer; import java.nio.charset.Charset; import java.nio.charset.CharsetDecoder; import java.nio.charset.CharsetEncoder; import java.util.Enumeration; public class Utils { static String TAG = "pp"; static public final boolean DEBUG = false; public static InetAddress getLocalIpAddress() throws UnknownHostException { try { for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements();) { NetworkInterface intf = en.nextElement(); for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements();) { InetAddress inetAddress = enumIpAddr.nextElement(); if (!inetAddress.isLoopbackAddress()) { return inetAddress; } } } } catch (SocketException ex) { LOG.error(ex.toString()); } return InetAddress.getLocalHost(); } private static CharsetEncoder sEncoder = Charset.forName("ISO-8859-1").newEncoder(); private static CharsetDecoder sDecoder = Charset.forName("GBK").newDecoder(); public static String convertGBK(String input) { try { ByteBuffer bbuf = sEncoder.encode(CharBuffer.wrap(input)); CharBuffer cbuf = sDecoder.decode(bbuf); String output = cbuf.toString(); return output; } catch (Exception e) { //e.printStackTrace(); return input; } } }