黎活明8天快速掌握android视频教程23_网络通信之网络图片查看器

1、首先新建立一个java web项目的工程。使用的是myeclipe开发软件

图片的下载路径是http://192.168.1.103:8080/lihuoming_23/3.png  当前手机和电脑在同一个局域网范围内

2 、Android项目的工程如下

整个工程采用MVC模式:

1、controller是控制层、包括activity 、fragment 、adapter、broadcast、service

2、bussiess是业务层,主要负责具体的业务操作,例如从后台下载下载图片这就是一个具体的业务操作,业务操作的时候,最后不要对业务操作过程中产生的异常进行处理,应该将异常抛出去到控制层,由控制层对异常进行处理,控制层如果收到了异常,说明该业务失败,控制层在做出相应的toast提示,或者提示用于做出相应的操作。

这里最好的操作是:定义一个业务操作接口,然后在写一个业务的实现类,controller只和业务类打交道

3、模型层:主要是对数据进行操作、包括javabean对象、db dao

4、utils:工具类

2 Android studio工程

1、xml文件

<?xml version="1.0" encoding="utf-8"?>
<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=".controller.activity.MainActivity">

    <TextView
        android:textSize="25sp"
        android:text="从网络获得下载的图片"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    <Button
        android:id="@+id/btn_main_download"
        android:textSize="25sp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="点击下载"/>
    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/iv_main_show"/>

</LinearLayout>

2 业务操作类

按照上面的规范最好写出:

先定义一个业务操作类的接口

public interface ImageBussiess  {

public static byte[] downLoadImage(String path) throws Exception
}

然后定义业务的实现类

/**
 * Created by Administrator on 2017/4/17.
 * 下载图片的业务操作类,业务层不要try catch异常
 * 应该将异常抛出去,由控制层activity来进行处理和显示
 *
 */
public class ImageBussiessImp  Implement  ImageBussiess{
    public static byte[] downLoadImage(String path) throws IOException {
        URL url = new URL(path);
        HttpURLConnection openConnection = (HttpURLConnection) url.openConnection();
        openConnection.setConnectTimeout(5000);
        openConnection.setRequestMethod("GET"); //采用get的请求方式
        openConnection.connect();
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        InputStream inputStream = null;
        if(openConnection.getResponseCode() == 200){
           inputStream = openConnection.getInputStream();
            byte[] buffer = new byte[1024];

            int len = -1;
            while ((len = inputStream.read(buffer)) != -1){
                outputStream.write(buffer,0,len);
            }
        }
        inputStream.close();
        return  outputStream.toByteArray();
    }
}

4 activity

public class MainActivity extends Activity {

    private ImageView iv_main_show;
    private Button btn_main_download;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        initListener();
    }

    private void initListener() {
        btn_main_download.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ExecutorService executorService = Executors.newCachedThreadPool(); // 网络下载都必须在子线程中进行,这里使用的是线程池的方式开启线程
                executorService.execute(new Runnable() {
                    @Override
                    public void run() {
                        String path = "http://192.168.1.103:8080/lihuoming_23/3.png";//myeclpise建立的工程
                        try {
                            byte[] datas = ImageBussiess.downLoadImage(path);
                            final Bitmap bitmap = BitmapFactory.decodeByteArray(datas, 0, 
                                    datas.length);
                            runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    iv_main_show.setImageBitmap(bitmap);//界面的显示必须在主线程中,runOnUiThread就是在线程中更新界面的显示
                                }
                            });

                        } catch (final IOException e) {
                            e.printStackTrace();
                            runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    Toast.makeText(MainActivity.this, "图片下载失败" + e.toString(),
                                            Toast.LENGTH_LONG).show();
                                }
                            });
                            ;
                        }
                    }
                });


            }
        });
    }

    private void initView() {
        btn_main_download = (Button) findViewById(R.id.btn_main_download);
        iv_main_show = (ImageView) findViewById(R.id.iv_main_show);
    }
}

原文地址:https://www.cnblogs.com/kebibuluan/p/6755482.html