您好,登錄后才能下訂單哦!
話不多說,請看代碼:
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); } }); } } }
以上就是本文的全部內容,希望本文的內容對大家的學習或者工作能帶來一定的幫助,同時也希望多多支持億速云!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。