IP查询系统的异步回调案例
话不多说,请看代码:
package com.lxj.demo; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.net.MalformedURLException; import java.net.URL; public class Http extends Thread{ // 下载结束的回调接口 public interface IResult{ void success(String msg); void fail(String msg); } // 创建网络地址的引用 String addr; // 创建回调接口的引用 IResult iResult ; // 生成构造方法把网络和接口的引用传进去 public Http(String addr, IResult iResult) { super(); this.addr = addr; this.iResult = iResult; // 开启线程 线程一开启就产生了异步 start(); } @Override // 重写线程的run方法,只要是网络就要用到多线程 public void run() { try { // 创建URL,把网络地址传进去 URL url = new URL(addr); try { // 利用字符缓冲读取流读取网络数据 BufferedReader br = new BufferedReader(new InputStreamReader(url.openStream())); // 创建stringbuffer对象 StringBuffer sb = new StringBuffer(); // 创建一个临时储存字符串的变量 String temp; // 当temp不为空是就一直读取数据 while ((temp = br.readLine()) != null) { sb.append(temp); } // 数据读取成功后 // 把读取的所有数据传到回调接口,再由接口传出去 iResult.success(sb.toString()); } catch (IOException e) { e.printStackTrace(); } } catch (MalformedURLException e) { e.printStackTrace(); // 网络请求发生异常 iResult.fail("网络请求失败"); } } }
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 + "\ncountry:" + country + "\nip:" + ip + "\nprovince:" + 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); } }); } } }
以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,同时也希望多多支持我们!
赞 (0)