黎活明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);
    }
}
复制代码

 

 


posted on   luzhouxiaoshuai  阅读(159)  评论(0编辑  收藏  举报

编辑推荐:
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

统计

点击右上角即可分享
微信分享提示