1 package com.lxj.demo;
2
3 import java.io.BufferedReader;
4 import java.io.IOException;
5 import java.io.InputStreamReader;
6 import java.net.MalformedURLException;
7 import java.net.URL;
8
9 public class Http extends Thread{
10 // 下载结束的回调接口
11 public interface IResult{
12 void success(String msg);
13 void fail(String msg);
14 }
15 // 创建网络地址的引用
16 String addr;
17 // 创建回调接口的引用
18 IResult iResult ;
19 // 生成构造方法把网络和接口的引用传进去
20 public Http(String addr, IResult iResult) {
21 super();
22 this.addr = addr;
23 this.iResult = iResult;
24 // 开启线程 线程一开启就产生了异步
25 start();
26 }
27 @Override
28 // 重写线程的run方法,只要是网络就要用到多线程
29 public void run() {
30 try {
31 // 创建URL,把网络地址传进去
32 URL url = new URL(addr);
33 try {
34 // 利用字符缓冲读取流读取网络数据
35 BufferedReader br = new BufferedReader(new InputStreamReader(url.openStream()));
36 // 创建stringbuffer对象
37 StringBuffer sb = new StringBuffer();
38 // 创建一个临时储存字符串的变量
39 String temp;
40 // 当temp不为空是就一直读取数据
41 while ((temp = br.readLine()) != null) {
42 sb.append(temp);
43 }
44 // 数据读取成功后
45 // 把读取的所有数据传到回调接口,再由接口传出去
46 iResult.success(sb.toString());
47 } catch (IOException e) {
48 e.printStackTrace();
49 }
50 } catch (MalformedURLException e) {
51 e.printStackTrace();
52 // 网络请求发生异常
53 iResult.fail("网络请求失败");
54 }
55 }
56 }
package com.lxj.demo;
import net.sf.json.JSONObject;
import com.xykj.demo.Http.IResult;
public class IPRequest {
// 请求IP信息的回调接口,在这里必须要使用回调,因为不知道Http什么时候下载完成
public interface IRequest{
void success(IPBean ipBean);
void fail(String msg);
}
// 只要给一个ip地址和回调接口,我们可以通过回调接口传回转化好的ip信息对象
public void request(String IP,IRequest iRequest){
// 把用户输入的ip地址拼接到url中,发起Http请求
String addr ="http://apicloud.mob.com/ip/query?key=520520test&ip="
+ IP
+ "";
new Http(addr, new IResult() {
// 创建完的IResult success和fail不会马上调用,必须要等到Http下载完成或者发生异常才会调用
@Override
public void success(String msg) {
// 把接收的json数据转化成IPBean对象
JSONObject jsonObject = JSONObject.fromObject(msg);
IPBean ipBean = (IPBean) JSONObject.toBean(jsonObject, IPBean.class);
// 产生回调,把转化好的对象传递出去给Demo
iRequest.success(ipBean);
}
@Override
public void fail(String msg) {
// Http请求失败的时候
iRequest.fail(msg);
}
});
}
}
package com.lxj.demo;
public class IPBean {
public static class Result{
private String city;
private String country;
private String ip;
private String province;
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getCountry() {
return country;
}
public void setCountry(String country) {
this.country = country;
}
public String getIp() {
return ip;
}
public void setIp(String ip) {
this.ip = ip;
}
public String getProvince() {
return province;
}
public void setProvince(String province) {
this.province = province;
}
@Override
// 这里必须要重写toString方法,否则将打印的内存里面的地址
public String toString() {
return "city:" + city + "
country:" + country + "
ip:"
+ ip + "
province:" + province;
}
}
Result result;
private String msg;
private String retCode;
public Result getResult() {
return result;
}
public void setResult(Result result) {
this.result = result;
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
public String getRetCode() {
return retCode;
}
public void setRetCode(String retCode) {
this.retCode = retCode;
}
}
package com.lxj.demo;
import java.util.Scanner;
import com.xykj.demo.IPRequest.IRequest;
public class Demo {
public static void main(String[] args) {
System.out.println("************欢迎使用IP查询系统************");
Scanner sc = new Scanner(System.in);
// 创建IPRequest对象
IPRequest ipRequest = new IPRequest();
while (true) {
System.out.println("请输入要查询的IP:");
String ip = sc.next();
// 调用IPRequest里面的request方法
ipRequest.request(ip, new IRequest() {
@Override
public void success(IPBean ipBean) {
System.out.println("*************查询结果*************");
System.out.println(ipBean.getResult());
}
@Override
public void fail(String msg) {
System.out.println(msg);
}
});
}
}
}