Os dados retornados por Taobao são:
{"code": 0, "dados": {"country": "/u4e2d/u56fd", "country_id": "cn", "área": "/u534e/u4e1c", "are_id": "300000", "região": "/u5c71/u4e1c/u7701", "region_id": " 370000 "," City ":"/u4e1c/u8425/u5e02 "," city_id ":" 370500 "," condado ":", "county_id": "-1", "isp": "/u8054/u901a" , "ISP_ID": "100026", "IP": "60.214.183.158"}}
uso:
A cópia do código é a seguinte:
String arr1 = getAddressByip.getAddressByip ("120.192.182.1");
System.out.println (ARR1);
Por favor, veja o código para obter detalhes
A cópia do código é a seguinte:
pacote getAddressByip;
importar java.io.byteArrayOutputStream;
importar java.io.ioException;
importar java.io.inputStream;
importar java.net.httpurlConnection;
importar java.net.MalformEdUrlexception;
importar java.net.url;
importação net.sf.json.jsonObject;
Classe pública GetAddressByip
{
/**
*
* @param ip
* @retornar
*/
public static string getaddressbyip (string ip) {
String resout = "";
tentar{
String str = getjsonContent ("http://ip.taobao.com/service/getipinfo.php?ip="+ip);
System.out.println (str);
JsonObject obj = jsonObject.FromObject (str);
JsonObject obj2 = (jsonObject) obj.get ("dados");
String code = (string) obj.get ("code");
if (code.equals ("0")) {
resout = obj2.get ("country") +"-" +obj2.get ("área") +"-" +obj2.get ("city") +"-" +obj2.get ("isp ");
}outro{
resout = "O endereço IP está incorreto";
}
} catch (Exceção e) {
E.PrintStackTrace ();
resolve = "Obtenha a exceção do endereço IP:"+e.getMessage ();
}
resposta de retorno;
}
public static string getjsonContent (string urlstr)
{
tentar
{// Obtenha o objeto de conexão httpurlConnection
Url url = novo url (urlstr);
HttpurlConnection httpconn = (httpurlConnection) URL
.OPENCONNEÇÃO ();
// Definir propriedades de conexão
httpconn.setConnectTimeout (3000);
httpconn.setdoinput (true);
httpconn.setRequestMethod ("get");
// Obtenha o código correspondente
int respcode = httpconn.getRESPONSecode ();
if (respcode == 200)
{
retorno convertStream2json (httpconn.getInputStream ());
}
}
Catch (malformedurlexception e)
{
E.PrintStackTrace ();
}
Catch (ioexception e)
{
E.PrintStackTrace ();
}
retornar "";
}
String estática privada convertStream2json (InputStream inputStream)
{
String jSonstr = "";
// bytearrayOutputStream é equivalente ao fluxo de saída de memória
ByteArrayOutputStream Out = new ByteArrayOutputStream ();
byte [] buffer = novo byte [1024];
int len = 0;
// Transfira o fluxo de entrada para o fluxo de saída de memória
tentar
{
while ((len = inputStream.read (buffer, 0, buffer.length))! = -1)
{
out.write (buffer, 0, len);
}
// Converta o fluxo de memória em string
jSonstr = new String (out.tobyteArray ());
}
Catch (ioexception e)
{
// TODO BLOCO DE CAPAGEM AUTOMAGEM
E.PrintStackTrace ();
}
retornar JSonstr;
}
}