Android文件下载 HttpURLConnection
来源:互联网 发布:php软件 编辑:程序博客网 时间:2024/05/18 01:51
步骤:通过new URL(目标网址字符串)得到URL对象url---->调用url的openConnection方法获得一个HttpURLConnection对象connection---->调用connection的getInputStream方法获得一个InputStream对象,访问结束,接下来就是io操作了。
实例:
16 public class DownloadDemoActivity extends Activity {
17 /** Called when the activity is first created. */
18 TextView textView;
19
20 Handler handler = new Handler(){
21
22 @Override
23 public void handleMessage(Message msg)
24 {
25 textView.setText((String)msg.obj);
26 }
27
28 };
29
30
31 @Override
32 public void onCreate(Bundle savedInstanceState) {
33 super.onCreate(savedInstanceState);
34 setContentView(R.layout.main);
35
36 textView = (TextView) findViewById(R.id.textView1);
37
38 }
39
40 public void buttonClick(View view)
41 {
42 new Thread(){
43
44 @Override
45 public void run()
46 {
47 StringBuffer stringBuffer = new StringBuffer();
48 HttpURLConnection connection = null;
49
50 try
51 {
52 URL url = new URL("http://www.baidu.com");
53 connection =
54 (HttpURLConnection) url.openConnection();
55
56 BufferedReader bReader = new BufferedReader
57 (new InputStreamReader(connection.getInputStream())) ;
58 String str;
59
60 while((str = bReader.readLine() ) != null)
61 {
62 stringBuffer.append(str);
63 }
64
65 }
66 catch (Exception e)
67 {
68 // TODO Auto-generated catch block
69 e.printStackTrace();
70 }finally{
71 connection.disconnect();
72 }
73
74 Message msg = handler.obtainMessage();
75 msg.obj = stringBuffer.toString();
76 msg.sendToTarget();
77
78 }
79
80
81
82 }.start();
83 }
84 }
- Android文件下载 HttpURLConnection
- android 文件下载详解(HttpURLConnection)
- Android学习之使用HttpURLConnection下载文件
- 使用HttpURLConnection下载文件
- 使用HttpURLConnection下载文件
- HttpUrlConnection实现文件下载
- 使用HttpURLConnection下载文件
- HttpURLConnection实现文件下载
- android HttpURLConnection 实现下载
- Android 使用HttpURLConnection 下载
- Android HttpURLConnection 多线程下载
- android多线程下载-HttpURLConnection
- Android HttpURLConnection 下载xml文件时候 出现ioexception
- Android - HttpURLConnection下载网络文件,加载网络图片
- 八.使用HttpURLConnection下载文件
- 使用HttpURLConnection实现文件下载
- HttpUrlConnection、HttpClient下载文件实例
- 使用HttpUrlConnection多线程下载文件
- 听谭浩强教授讲座有感
- Android自定义action与permission!!!
- ASCII码表(美国标准信息交换标准码)
- 分析对象所占储存空间大小 我还看不懂···
- STL概述
- Android文件下载 HttpURLConnection
- hibernate多对多例子
- linux 批量删除及修改文件shell命令
- 敏捷开发智慧敏捷系列之二:写不写文档?
- 视频编码中的PAFF和MBAFF的区别 转自:http://blog.csdn.net/kerryhung/article/details/4433256
- 使用spring MVC框架进行文件上传
- android学习笔记一(基础部分)
- 快速学习AJAX之六 Ajax实现无刷新上传头像
- 如何判断 linux内核 中 如何判断 条件编译