hyt_ao 2016-02-17 14:18 采纳率: 41.2%
浏览 1390
已采纳

这两段代码为什么前者可执行,后者不行,若后者不放在run方法里是可执行的

前者:
// 创建子线程
new Thread() {
public void run() {
try {
// 获取源码路径
String path = et_enter.getText().toString().trim();
// 将路径转换为URL
URL url = new URL(path);
// 利用URL对象得到httpURLconnection对象 用于发送或接收数据
HttpURLConnection conn = (HttpURLConnection) url
.openConnection();
// 发送get请求,以得到网页源代码
conn.setRequestMethod("GET");// 必须大写
// 设置请求超时时间
conn.setConnectTimeout(5000);
// 获取服务器返回的状态码
int code = conn.getResponseCode();
// 判断是否请求成功
if (code == 200) {
InputStream in = conn.getInputStream();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
int len = -1;
byte[] buffer = new byte[1024];
while ((len = in.read(buffer)) != -1) {
baos.write(buffer, 0, len);
}
String content = new String(baos.toByteArray());

                    // 创建message对象用于存取content
                    Message msg = new Message();
                    msg.obj = content;

                    // 调用sendMessage方法发送消息
                    hander.sendMessage(msg);

                    // tv_display.setText(content);
                }

            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        };
    };

后者:
// 创建子线程
new Thread(){
public void run() {

            try {
                // 获取源码路径
                String path = et_enter.getText().toString().trim();
                // 将路径转换为URL
                URL url = new URL(path);
                // 利用URL对象得到httpURLconnection对象 用于发送或接收数据
                HttpURLConnection conn = (HttpURLConnection) url
                        .openConnection();
                // 发送get请求,以得到网页源代码
                conn.setRequestMethod("GET");// 必须大写
                // 设置请求超时时间
                conn.setConnectTimeout(5000);
                // 获取服务器返回的状态码
                int code = conn.getResponseCode();
                // 判断是否请求成功
                if (code == 200) {
                    InputStream in = conn.getInputStream();

                    //通过BitmapFactory获取bitmap
                    Bitmap bitmap = BitmapFactory.decodeStream(in);

                    // 创建message对象用于存取content
                    Message msg = Message.obtain();
                    msg.obj = bitmap;

                    // 调用sendMessage方法发送消息
                    hander.sendMessage(msg);

                }

            } catch (Exception e) {
                e.printStackTrace();
            }
        };
    };
  • 写回答

3条回答 默认 最新

  • 小菜陈 2016-02-18 01:19
    关注

    http://www.cnblogs.com/yourancao520/archive/2012/06/25/2561367.html这里有篇文章和你类似,可以看看,他的是将
    Bitmap bitmap = BitmapFactory.decodeStream(in)改成了Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 maple软件,用solve求反函数出现rootof,怎么办?
  • ¥50 汇编语言除法溢出问题
  • ¥65 C++实现删除N个数据列表共有的元素
  • ¥15 Visual Studio问题
  • ¥15 state显示变量是字符串形式,但是仍然红色,无法引用,并显示类型不匹配
  • ¥20 求一个html代码,有偿
  • ¥100 关于使用MATLAB中copularnd函数的问题
  • ¥20 在虚拟机的pycharm上
  • ¥15 jupyterthemes 设置完毕后没有效果
  • ¥15 matlab图像高斯低通滤波