Java -- 网络编程

网络编程

  • 强引用、软引用、弱引用、虚引用
    如果 一个对象有强引用引用它,那么一定不会被GC掉
    如果一个对象有软引用引用它,那么在内存不足时就会被GC掉
    如果 一个对象有弱引用它,那么在发生垃圾回收时就会被GC掉
    通常软引用和弱引用都是用来实现对象缓存功能的
    一般也不会直接使用SoftReference和WeakReference类
    而是使用一个叫做WeakHashMap的容器
    WeakHashMap的键是以弱引用的方式持有的
    键值对中的键所引用的对象被释放后会自动移除键值对组合
    此项功能最适合用来设计对象缓存(容器中持有可留可不留的对象)

  • 协议 - 通信双方对话的标准和规范

  • HTTP - 超文本传输协议 - Hyper-Text Transfer Protocol

  • HTTP请求
  • GET - 从服务器获取资源
  • POST - 向服务器提交数据
  • 请求行 ---> 命令 资源路径/资源名称 HTTP/1.1\r\n
  • 请求头 ---> 多组 键:值
  • \r\n
  • 消息体(可以为空)
  • HTTP响应
  • 响应行 ---> HTTP/1.1 状态码
  • 响应头 ---> 多组 键:值
  • \r\n
  • 消息体
  • test/html ---> HTML书写的网页
  • test/xml ---> 以XML格式表示的数据
  • 对于移动客户端,绝大多数是以下形式:
  • application/json ---> 以JSON格式表示的数据
  • HTML - Hyper-Text Markup Language
  • 标签 --- 内容 (H5)
  • 样式表 --- 显示 (CSS3)
  • JavaScript --- 行为 (ECMA6) ---> Ajax
  • URL - 统一资源定位符 - 唯一标记某一个资源
  • 协议://域名或者IP地址:端口/路径/资源名称[?查询字符串]
  • 查询字符串:参数名1=参数值1&参数名2=参数值2
  • 端口 - 对IP地址的扩展 - 用于区分不同的服务
  • DNS - 域名服务 - 将域名转换成IP地址
  • XML - eXtensible Markup Language - 可扩展标记语言
  • JSON - JavaScript Object Notation - Java脚本的对象标记
  • JSON解析工具 - jackson/gson/fastjson

-通过网络接口查询身份证信息

class PersonInfo{
    private String address;
    @SerializedName("sex")
    private String gender;
    private String birthday;
    
    public String getAddress() {
        return address;
    }
    public void setAddress(String address) {
        this.address = address;
    }
    public String getGender() {
        return gender;
    }
    public void setGender(String sex) {
        this.gender = sex;
    }
    public String getBirthday() {
        return birthday;
    }
    public void setBirthday(String birthday) {
        this.birthday = birthday;
    }   
}

class IdCardModel{
    private int errNum;
    private String retMsg;
    private PersonInfo retData;
    
    public int getErrNum() {
        return errNum;
    }
    public void setErrNum(int errNum) {
        this.errNum = errNum;
    }
    public String getRetMsg() {
        return retMsg;
    }
    public void setRetMsg(String retMsg) {
        this.retMsg = retMsg;
    }
    public PersonInfo getRetData() {
        return retData;
    }
    public void setRetData(PersonInfo retData) {
        this.retData = retData;
    }   
}

public class Test02 {

    public static void main(String[] args) {
        URLConnection connection = null;
        try {
            //基于HTTP协议访问服务器
            
            //1.统一资源定位符对象
            //如果查询字符串中有不允许在URL中出现的字符(例如:中文)
            //这些字符需要转换成百分号编码才能使用
            URL url = new URL("http://apis.baidu.com/apistore/idservice/id?id=51082419931009597X");
            //2.基于URL对象创建连接对象
            connection = url.openConnection(); //打开连接
            //设置请求行中的请求方法
            ((HttpURLConnection) connection).setRequestMethod("GET");
            //设置请求头
            connection.setRequestProperty("apikey", "2fa58657897cc7c76740406af043b75d");
            connection.connect();  //连接服务器
            InputStream in = connection.getInputStream();
            BufferedReader br = new BufferedReader(new InputStreamReader(in, "UTF-8"));
            StringBuilder sb = new StringBuilder();
            String temp;
            while((temp = br.readLine()) != null){
                sb.append(temp);
            }
            
            //使用Gson第三方库将服务器返回的JSON格式的字符串处理成对象模型
            
            //1.通过JSonParser对象的parse()方法将字符串转成
            JsonElement element = new JsonParser().parse(sb.toString());
            
            //2.通过Gson对象的fromJson()方法将JsonElement转换成自定义类型的对象
            IdCardModel model = new Gson().fromJson(element, IdCardModel.class);
            System.out.println(model.getRetData().getAddress());
            System.out.println(model.getRetData().getGender());
            System.out.println(model.getRetData().getBirthday());
            
        } 
        catch (MalformedURLException e) {
            e.printStackTrace();
        } 
        catch (IOException e) {
            e.printStackTrace();
        }
        finally {
            if (connection != null) {
                ((HttpURLConnection)connection).disconnect();
            }
        }
    }
}

你可能感兴趣的:(Java -- 网络编程)