Android文件下载 HttpURLConnection
2011-11-01 17:24
357 查看
步骤:通过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 }
实例:
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)
- android 使用AsyncHttpClient框架上传文件以及使用HttpURLConnection下载文件
- android 使用AsyncHttpClient框架上传文件以及使用HttpURLConnection下载文件
- Android - HttpURLConnection下载网络文件,加载网络图片
- android开发之HttpUrlConnection与OkHttp实现文件上传下载
- Android_HttpURLConnection下载文件
- Android2.2以上的版本HttpURLConnection.getContentLength()获取的size跟下载下来的file的legth不相等
- HttpURLConnection 下载文件
- android多线程暂停下载-HttpURLConnection
- Android HttpURLConection下载文件乱码以及adb shell 显示中文乱码
- 【Android进阶】使用HttpURLConnection实现图片的下载与现显示
- 使用HttpURLConnection下载文件时出现 java.io.FileNotFoundException彻底解决办法
- 用HttpUrlConnection来下载文件信息
- Android 网络应用重点———使用HttpURLConnection 上传文件
- 下载网络文件HttpURLConnection.getContentLength()大小为 -1
- Android Http协议笔记(使用HttpURLConnection)文件上传+参数
- android使用HttpURLConnection实现带参数文件上传
- Android中如何使用HttpURLConnection实现GET POST JSON数据与下载图片