Asynchronous callback of the IP query system, ip Query System callback
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 // download end callback interface 11 public interface IResult {12 void success (String msg); 13 void fail (String msg ); 14} 15 // create a network address reference 16 String addr; 17 // create a callback interface reference 18 IResult iResult; 19 // generate a structure Method: Upload the network and interface references to 20 public Http (String addr, IResult iResult) {21 super (); 22 this. addr = addr; 23 this. iResult = iResult; 24 // when the thread is enabled, the asynchronous 25 start (); 26} 27 @ Override28 // rewrite the thread's run method, multi-threaded 29 public void run () {30 try {31 // create a URL as long as it is a network, and upload the network address to 32 URL url = new URL (addr ); 33 try {34 // read network data using the character buffer 35 BufferedReader br = new BufferedReader (new InputStreamReader (url. openStream (); 36 // create stringbu Ffer object 37 StringBuffer sb = new StringBuffer (); 38 // create a temporary String storage variable 39 String temp; 40 // when temp is not empty, it will always read data 41 while (temp = br. readLine ())! = Null) {42 sb. append (temp); 43} 44 // after the data is read successfully, 45 // upload all the read data to the callback interface, and then pass the API out 46 iResult. success (sb. toString (); 47} catch (IOException e) {48 e. printStackTrace (); 49} 50} catch (MalformedURLException e) {51 e. printStackTrace (); 52 // network request exception 53 iResult. fail ("Network request failed"); 54} 55} 56}
Package com. lxj. demo; import net. sf. json. JSONObject; import com. xykj. demo. http. IResult; public class IPRequest {// callback interface for requesting IP information, which must be used here, because I don't know when Http will download public interface IRequest {void success (IPBean ipBean); void fail (String msg);} // I only need to give an IP address and callback interface, we can use the callback interface to return the converted ip information object public void request (String IP, IRequest iRequest) {// concatenate the ip address entered by the user into the url, initiate Http request String addr = "http://apicloud.mob.com/ip/quer Y? Key = 520520 test & ip = "+ IP +" "; new Http (addr, new IResult () {// The created IResult success and fail will not be called immediately, @ Override public void success (String msg) must be called until the Http download is complete or an exception occurs. {// convert the received json data to the IPBean object JSONObject jsonObject = JSONObject. fromObject (msg); IPBean ipBean = (IPBean) JSONObject. toBean (jsonObject, IPBean. class); // generates a callback to pass the converted object to Demo iRequest. success (ipBean) ;}@ Override public void fail (String msg) {// iRequest when the Http request fails. 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 // here the toString method must be rewritten, otherwise the address in the printed memory will be 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 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. producer; import com. xykj. demo. IPRequest. IRequest; public class Demo {public static void main (String [] args) {System. out. println ("************** welcome to the IP address query system ************"); using SC = new using (System. in); // create IPRequest object IPRequest ipRequest = new IPRequest (); while (true) {System. out. println ("Enter the IP address to query:"); String ip = SC. next (); // call the request Method IPRequest in 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 );}});}}}