Android StudioHttp协议GET连接

代码内容

package com.example.day13;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Handler;
import android.os.HandlerThread;
import android.os.Looper;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
     
    ImageView imageView;
    private HandlerThread handlerThread;
    private HttpHandler handler;

    private final int DOWNLOAD = 1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
     
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        imageView = findViewById(R.id.img);
        findViewById(R.id.downLoad).setOnClickListener(this);
        handlerThread = new HandlerThread("handlerThread");
        handlerThread.start();
        //让handler消息运行在子线程
        handler = new HttpHandler(handlerThread.getLooper());
    }

    @Override
    public void onClick(View v) {
     
        switch (v.getId()){
     
            case R.id.downLoad:
                handler.sendEmptyMessage(DOWNLOAD);
                break;
        }
    }

    private void downloadFile() {
     
        String downLoadUrl = "http://i0.hdslb.com/bfs/article/535c9cc9cde7dc2f858fe99aecaf921285f71786.jpg";
        String savePath = "/sdcard/flower.jpg";

        File file = new File(savePath);
        if (file.exists()){
     
            file.delete();
        }
        try {
     
            URL url = new URL(downLoadUrl);
            HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
            urlConnection.setRequestMethod("GET");
            urlConnection.setConnectTimeout(5000);
            urlConnection.setReadTimeout(5000);
            urlConnection.setDoInput(true);
            urlConnection.connect();

            if (urlConnection.getResponseCode() == 200){
     
                InputStream in = urlConnection.getInputStream();
                BufferedInputStream bis = new BufferedInputStream(in);
                FileOutputStream fos = new FileOutputStream(savePath);
                byte[] bytes = new byte[1024];
                int len = 0;
                while ((len = bis.read(bytes))!=-1){
     
                    fos.write(bytes,0,len);
                }
                //强制把数据写入磁盘
                fos.flush();
                bis.close();
                fos.close();
            }
            Bitmap bitmap = BitmapFactory.decodeFile(savePath);
            imageView.setImageBitmap(bitmap);
        } catch (Exception e) {
     
            e.printStackTrace();
        }
    }

    public class HttpHandler extends Handler{
     
        public HttpHandler(Looper looper) {
     
            super(looper);
        }

        @Override
        public void handleMessage(Message msg) {
     
            super.handleMessage(msg);
            switch (msg.what){
     
                case DOWNLOAD:
                    downloadFile();
                    break;
            }
        }
    }
}

你可能感兴趣的:(Android StudioHttp协议GET连接)