欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

java调用淘宝api联网查询ip归属地

程序员文章站 2024-02-26 20:14:58
淘宝返回的数据为:{"code":0,"data":{"country":"\u4e2d\u56fd","country_id":"cn","area":"\u534e\u...

淘宝返回的数据为:
{"code":0,"data":{"country":"\u4e2d\u56fd","country_id":"cn","area":"\u534e\u4e1c","area_id":"300000","region":"\u5c71\u4e1c\u7701","region_id":"370000","city":"\u4e1c\u8425\u5e02","city_id":"370500","county":"","county_id":"-1","isp":"\u8054\u901a","isp_id":"100026","ip":"60.214.183.158"}}

用法:

复制代码 代码如下:

string arr1 = getaddressbyip.getaddressbyip("120.192.182.1");
system.out.println(arr1);


详细请看代码

复制代码 代码如下:

package getaddressbyip;

import java.io.bytearrayoutputstream;
import java.io.ioexception;
import java.io.inputstream;
import java.net.httpurlconnection;
import java.net.malformedurlexception;
import java.net.url;

import net.sf.json.jsonobject;

public class getaddressbyip
{

 
 /**
  *
  * @param ip
  * @return
  */
 public static string getaddressbyip(string ip){
  string resout = "";
  try{
   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("data");
   string code = (string) obj.get("code");
   if(code.equals("0")){

    resout =  obj2.get("country")+"--" +obj2.get("area")+"--" +obj2.get("city")+"--" +obj2.get("isp");
   }else{
    resout =  "ip地址有误";
   }
  }catch(exception e){

   e.printstacktrace();
    resout = "获取ip地址异常:"+e.getmessage();
  }
  return resout;

 }

    public static string getjsoncontent(string urlstr)
    {
        try
        {// 获取httpurlconnection连接对象
            url url = new url(urlstr);
            httpurlconnection httpconn = (httpurlconnection) url
                    .openconnection();
            // 设置连接属性
            httpconn.setconnecttimeout(3000);
            httpconn.setdoinput(true);
            httpconn.setrequestmethod("get");
            // 获取相应码
            int respcode = httpconn.getresponsecode();
            if (respcode == 200)
            {
                return convertstream2json(httpconn.getinputstream());
            }
        }
        catch (malformedurlexception e)
        {
            e.printstacktrace();
        }
        catch (ioexception e)
        {
            e.printstacktrace();
        }
        return "";
    }
    private static string convertstream2json(inputstream inputstream)
    {
        string jsonstr = "";
        // bytearrayoutputstream相当于内存输出流
        bytearrayoutputstream out = new bytearrayoutputstream();
        byte[] buffer = new byte[1024];
        int len = 0;
        // 将输入流转移到内存输出流中
        try
        {
            while ((len = inputstream.read(buffer, 0, buffer.length)) != -1)
            {
                out.write(buffer, 0, len);
            }
            // 将内存流转换为字符串
            jsonstr = new string(out.tobytearray());
        }
        catch (ioexception e)
        {
            // todo auto-generated catch block
            e.printstacktrace();
        }
        return jsonstr;
    }
}