Java百宝箱——实现ip地址、手机号、身份证号归属地查询

本文介绍了一个包含IP地址查询、天气预报、简繁体转换、身份证归属地及手机号码归属查询等功能的生活百宝箱Java应用程序。利用API接口实现了IP地址的精确查询,并通过身份证号直接查询归属地。

1、功能界面

package com.company;

import com.alibaba.fastjson.JSONObject;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Scanner;

public class Main {

    //菜单
    public static String menu(){
        return "\t===========生活百宝箱===========\n" +
                "1、ip地址来源查询\n"+
                "2、天气预报查询\n"+
                "3、中文简繁体双向转换\n"+
                "4、身份证归属查询\n"+
                "5、手机号码归属查询\n" +
                "6、退出";
    }


    public static void main(String[] args) {
	// write your code here
        Scanner sc=new Scanner(System.in);
        int choice=-1;
        while (true){
            System.out.println(menu());
            System.out.print("请选择操作:");

            //用户输入数字选择操作,如果输入操作不符合规范,提示错误信息后让用户继续选择
            try {
                choice = sc.nextInt();
                //如果输入为6则退出
                if (choice==6)
                    break;
                else if(choice==1) {
                    System.out.print("请输入ip地址");
                    String ip=sc.next();
                    IpUtil.getIp(ip);
                    continue;
                }
                else if(choice==2){
                    System.out.print("请输入城市名称:");
                    String city=sc.next();
                    System.out.println(WeatherUtil.getWeather(city));
                    continue;
                }
                else if(choice==3){
                    jianfanConvert.jianfan();
                    continue;
                }
                else if(choice==4){
                    System.out.print("请输入身份证号:");
                    String cardNum=sc.next();
                    IdCardUtil.idCard(cardNum);
                    continue;
                }
                else if(choice==5){
                    System.out.print("请输入手机号:");
                    String phone=sc.next();
                    System.out.println(MobileLocationUtil.getMobileNoTrack(phone));
                }
            }
            catch (Exception e){
                continue;
            }
        }
    }
}

2、ip地址查询

通过java实现ip地址查询的方法主要就是通过接口的调用。
这里使用的地址为太平洋网提供的api,该api调用能精确到区县。
先看实现的效果
在这里插入图片描述
ip地址查询函数。接口返回的数据是json格式,所以这里应用的是阿里的fastjson的jar包来解析返回的数据。

package com.company;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import java.util.regex.*;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

/**
 * @Author: jack
 */
public class IpUtil {

    public static void getIp(String ip) {

//     //接口连接地址
        String urlStr="http://whois.pconline.com.cn/ipJson.jsp?ip="+ip+"&json=true";
        //调用getResult获取接口返回值
        String result =  getResult(urlStr, "gbk");
        //解析json获取地址
        JSONObject jsonObject=JSONObject.parseObject(result);
        System.out.println(jsonObject.getString("addr"));

    }


    private static String getResult(String urlStr, String encoding) {
        URL url = null;
        HttpURLConnection connection = null;
        try {
            url = new URL(urlStr);
            connection = (HttpURLConnection) url.openConnection(); // 新建连接实例
            connection.setConnectTimeout(20000); // 设置连接超时时间,单位毫秒
            connection.setReadTimeout(20000); // 设置读取数据超时时间,单位毫秒
            connection.setDoOutput(true); // 是否打开输出流 true|false
            connection.setDoInput(true); // 是否打开输入流true|false
            connection.setRequestMethod("GET"); // 提交方法POST|GET
            connection.setUseCaches(false); // 是否缓存true|false
            connection.connect(); // 打开连接端口
            BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream(), encoding));// 往对端写完数据对端服务器返回数据// ,以BufferedReader流来读取

            String text="";
            String line = "";
            while ((line = reader.readLine()) != null) {
                text+=line+"\n";
            }
            reader.close();
            return text;
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (connection != null) {
                connection.disconnect(); // 关闭连接
            }
        }
        return null;
    }

}

3、身份证归属地查询

身份证归属地查询这个的处理方法就相对简单粗暴了一些。直接将每个城市或者区县对应的编码找到之后读取,或者直接放进代码当中即可。比如下图这样
在这里插入图片描述
以下省略nnnn行代码…

4、更多查询

完整源码以及其他更多查询可以关注一下公众弄号回复"0006"获取源码
在这里插入图片描述

适配机型/应用平台 Java/S40 适用机型:诺基亚 7610S; 7510a; 7310c; 7070; 6103; 5000; 3120c; 2680s; 2600c; 1680c; 5220xm; 3600s; 7360; 7380; 6070; 8800 SE; 5300; 6088; 8800 Sirocco; 6086; 8600 Luna; 3500c; 7900; 6131i; 6263; 7500; 8800 Arte; 8600; 7390; 7373; 7370; 6555; 6288; 6280; 6275; 6267; 6165; 6151; 6136; 6133; 6131; 6126; 6125; 6111; 6085; 6080; 6060; 6021; 5070; 3500; 3110; 3109; 2760; 2630; 2626; 2610; 6301; 5610xm; 5310xm; 6108; 7260; 6270; 6030; 6020; 3220; 3120; 2865; 6300; 6234; 6233; 5200; 6500c; 6500s; 7600; 7210; 7200; 6800; 6650; 6610; 6230; 6220; 6100; 5100; 3300; 3200; 3100 摩托罗拉 E398; EM25; EM28; K3; KRZR; L6; L6g; L800t; Pebl U3; RAZR maxx V1100; ROKR W5; SLVR L6i; SLVR L7; SLVR L71; SLVR L72; V3; V300; V303; V500; V501; V600; VE75; W206; W230; W270; W360; W380; W388; W395; W396; W510; W6; ZN200; w231 索尼爱立信 C702; C902; G502; K660i; R306c; T280i; T303c; W350c; W760c; Z550c; Z558c; Z600; Z608; Z610i; Z780 三星 D528; D808; D828; D838; D848; D888; D908; D908i; D988; E218; E258; E428; E598; E848; E878; E898; E908; E958; F218; F258; F268; F278; F308; F488E; G608; G618; G800; G808E; J608; J618; J708; M318; M618; M628; Soulb; U308; U600; U608; U708E; U800; U808E; U900; U908E; W579; W629; X838 联想 E520; P705; P719; P790; P960; P990; S320; S530; S6; S600; S7; S70; S9; V80; i510; i758; i760; i817; i827; i880; i906; i909; i966; i968; p609; s90; s96 夏新 A510; A530; A616; A636; E3; E5; M515; M690 天语 A615; A902; A905; A906; A908; A909; A930; B922; C280; C800; D705; E60
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Demonslzh6

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值