即拿即用-HttpURLConnection分别实现图片,文本,文件的请求

本文使用了HttpURLConnection分别实现图片,文本,文件的网络请求

效果图:
这里写图片描述

判断读写状态
        //获取读写状态
        String state = Environment.getExternalStorageState();
        //判断读写状态
        if (!Environment.MEDIA_MOUNTED.equals(state)) {
            Toast.makeText(SimpleHttpActivity.this, "无法获取SDCard", Toast.LENGTH_SHORT).show();
            return;
        }
创建路径
                //获取目录路径
                File directory = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS) + "/"+"SimpleHttp");

                //创建目录路径
                if (!directory.exists()) {
                    boolean isSuccess = directory.mkdirs();
                    Log.e(TAG, "创建文件夹路径是否成功=" + isSuccess);
                }

                //创建文件路径(/storage/emulated/0/Download/SimpleHttp/app.apk)
                File file = new File(directory + "/" + "app.apk");
                Log.e(TAG, file.getAbsolutePath());

代码:

SimpleHttpActivity

package com.bourne.httprequest;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Base64;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;


import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;


/**
 * HttpURLConnection分别实现图片,文本,文件的请求
 */
public class SimpleHttpActivity extends AppCompatActivity implements View.OnClickListener {

    private String TAG;
    private ImageView imageView;
    private TextView textView;

    private static final int IMAGE_MSG_NEW_PIC = 2;
    private static final int IMAGE_MSG_CACHE_PIC = 1;
    private static final int IMAGE_ERROR = 3;
    private static final int IMAGE_EXCEPTION = 4;

    protected static final int TEXT_SUCCESS = 5;
    protected static final int TEXT_ERROR = 6;
    protected static final int FILE_SUCCESS = 7;

    //1.在主线程里面声明消息处理器 handler
    private Handler handler = new Handler() {
        //处理消息的
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case IMAGE_MSG_CACHE_PIC:
                    //3.处理消息 运行在主线程
                    Bitmap bitmap = (Bitmap) msg.obj;
                    imageView.setImageBitmap(bitmap);
                    Toast.makeText(SimpleHttpActivity.this, "使用缓存图", Toast.LENGTH_SHORT).show();
                    break;
                case IMAGE_MSG_NEW_PIC:
                    Bitmap bitmap2 = (Bitmap) msg.obj;
                    imageView.setImageBitmap(bitmap2);
                    Toast.makeText(SimpleHttpActivity.this, "下载图片完毕", Toast.LENGTH_SHORT).show();
                    break;
                case IMAGE_ERROR:
                    Toast.makeText(SimpleHttpActivity.this, "图片请求失败", Toast.LENGTH_SHORT).show();
                    break;
                case IMAGE_EXCEPTION:
                    Toast.makeText(SimpleHttpActivity.this, "图片发生异常,请求失败", Toast.LENGTH_SHORT).show();
                    break;
                case TEXT_ERROR:
                    Toast.makeText(SimpleHttpActivity.this, "文本请求失败", Toast.LENGTH_SHORT).show();
                    break;
                case TEXT_SUCCESS:
                    String text = (String) msg.obj;
                    textView.setText(text);
                    Toast.makeText(SimpleHttpActivity.this, "文本请求成功", Toast.LENGTH_SHORT).show();
                    break;
                case FILE_SUCCESS:
                    Toast.makeText(SimpleHttpActivity.this, "文件下载完毕", Toast.LENGTH_SHORT).show();
                    break;
            }

            super.handleMessage(msg);
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_simple_http);

        TAG = this.getLocalClassName();

        findViewById(R.id.btn_downloadImage).setOnClickListener(this);
        imageView = (ImageView) findViewById(R.id.imageView);
        findViewById(R.id.btn_downloadFile).setOnClickListener(this);
        findViewById(R.id.btn_downloadText).setOnClickListener(this);
        textView = (TextView) findViewById(R.id.textView);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_downloadImage:
                downloadBitmap();
                break;
            case R.id.btn_downloadFile:
                downloadFile();
                break;
            case R.id.btn_downloadText:
                downloadText();
                break;
        }
    }

    /**
     * 下载文件
     */
    private void downloadFile() {

        //请求地址
        final String urlStr = "http://downloads.jianshu.io/apps/haruki/JianShu-2.2.3-17040111.apk";

        //获取读写状态
        String state = Environment.getExternalStorageState();
        //判断读写状态
        if (!Environment.MEDIA_MOUNTED.equals(state)) {
            Toast.makeText(SimpleHttpActivity.this, "无法获取SDCard", Toast.LENGTH_SHORT).show();
            return;
        }

        //开启下载线程
        new Thread() {
            @Override
            public void run() {
                //获取目录路径
                File directory = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS) + "/" + "SimpleHttp");

                //创建目录路径
                if (!directory.exists()) {
                    boolean isSuccess = directory.mkdirs();
                    Log.e(TAG, "创建文件夹路径是否成功=" + isSuccess);
                }

                //创建文件路径(/storage/emulated/0/Download/SimpleHttp/app.apk)
                File file = new File(directory + "/" + "app.apk");
                Log.e(TAG, file.getAbsolutePath());

                InputStream input = null;
                OutputStream output = null;
                HttpURLConnection connection = null;
                try {
                    URL url = new URL(urlStr);
                    connection = (HttpURLConnection) url.openConnection();
                    connection.setDoInput(true); //允许输入流,即允许下载
                    connection.setDoOutput(true); //允许输出流,即允许上传
                    connection.setUseCaches(false); //不使用缓冲
                    connection.setRequestMethod("GET"); //使用get请求
                    connection.connect();
                    // expect HTTP 200 OK, so we don't mistakenly save error report
                    // instead of the file
                    if (connection.getResponseCode() != HttpURLConnection.HTTP_OK) {
                        Log.e(TAG, "Server returned HTTP " + connection.getResponseCode()
                                + " " + connection.getResponseMessage());
                    }
                    // this will be useful to display download percentage
                    // might be -1: server did not report the length
                    int fileLength = connection.getContentLength();
                    // download the file
                    input = connection.getInputStream();
                    output = new FileOutputStream(file);
                    byte data[] = new byte[4096];
                    long total = 0;
                    int count;
                    while ((count = input.read(data)) != -1) {
                        // allow canceling with back button
//                        if (isCancelled()) {
//                            input.close();
//                            return null;
//                        }
                        total += count;
                        // publishing the progress....
                        if (fileLength > 0) // only if total length is known
                            Log.e(TAG, "" + (int) (total * 100 / fileLength));
                        output.write(data, 0, count);
                    }
                } catch (Exception e) {
                    Log.e(TAG, e.toString());
                } finally {
                    try {
                        if (output != null)
                            output.close();
                        if (input != null)
                            input.close();
                        Message msg = new Message();
                        msg.what = FILE_SUCCESS;
                        handler.sendMessage(msg);

                    } catch (IOException ignored) {
                    }
                    if (connection != null)
                        connection.disconnect();
                }

            }
        }.start();
    }

    /**
     * 下载图片
     */
    private void downloadBitmap() {

        final String path = "http://images.csdn.net/20170413/andr1_meitu_1.jpg";

        new Thread() {
            public void run() {

                File file = new File(getCacheDir(), Base64.encodeToString(
                        path.getBytes(), Base64.DEFAULT));

                if (file.exists() && file.length() > 0) {
                    Log.e(TAG, "图片存在,拿缓存");
                    Bitmap bitmap = BitmapFactory.decodeFile(file
                            .getAbsolutePath());

                    Message msg = new Message();//声明消息
                    msg.what = IMAGE_MSG_CACHE_PIC;
                    msg.obj = bitmap;//设置数据
                    handler.sendMessage(msg);//让handler帮我们发送数据
                } else {
                    Log.e(TAG, "图片不存在,获取数据生成缓存");
                    // 通过http请求把图片获取下来。
                    try {
                        // 1.声明访问的路径, url 网络资源 http ftp rtsp
                        URL url = new URL(path);
                        // 2.通过路径得到一个连接 http的连接
                        HttpURLConnection conn = (HttpURLConnection) url
                                .openConnection();
                        // 3.判断服务器给我们返回的状态信息。
                        // 200 成功 302 从定向 404资源没找到 5xx 服务器内部错误
                        int code = conn.getResponseCode();
                        if (code == 200) {
                            InputStream is = conn.getInputStream();// png的图片
                            FileOutputStream fos = new FileOutputStream(file);
                            byte[] buffer = new byte[1024];
                            int len = -1;
                            while ((len = is.read(buffer)) != -1) {
                                fos.write(buffer, 0, len);
                            }
                            is.close();
                            fos.close();
                            Bitmap bitmap = BitmapFactory.decodeFile(file
                                    .getAbsolutePath());
                            //更新ui ,不能写在子线程
                            Message msg = new Message();
                            msg.obj = bitmap;
                            msg.what = IMAGE_MSG_NEW_PIC;
                            handler.sendMessage(msg);

                        } else {
                            // 请求失败
                            //土司更新ui,不能写在子线程
                            //Toast.makeText(this, "请求失败", 0).show();
                            Message msg = new Message();
                            msg.what = IMAGE_ERROR;
                            handler.sendMessage(msg);
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                        //土司不能写在子线程
                        //Toast.makeText(this, "发生异常,请求失败", 0).show();
                        Message msg = new Message();
                        msg.what = IMAGE_EXCEPTION;
                        handler.sendMessage(msg);
                    }
                }
            }


        }.start();
    }

    /**
     * 下载文本
     */
    private void downloadText() {
        final String path = "http://blog.csdn.net/iromkoear";
        //访问网络,把html源文件下载下来
        new Thread() {
            public void run() {
                try {
                    URL url = new URL(path);
                    HttpURLConnection conn = (HttpURLConnection) url.openConnection();
                    conn.setRequestMethod("GET");//声明请求方式 默认get
                    //conn.setRequestProperty("User-Agent", "Mozilla/5.0 (Linux; U; Android 2.3.3; zh-cn; sdk Build/GRI34) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 Mobile Safari/533.1 MicroMessenger/6.0.0.57_r870003.501 NetType/internet");
                    int code = conn.getResponseCode();
                    if (code == 200) {
                        InputStream is = conn.getInputStream();
                        String result = readStream(is);

                        Message msg = Message.obtain();//减少消息创建的数量
                        msg.obj = result;
                        msg.what = TEXT_SUCCESS;
                        handler.sendMessage(msg);
                    }
                } catch (Exception e) {
                    Message msg = Message.obtain();//减少消息创建的数量
                    msg.what = TEXT_ERROR;
                    handler.sendMessage(msg);
                    e.printStackTrace();
                }
            }

            ;
        }.start();
    }

    /**
     * 把输入流的内容转换成字符串
     *
     * @param is
     * @return null解析失败, string读取成功
     */
    public static String readStream(InputStream is) {
        try {
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            byte[] buffer = new byte[1024];
            int len = -1;
            while ((len = is.read(buffer)) != -1) {
                baos.write(buffer, 0, len);
            }
            is.close();
            String temptext = new String(baos.toByteArray());
            if (temptext.contains("charset=gb2312")) {//解析meta标签
                return new String(baos.toByteArray(), "gb2312");
            } else {
                return new String(baos.toByteArray(), "utf-8");
            }
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }
}

布局文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"

    android:layout_height="match_parent"
    >

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:gravity="center_horizontal"
            android:orientation="vertical">

            <Button
                android:id="@+id/btn_downloadImage"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="下载图片"
                />

            <ImageView
                android:id="@+id/imageView"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"/>

            <Button
                android:id="@+id/btn_downloadFile"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="下载文件"
                />

            <Button
                android:id="@+id/btn_downloadText"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="下载文字"
                />

            <TextView
                android:id="@+id/textView"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"/>

        </LinearLayout>

    </ScrollView>
</LinearLayout>

AndroidManifest

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
          package="com.bourne.httprequest">

    <!-- 网络请求权限 -->
    <uses-permission android:name="android.permission.INTERNET"/>
    <!-- 在SDCard中创建与删除文件权限 -->
    <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>
    <!-- 往SDCard写入数据权限 -->
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    <!-- 往SDCard读取数据权限 -->
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>

                <category android:name="android.intent.category.LAUNCHER"/>
            </intent-filter>
        </activity>
        <activity android:name=".SimpleHttpActivity">
        </activity>
    </application>

</manifest>

GitHub链接:https://github.com/mocn26169/HttpRequest-Demo

参考

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值