• 自己写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();
  • 相关阅读:
    linux的软连接和硬连接
    各种Python简单功能代码
    《财报就像一本故事书》刘顺仁(二) ——财务报表
    Atitit .h5文件上传 v3
    Atitti. 语法树AST、后缀表达式、DAG、三地址代码
    Atitit.在线充值功能的设计
    Atitit。数据库 安全性 重要敏感数据加密存储解决方案
    atitit.数据验证db数据库数据验证约束
    Atitit.提升电子商务安全性 在线充值功能安全方面的设计
    Atitit.antlr实现词法分析
  • 原文地址:https://www.cnblogs.com/xujingyang/p/6007335.html
Copyright © 2020-2023  润新知