java調(diào)用淘寶api聯(lián)網(wǎng)查詢ip歸屬地
淘寶返回的數(shù)據(jù)為:
{"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相當于內(nèi)存輸出流
ByteArrayOutputStream out = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len = 0;
// 將輸入流轉(zhuǎn)移到內(nèi)存輸出流中
try
{
while ((len = inputStream.read(buffer, 0, buffer.length)) != -1)
{
out.write(buffer, 0, len);
}
// 將內(nèi)存流轉(zhuǎn)換為字符串
jsonStr = new String(out.toByteArray());
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
return jsonStr;
}
}
相關文章
使用Spring MVC攔截器實現(xiàn)日志記錄的方法
本篇文章主要介紹了使用Spring MVC攔截器實現(xiàn)日志記錄的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-04-04IDEA配置Gradle及Gradle安裝的實現(xiàn)步驟
本文主要介紹了IDEA配置Gradle及Gradle安裝的實現(xiàn)步驟,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2023-08-08springbooot整合dynamic?datasource數(shù)據(jù)庫密碼加密方式
這篇文章主要介紹了springbooot整合dynamic?datasource?數(shù)據(jù)庫密碼加密方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-01-01使用java springboot設計實現(xiàn)的圖書管理系統(tǒng)(建議收藏)
這篇文章主要介紹了使用java springboot設計實現(xiàn)的圖書管理系統(tǒng),包含了整個的開發(fā)過程,以及過程中遇到的問題和解決方法,對大家的學習和工作具有借鑒意義,建議收藏一下2021-08-08Java reservedcodecachesize虛擬機參數(shù)案例詳解
這篇文章主要介紹了Java reservedcodecachesize虛擬機參數(shù)案例詳解,本篇文章通過簡要的案例,講解了該項技術的了解與使用,以下就是詳細內(nèi)容,需要的朋友可以參考下2021-08-08springboot aspect通過@annotation進行攔截的實例代碼詳解
這篇文章主要介紹了springboot aspect通過@annotation進行攔截的方法,本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-08-08Springboot整合MongoDB的Docker開發(fā)教程全解
這篇文章主要介紹了Springboot整合MongoDB的Docker開發(fā),本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值 ,需要的朋友可以參考下2020-07-07Spring?Data?JPA系列QueryByExampleExecutor使用詳解
這篇文章主要為大家介紹了Spring?Data?JPA系列QueryByExampleExecutor使用示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-09-09微信小程序獲取手機號的完整實例(Java后臺實現(xiàn))
我們在做小程序開發(fā)的過程中,經(jīng)常會涉及到用戶身份的問題,最普遍的就是我們要獲取用戶的手機號碼,下面這篇文章主要給大家介紹了關于微信小程序獲取手機號的完整實例,后臺由Java實現(xiàn),需要的朋友可以參考下2022-06-06