package cn.edu.lit.yw5;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import net.sf.json.JSONObject;
public class K780DataTest {
public static void main(String[] args) throws Exception {
System.out.println(getIPInfo("183.129.214.98"));
System.out.println(queryNowTime());
}
/**
* 获得系统当前时间
* @return
*/
public static String queryNowTime(){
String url = "http://api.k780.com:88/?app=life.time&appkey=14841&sign=6971f83ee124d889d7a1379611c1ae5b&format=json";//获得当前时间的URL
String jsonStr = loadJSON(url);//获得返回的json
//解析json
JSONObject object = JSONObject.fromObject(jsonStr);//获得json对象
JSONObject object2 = object.getJSONObject("result");//获得result键对应的json
String dateStr1 = object2.getString("datetime_1");
String dateStr2 = object2.getString("datetime_2");
return "当前时间为:\n"+dateStr1+"\n"+dateStr2;
}
/**
* 获得IP的归属地信息
* @param ipStr
* @return
*/
public static String getIPInfo(String ipStr) {
String url = "http://api.k780.com:88/?app=ip.get&ip="+ipStr+"&appkey=14841&sign=6971f83ee124d889d7a1379611c1ae5b&format=json";//获得IP的URL
String jsonStr = loadJSON(url);//获得返回的json
//解析json
JSONObject object = JSONObject.fromObject(jsonStr);//获得json对象
JSONObject object2 = object.getJSONObject("result");//获得result对应的json
String ip = object2.getString("ip");//获得json返回的ip
String operators = object2.getString("operators");//获得运营商
String area_style_simcall = object2.getString("area_style_simcall");//获得归属地
return "IP: "+ip+"\nInfo:"+area_style_simcall+"---"+operators;
}
/**
* 获得页面返回的字符串
* @param urlStr
* @return
*/
//获得页面返回的json
public static String loadJSON (String urlStr) {
StringBuilder json = new StringBuilder();
try {
URL url = new URL(urlStr);//获得url对象
URLConnection connect = url.openConnection();//获得URLConnect对象,连接资源
BufferedReader in = new BufferedReader(new InputStreamReader(connect.getInputStream(),"utf-8"));//通过URLConnect获得输入流对象,通过转换流指定编码方式
String inputLine = null;
while ( (inputLine = in.readLine()) != null) {
json.append(inputLine);
}
in.close();
} catch (MalformedURLException e) {
} catch (IOException e) {
}
return json.toString();
}
}
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import net.sf.json.JSONObject;
public class K780DataTest {
public static void main(String[] args) throws Exception {
System.out.println(getIPInfo("183.129.214.98"));
System.out.println(queryNowTime());
}
/**
* 获得系统当前时间
* @return
*/
public static String queryNowTime(){
String url = "http://api.k780.com:88/?app=life.time&appkey=14841&sign=6971f83ee124d889d7a1379611c1ae5b&format=json";//获得当前时间的URL
String jsonStr = loadJSON(url);//获得返回的json
//解析json
JSONObject object = JSONObject.fromObject(jsonStr);//获得json对象
JSONObject object2 = object.getJSONObject("result");//获得result键对应的json
String dateStr1 = object2.getString("datetime_1");
String dateStr2 = object2.getString("datetime_2");
return "当前时间为:\n"+dateStr1+"\n"+dateStr2;
}
/**
* 获得IP的归属地信息
* @param ipStr
* @return
*/
public static String getIPInfo(String ipStr) {
String url = "http://api.k780.com:88/?app=ip.get&ip="+ipStr+"&appkey=14841&sign=6971f83ee124d889d7a1379611c1ae5b&format=json";//获得IP的URL
String jsonStr = loadJSON(url);//获得返回的json
//解析json
JSONObject object = JSONObject.fromObject(jsonStr);//获得json对象
JSONObject object2 = object.getJSONObject("result");//获得result对应的json
String ip = object2.getString("ip");//获得json返回的ip
String operators = object2.getString("operators");//获得运营商
String area_style_simcall = object2.getString("area_style_simcall");//获得归属地
return "IP: "+ip+"\nInfo:"+area_style_simcall+"---"+operators;
}
/**
* 获得页面返回的字符串
* @param urlStr
* @return
*/
//获得页面返回的json
public static String loadJSON (String urlStr) {
StringBuilder json = new StringBuilder();
try {
URL url = new URL(urlStr);//获得url对象
URLConnection connect = url.openConnection();//获得URLConnect对象,连接资源
BufferedReader in = new BufferedReader(new InputStreamReader(connect.getInputStream(),"utf-8"));//通过URLConnect获得输入流对象,通过转换流指定编码方式
String inputLine = null;
while ( (inputLine = in.readLine()) != null) {
json.append(inputLine);
}
in.close();
} catch (MalformedURLException e) {
} catch (IOException e) {
}
return json.toString();
}
}