android中的HttpUrlConnection的使用之二

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了android中的HttpUrlConnection的使用之二相关的知识,希望对你有一定的参考价值。

httpUrlConnection主要用于网络传输当中,前面已经提及到了使用HttpUrlConnection来加载一个网站,这里我记录一下:用它在网络上下载一张图片并且加载到imageview当中。我们需要注意的是:当前很多网站上的图片传输的模式主要分两种:1.一是加密传输,使用HttpsUrlConnection进行链接;2.而是非加密传输,使用HttpUrlConnection来传输。代码如下(非加密传输):

xml代码

 1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 2     xmlns:tools="http://schemas.android.com/tools"
 3     android:layout_width="match_parent"
 4     android:layout_height="match_parent"
 5     tools:context="com.example.android_http2.MainActivity" >
 6     <ImageView 
 7         android:layout_gravity="center"
 8         android:id="@+id/imageview"
 9         android:layout_width="wrap_content"
10         android:layout_height="wrap_content"
11         />
12 
13 </LinearLayout>

Java代码

首先创建了一个MainActivity类,来对ui进行更新的操作。

 1 package com.example.android_http2;
 2 
 3 import android.app.Activity;
 4 import android.os.Bundle;
 5 import android.os.Handler;
 6 import android.widget.ImageView;
 7 
 8 public class MainActivity extends Activity {
 9     private ImageView imageview = null;
10     private Handler handler = new Handler();
11     @Override
12     protected void onCreate(Bundle savedInstanceState) {
13         super.onCreate(savedInstanceState);
14         setContentView(R.layout.activity_main);
15         imageview = (ImageView) findViewById(R.id.imageview);
16         new HttpThread("http://img.mp.itc.cn/upload/20160416/5cbd639a2bbf41ffa68147df01254444_th.jpg", imageview, handler).start();
17     }
18 
19 }

其次,创建一个线程类,来进行异步下载的功能。将网络上的代码先下载到本地上去,然后在用handler来通知主线程更新ui。

  1 package com.example.android_http2;
  2 
  3 import java.io.File;
  4 import java.io.FileNotFoundException;
  5 import java.io.FileOutputStream;
  6 import java.io.IOException;
  7 import java.io.InputStream;
  8 import java.net.HttpURLConnection;
  9 import java.net.MalformedURLException;
 10 import java.net.URL;
 11 
 12 import javax.net.ssl.HttpsURLConnection;
 13 
 14 import android.graphics.Bitmap;
 15 import android.graphics.BitmapFactory;
 16 import android.os.Environment;
 17 import android.os.Handler;
 18 import android.util.Log;
 19 import android.widget.ImageView;
 20 
 21 public class HttpThread extends Thread{
 22     private String url = null;
 23     private ImageView imageview = null;
 24     private Handler handler = null;
 25     public HttpThread (String url, ImageView imageview, Handler handler)
 26     {
 27         this.handler = handler;
 28         this.imageview = imageview;
 29         this.url = url;
 30     }
 31     public void run() {
 32         HttpURLConnection httpurlconnection = null;
 33         try {
 34             URL url = new URL(this.url);
 35             httpurlconnection = (HttpURLConnection) url.openConnection();
 36         } catch (MalformedURLException e) {
 37             // TODO Auto-generated catch block
 38             e.printStackTrace();
 39         } catch (IOException e) {
 40             // TODO Auto-generated catch block
 41             e.printStackTrace();
 42         }
 43         InputStream in = null;
 44         try {
 45             httpurlconnection.setReadTimeout(5000);
 46             //允许获得输入流
 47             httpurlconnection.setDoInput(true);
 48             in = httpurlconnection.getInputStream();
 49         } catch (IOException e1) {
 50             // TODO Auto-generated catch block
 51             e1.printStackTrace();
 52         }
 53         FileOutputStream out = null;
 54         File downfile = null;
 55         //判断当前的内存卡是否可以用
 56         Log.i("main", "我进来没1?");
 57         File file = null;
 58         if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED))
 59         {
 60             Log.i("main", "我进来没?");
 61             file = new File(Environment.getExternalStorageDirectory().toString() + File.separator + "imageviewdown" + File.separator + "image.jpg");
 62             if(!file.getParentFile().exists())
 63             {
 64                 file.getParentFile().mkdirs();
 65                 
 66             }
 67             else if(!file.exists())
 68             {
 69                 Log.i("main", "我进来没?  62");
 70                 try {
 71                     file.createNewFile();
 72                 } catch (IOException e) {
 73                     // TODO Auto-generated catch block
 74                     e.printStackTrace();
 75                 }
 76             }
 77             try {
 78                 out = new FileOutputStream(file);
 79             } catch (FileNotFoundException e) {
 80                 Log.i("main", "异常没?");
 81                 e.printStackTrace();
 82             }
 83         }
 84         byte buff[] = new byte[2 * 1024];
 85         int len = 0;
 86         try {
 87             while((len = in.read(buff)) != -1)
 88             {
 89                 out.write(buff, 0,len);
 90             }
 91         } catch (IOException e) {
 92             // TODO Auto-generated catch block
 93             e.printStackTrace();
 94         }
 95         //将下载在本地的图片加载在一个bitmap当中来
 96         final Bitmap bitmap = BitmapFactory.decodeFile(file.getAbsolutePath());
 97         Log.i("main", file.getAbsolutePath());
 98         //通知主线程来更新ui
 99         handler.post(new Runnable() {
100             public void run() {
101                 imageview.setImageBitmap(bitmap);
102             }
103         });
104     }
105 
106 }

 

以上是关于android中的HttpUrlConnection的使用之二的主要内容,如果未能解决你的问题,请参考以下文章

Android中"get","post"请求的其中三种常用数据提交方式

JAVA多线程下载

OkHttp框架从入门到放弃,解析图片使用Picasso裁剪,二次封装OkHttpUtils,Post提交表单数据

java抓取网页数据

用IO流发送Http请求

Java通过代理server上网