• Android(java)学习笔记144:网络图片浏览器的实现(ANR)


    1.我们在Android下,实现使用http协议进行网络通信,请求网络数据。这里是获取网络上的图片信息,让它可以显示在手机上;

    但是我们这个手机连接网络是很费时间,如果我们在主线程(UI线程)中写这个网络连接的逻辑,这是很容易报一个错误:android.os.NetworkOnMainThreadException(Android4.0之后引入的异常)

    主线程很重要,它负责监听系统的各种事件,如果主线程在一段时间内没有响应,系统就会这个应用程序无响应,就会产生ANR的异常。下面有必要说明一下这个ANR异常

    2.ANRs ("Application Not Responding"),意思是"应用没有响应".

    在如下情况下,Android会报出ANR错误:
    •主线程 ("事件处理线程"/ "UI线程") 在5秒内没有响应输入事件.
    • BroadcastReceiver 没有在10秒内完成返回.

    通常情况下,下面这些做法会导致ANR:
    (1).在主线程内进行网络操作
    (2).在主线程内进行一些缓慢的磁盘操作(例如执行没有优化过的SQL查询)

    应用应该在5秒或者10秒内响应,否则用户会觉得“这个应用很垃圾”“烂”“慢”…等等

    逻辑应该是:
    (1). new出一个新的线程,进行数据请求.
    (2). 获取数据后,调用handler.sendMessage方法.

    (3). 在handler的handle()方法中更新UI.

    案例:

    activity_main.xml

     1 <RelativeLayout 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=".MainActivity" >
     6 
     7     <Button
     8         android:onClick="click"
     9         android:layout_width="wrap_content"
    10         android:layout_height="wrap_content"
    11         android:layout_centerHorizontal="true"
    12         android:layout_centerVertical="true"
    13         android:text="点我" />
    14 
    15 </RelativeLayout>

    MainActivity.java

    package com.itheima.anr;
    
    import android.app.Activity;
    import android.os.Bundle;
    import android.view.View;
    
    public class MainActivity extends Activity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
        }
    
        public void click(View view){
            try {
                System.out.println(Thread.currentThread().getName());
                Thread.sleep(6000);//当前线程。
                
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        
    }

    这里的Thread.sleep(6000),这里的表示Thread线程是Main线程,也就是UI主线程,我们之前说过了UI主线程在5s不能响应,系统就会认为这个应用程序是不响应的。这里6s>5s,所以会报错ANRs ("Application Not Responding")

    3.前面说了这么多,也就是说了关于在Android下进行网络连接注意事项,网络连接是耗时程序代码:下面开始实现网络图片浏览器程序:

    MainActivity.java:

    package com.itheima.netimageviewer;
    
    import java.io.File;
    import java.io.FileOutputStream;
    import java.io.InputStream;
    import java.net.HttpURLConnection;
    import java.net.URL;
    
    import android.app.Activity;
    import android.graphics.Bitmap;
    import android.graphics.BitmapFactory;
    import android.os.Bundle;
    import android.text.TextUtils;
    import android.util.Base64;
    import android.view.View;
    import android.widget.EditText;
    import android.widget.ImageView;
    import android.widget.Toast;
    
    public class MainActivity extends Activity {
        private EditText et_path;
        private ImageView iv;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            et_path = (EditText) findViewById(R.id.et_path);
            iv = (ImageView) findViewById(R.id.iv);
        }
    
        /**
         * 点击查看网络上的图片
         * 
         * @param view
         */
        public void click(View view) {
            String path = et_path.getText().toString().trim();// http://www.baidu.com/aa.png
            if (TextUtils.isEmpty(path)) {
                Toast.makeText(this, "图片路径不能为空", 0).show();
                return;
            }
            //把文件名编码之后+缓冲的路径变成file文件
            File file = new File(getCacheDir(), Base64.encodeToString(
                    path.getBytes(), Base64.DEFAULT));
            if (file.exists() && file.length() > 0) {
                System.out.println("图片存在,拿缓存");
                Bitmap bitmap = BitmapFactory.decodeFile(file
                        .getAbsolutePath());
                iv.setImageBitmap(bitmap);
            } else {
                System.out.println("图片不存在,获取数据生成缓存");
                // 通过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());
                        iv.setImageBitmap(bitmap);
                    } else {
                        // 请求失败
                        Toast.makeText(this, "请求失败", 0).show();
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                    Toast.makeText(this, "发生异常,请求失败", 0).show();
                }
            }
        }
    }

    activity_main.xml:

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        tools:context=".MainActivity" >
    
        <EditText
            android:text="http://www.baidu.com/img/bd_logo1.png"
            android:id="@+id/et_path"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="请输入网络图片的路径" />
    
        <Button
            android:onClick="click"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="查看" />
    
        <ImageView
            android:id="@+id/iv"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    
    </LinearLayout>

    AndroidManifest.xml:

    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="com.himi.webpicturewatch"
        android:versionCode="1"
        android:versionName="1.0" >
        <uses-permission android:name="android.permission.INTERNET"/>
        <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
    
        <uses-sdk
            android:minSdkVersion="15"
            android:targetSdkVersion="17" />
    
        <application
            android:allowBackup="true"
            android:icon="@drawable/ic_launcher"
            android:label="@string/app_name"
            android:theme="@style/AppTheme" >
            <activity
                android:name=".MainActivity"
                android:label="@string/app_name" >
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />
    
                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
            </activity>
        </application>
    
    </manifest>
  • 相关阅读:
    面向过程编程
    生成器
    迭代器
    装饰器
    函数对象与闭包
    名称空间和作用域
    Django中的as_view方法源码分析
    DRF3序列化反序列化
    DRF4级联与外键字段
    django--BBS项目,后端业务逻辑整理
  • 原文地址:https://www.cnblogs.com/hebao0514/p/4767538.html
Copyright © 2020-2023  润新知