k780数据解析测试

6 篇文章 1 订阅
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();
    }

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值