zoukankan      html  css  js  c++  java
  • Android中使用URL从网络上获取data

    从一个http server获取一些文本,大小应该是小于1KB,下载用不了0.1秒,但是使用URL的openStream()获取这个数据的inputstream要花费5~10秒,代码如下:

     1 try {
     2    System.out.println("begin get url");
     3    URL url = new URL(murl);
     4    System.out.println("get url success");
     5    is = url.openStream();
     6    System.out.println("get inputstream success");
     7   } catch (MalformedURLException e) {
     8    // TODO Auto-generated catch block
     9    e.printStackTrace();
    10   } catch (IOException e) {
    11    // TODO Auto-generated catch block
    12    e.printStackTrace();
    13   }

     解决方法:使用如下方法可以代替,整体延时不超过1秒

     1   HttpGet httpGet = new HttpGet(murl);
     2   HttpClient httpclient = new DefaultHttpClient();
     3   // Execute HTTP Get Request
     4   HttpResponse response = null;
     5   try {
     6    response = httpclient.execute(httpGet);
     7   } catch (ClientProtocolException e) {
     8    // TODO Auto-generated catch block
     9    e.printStackTrace();
    10   } catch (IOException e) {
    11    // TODO Auto-generated catch block
    12    e.printStackTrace();
    13   }
    14         try {
    15    InputStream is = response.getEntity().getContent();
    16   } catch (IllegalStateException e) {
    17    // TODO Auto-generated catch block
    18    e.printStackTrace();
    19   } catch (IOException e) {
    20    // TODO Auto-generated catch block
    21    e.printStackTrace();
    22   }
  • 相关阅读:
    24. orcle创建备份表
    23. oralce11g导出dmp然后导入Oracle10g
    模板
    日记集
    Outlook 2013 电子邮件账户设置备份与恢复
    Firefox恢复书签
    Windows 7 Professional安装多语言包
    CentOS下成功修复了Windows的grub引导
    CentOS 7修复MBR和GRUB
    从移动硬盘开机,引导VHD(Win10)
  • 原文地址:https://www.cnblogs.com/maliqian/p/2506681.html
Copyright © 2011-2022 走看看