zoukankan      html  css  js  c++  java
  • 使用HttpUrlConnection连接网络的例程

      new Thread(new Runnable() {
       @Override
       public void run() {
        String urlString = "http://www....";
        HttpURLConnection conn = null;
        try {
         URL url = new URL(urlString);
         conn = (HttpURLConnection) url.openConnection();
         conn.setRequestMethod("GET");
         conn.setConnectTimeout(8000);
         conn.setReadTimeout(8000);
         InputStream in = conn.getInputStream();
         // 下面对获得的输入流进行读取
         BufferedReader reader = new BufferedReader(
           new InputStreamReader(in));
         StringBuilder sb = new StringBuilder();
         String line;
         while ((line = reader.readLine()) != null) {
          sb.append(line);
         }
         Message msg = new Message();
         msg.what = 1;
         msg.obj = sb.toString();
         handler.sendMessage(msg);
        } catch (Exception e) {
         e.printStackTrace();
        } finally {
         if (conn != null)
          conn.disconnect();
        }
       }// run方法结束
      });// 线程结束
  • 相关阅读:
    mongo dump
    http请求
    DT-06 For AT
    DT-06 For Homekit
    DT-06 For MQTT
    利用DoHome APP和音箱控制小车的实验参考步骤
    利用DoHome APP和音箱控制LED灯实验参考步骤
    利用DoHome APP和音箱控制继电器通断电实验参考步骤
    HTML5学习笔记1
    HTML5学习第四天
  • 原文地址:https://www.cnblogs.com/maxma/p/9169620.html
Copyright © 2011-2022 走看看