zoukankan      html  css  js  c++  java
  • 自己写http获取网络资源和解析json数据

    虽然github上有很多开源的,方便的jar报,用起来也很方便,但我们也需要了解其中的原理,如何自己不用第三方jar包来获取网络资源

    主要代码如下:  因为联网是耗时的操作,所以需要另开一个线程来执行

     1 new Thread(){
     2             public void run() {
     3                 try {
     4                     
     5                     //首先声明url连接对象
     6                     URL url=new URL(path);
     7                     //获取HttpURLConnection对象
     8                     HttpURLConnection connection = (HttpURLConnection) url.openConnection();
     9 //                    connection.setReadTimeout(5000);
    10                     //设置连接超时时间,毫秒为单位
    11                     connection.setConnectTimeout(5000);
    12                     
    13                     //http方式
    14                     connection.setRequestMethod("GET");
    15                     
    16                     //设置http头属性
    17                     connection.setRequestProperty("apikey",  "0a37fe84ecb7c6fe98ca3e8ba48b5f24");
    18                     //获取返回码//200为正常      404 找不到资源
    19                     int code = connection.getResponseCode();
    20                     if(code==200){
    21                         
    22                         //获取字节流
    23                         InputStream inputStream = connection.getInputStream();
    24                         //解析字节流
    25                         BufferedReader bf=new BufferedReader(new InputStreamReader(inputStream, "utf-8"));    
    26                         StringBuilder sb=new StringBuilder();
    27                         String s=null;
    28                         while ((s=bf.readLine())!=null) {
    29                             sb.append(s+"
    ");
    30                         }
    31                         
    32                         //解析json对象
    33                         JSONObject jsonObject = new JSONObject(sb.toString());
    34                         JSONArray jsonArray = jsonObject.getJSONArray("newslist");
    35                         for (int i = 0; i < jsonArray.length(); i++) {
    36                             news n= new news();
    37                             JSONObject jsonObject2 = jsonArray.getJSONObject(i);
    38                             n.setTime(jsonObject2.getString("ctime"));
    39                             n.setTitle(jsonObject2.getString("title"));
    40                             n.setDescription(jsonObject2.getString("description"));
    41                             n.setPicUrl(jsonObject2.getString("picUrl"));
    42                             n.setUrl(jsonObject2.getString("url"));
    43                             list.add(n);
    44                         }
    45                         
    46                     }
    47                 } catch (Exception e) {
    48                     e.printStackTrace();
    49                 }
    50             };
    51             
    52         }.start();
  • 相关阅读:
    Redis提供的持久化机制(RDB和AOF)
    linux创建子进程--fork()方法
    数据库-锁的实践
    nginx中,$request_uri和$uri的区别
    journal size
    目的:将两个三T的硬盘做成LVM(sdc,sdd)
    安装 rbbitMQ redis mongo的三个扩展
    MySQL server has gone away
    mysql创建utf-8字符集数据库
    Linux下杀毒软件clamav的安装和使用
  • 原文地址:https://www.cnblogs.com/xujingyang/p/6007335.html
Copyright © 2011-2022 走看看