IP查询系统的异步回调案例
程序员文章站
2024-03-06 14:42:44
话不多说,请看代码:
package com.lxj.demo;
import java.io.bufferedreader;
import java.io....
话不多说,请看代码:
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); } }); } } }
以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,同时也希望多多支持!