The asynchronous callback case for IP query system

  • 2020-06-07 04:31:15
  • OfStack

Without further ado, see the code:


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{
  //  Download the end of the callback interface 
  public interface IResult{
    void success(String msg);
    void fail(String msg);
  }
  //  Create a reference to a network address 
  String addr;
  //  Creates a reference to the callback interface 
  IResult iResult ;
  //  Build constructors pass in network and interface references 
  public Http(String addr, IResult iResult) {
    super();
    this.addr = addr;
    this.iResult = iResult;
    //  Open the thread   thread 1 Turning it on creates asynchrony 
    start();
  }
  @Override
  //  Overridden thread's run Method, as long as the network will use multithreading 
  public void run() {
    try {
      //  create URL Pass in the network address 
      URL url = new URL(addr);
      try {
        //  Read network data using character buffer read stream 
        BufferedReader br = new BufferedReader(new InputStreamReader(url.openStream()));
        //  create stringbuffer object 
        StringBuffer sb = new StringBuffer();
        //  create 1 A variable that temporarily stores a string 
        String temp;
        //  when temp Not for nothing is just 1 Direct reading data 
        while ((temp = br.readLine()) != null) {
          sb.append(temp);
        }
        //  After the data is read successfully 
        //  Passes all read data to and from the callback interface 
        iResult.success(sb.toString());
      } catch (IOException e) {
        e.printStackTrace();
      }
    } catch (MalformedURLException e) {
      e.printStackTrace();
      //  An exception occurred on the network request 
      iResult.fail(" Network request failed ");
    }
  }
}

package com.lxj.demo;
import net.sf.json.JSONObject;
import com.xykj.demo.Http.IResult;
public class IPRequest {
  //  request IP The callback interface for the message, in this case you have to use the callback because you don't know Http When will the download be complete 
  public interface IRequest{
    void success(IPBean ipBean);
    void fail(String msg);
  }
  //  As long as give 1 a ip Address and callback interface, through which we can return the converted ip Information object 
  public void request(String IP,IRequest iRequest){
    //  Input by the user ip Address splicing to url Initiated, Http request 
    String addr ="http://apicloud.mob.com/ip/query?key=520520test&ip="
        + IP
        + "";
    new Http(addr, new IResult() {
      //  To create the IResult success and fail It won't be called right away, it has to wait Http It will not be called until the download is complete or an exception occurs 
      @Override
      public void success(String msg) {
        //  The receiving json Convert data into IPBean object 
        JSONObject jsonObject = JSONObject.fromObject(msg);
        IPBean ipBean = (IPBean) JSONObject.toBean(jsonObject, IPBean.class);
        //  Generates a callback that passes the transformed object to Demo
        iRequest.success(ipBean);
      }
      @Override
      public void fail(String msg) {
        // Http When the request fails 
        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
    //  I have to rewrite it here toString Method, otherwise will print the address inside the memory 
    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("************ Welcome to use IP Query system ************");
    Scanner sc = new Scanner(System.in);
    //  create IPRequest object 
    IPRequest ipRequest = new IPRequest();
    while (true) {
      System.out.println(" Please enter what you want to query IP:");
      String ip = sc.next();
      //  call IPRequest The inside of the request methods 
      ipRequest.request(ip, new IRequest() {
        @Override
        public void success(IPBean ipBean) {
          System.out.println("************* The query results *************");
          System.out.println(ipBean.getResult());
        }
        @Override
        public void fail(String msg) {
          System.out.println(msg);
        }
      });
    }
  }
}

Related articles: