前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >GetAPictureFromInternet网络编程

GetAPictureFromInternet网络编程

作者头像
提莫队长
发布2019-02-21 15:41:54
3350
发布2019-02-21 15:41:54
举报
文章被收录于专栏:刘晓杰刘晓杰

这是《Android 4.0网络编程详解》书上的一个例子,可能是由于版本的原因吧,报了很多错。 新建android工程GetAPictureFromInternet 布局文件main.xml

代码语言:javascript
复制
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/LinearLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.example.getapicturefrominternet.MainActivity" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="match_parent"
        android:layout_height="50sp"
        android:text="图片路径:"
        android:textSize="40sp" />

    <EditText
        android:id="@+id/path"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@android:drawable/edit_text"
        android:text="http://10.107.56.64:8080/ServerForPicture/girl.jpg" >

        <!-- 不要用localhost,localhost表示android虚拟机的本机地址,我们要访问的是自己电脑的ip -->

        <requestFocus />
    </EditText>

    <Button
        android:id="@+id/button1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="查看" />

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</LinearLayout>
这里写图片描述
这里写图片描述

然后是新建服务器 File—>New–>Other–>Server–>Tomcat v7.0 Server–>Finish

新建web项目 File–>New–>Project–>Dynamic WebProject。一路next,最后

这里写图片描述
这里写图片描述

勾选Generate XXX选项

右键新建的web项目,Run As–>Run On Server.补全地址就会有如下的画面

这里写图片描述
这里写图片描述

至此,服务器搭建完成

接下来是android工程的编码 MainActivity.java

代码语言:javascript
复制
package com.example.getapicturefrominternet;

import android.app.Activity;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;

public class MainActivity extends Activity {
    private EditText path;
    private ImageView image;
    private Button button;
    private Bitmap bitmap;
    private Handler myHandler = new Handler(){
        public void handleMessage(Message msg) {
            if(msg.what == 0){
                image.setImageBitmap(bitmap);
            }
        };
    };

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

        path = (EditText)findViewById(R.id.path);
        image = (ImageView)findViewById(R.id.imageView1);
        button = (Button)findViewById(R.id.button1);
        button.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                final String pathString = path.getText().toString();
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        bitmap = ImageService.getBitmap(pathString);
                        myHandler.sendEmptyMessage(0);
                    }
                }).start();
            }
        });
    }
}

ImageService.java

代码语言:javascript
复制
package com.example.getapicturefrominternet;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;

public class ImageService {
    public static Bitmap getBitmap(String path) {
        Bitmap bitmap = null;
        try {
            URL url = new URL(path);
            HttpURLConnection connection = (HttpURLConnection) url
                    .openConnection();
            connection.setConnectTimeout(500);
            connection.setRequestMethod("GET");
            if (connection.getResponseCode() == 200) {
                InputStream inputStream = connection.getInputStream();
                byte[] buffer = new byte[1024];
                int len = -1;
                ByteArrayOutputStream bos = new ByteArrayOutputStream();
                while ((len = inputStream.read(buffer)) != -1) {
                    bos.write(buffer, 0, len);
                }
                byte[] data = bos.toByteArray();
                bos.close();
                bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
            }
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return bitmap;
    }
}

书上的代码有问题,按照书上的敲运行一直出错。第一,网络处理函数没有开线程;第二,ImageService的实现也有问题。调试了一大会儿

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2016年04月06日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档