1.必须要开子线程来操作耗时操作,android.os.NetworkOnMainThreadException

new Thread(new Runnable() {

			@Override
public void run() {
// TODO Auto-generated method stub
try {
updateFile = Environment.getExternalStorageDirectory()
+ "/3530.jpg";
//downloadUpdateFile("http://image.anzimall.com/3530/3530_K21D_app_V2.3_2015050528.bin", updateFile);
downloadUpdateFile("http://img1.cache.netease.com/catchpic/F/FC/FCC085159B92C5EE4FDDB9618166051E.jpg", updateFile);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace(); //android.os.NetworkOnMainThreadException
System.out.println(e.toString()+"");
}
}
}).start();

2.抛出异常之后一定要打印异常,看看异常的具体信息。System.out.println(e.toString()+"");


public static long downloadUpdateFile(String down_url, String file)
throws Exception { int downloadCount = 0;// 已经下载好的大小 InputStream inputStream;
OutputStream outputStream;
URL url = new URL(down_url);
HttpURLConnection httpurlconnection = (HttpURLConnection) url
.openConnection();
httpurlconnection.setConnectTimeout(5000);
httpurlconnection.setReadTimeout(5000);
// 获取下载文件的size
//totalSize = httpURLConnection.getContentLength(); if (httpurlconnection.getResponseCode() == 404) {
throw new Exception("fail!");
// 这个地方应该加一个下载失败的处理,但是,因为我们在外面加了一个try---catch,已经处理了Exception,
// 所以不用处理
} inputStream = httpurlconnection.getInputStream();
File file_ok = new File(file);
outputStream = new FileOutputStream(file_ok);// 文件存在则覆盖掉 byte buffer[] = new byte[1024];
int readsize = 0; while ((readsize = inputStream.read(buffer)) != -1) {
outputStream.write(buffer, 0, readsize);
downloadCount += readsize;// 时时获取下载到的大小
}
if (httpurlconnection != null) {
httpurlconnection.disconnect();
}
inputStream.close();
outputStream.close(); return downloadCount;
}

  

主线程中一定不能放耗时操作,必须要开子线程,比如下载文件,不然会不让你拿到输入流--报错显示android.os.NetworkOnMainThreadException的更多相关文章

  1. android4.0以上访问网络不能在主线程中进行以及在线程中操作UI的解决方法

    MONO 调用一个线程操作UI 然后报Only the original thread that created a view hierarchy can touch its views.错误 goo ...

  2. 用Handler的post()方法来传递线程中的代码段到主线程中执行

    自定义的线程中是不能更新UI的,但是如果遇到更新UI的事情,我们可以用handler的post()方法来将更新UI的方法体,直接传送到主线程中,这样就能直接更新UI了.Handler的post()方法 ...

  3. 主线程中也不绝对安全的 UI 操作

    从最初开始学习 iOS 的时候,我们就被告知 UI 操作一定要放在主线程进行.这是因为 UIKit 的方法不是线程安全的,保证线程安全需要极大的开销.那么问题来了,在主线程中进行 UI 操作一定是安全 ...

  4. 在非主线程中更新UI

    在非主线程中调用了showMessage方法,结果报错:Can't create handler inside thread that has not called Looper.prepare() ...

  5. 主线程中的Looper.loop()为什么不会造成ANR

    引子: 正如我们所知,在android中如果主线程中进行耗时操作会引发ANR(Application Not Responding)异常. 造成ANR的原因一般有两种: 当前的事件没有机会得到处理(即 ...

  6. 解决Android3.0之后不能在主线程中进行HTTP请求

    感谢大佬:https://www.cnblogs.com/falzy/p/5763848.html 在Android3.0以后,会发现,只要是写在主线程(就是Activity)中的HTTP请求,运行时 ...

  7. Linux 下子线程 exit code 在主线程中的使用

    Linux线程函数原型是这样的: void* thread_fun(void* arg) 它的返回值是 空类型指针,入口参数也是 空类型指针.那么线程的 exit code 也应该是 void * 类 ...

  8. [原]unity中WWW isDone方法只能在主线程中调用

    项目中要使用动态加载,原计划是生成WWW对象后,放到一个容器里.由一个独立线程轮询容器里的对象,如果www.isDone为true时,回调一个接口把结果交给请求方. new Thread( new T ...

  9. 在主线程中慎用WaitForSingleObject (WaitForMultipleObjects)

    下面的代码我调试了将近一个星期,你能够看出什么地方出了问题吗?线程函数: DWORD WINAPI ThreadProc(    while(!bTerminate)    {        // 从 ...

随机推荐

  1. SCCM部署win7操作系统,系统盘为D盘

    SCCM部署win7操作系统,系统盘为D盘 1. 系统部署完毕之后是这样的 2.解决方案 解决分发干净Windows7操作系统盘符为”D”的方法也比较简单. 在SCCM管理控制台中,打开”软件库””操 ...

  2. Weka 3: Data Mining Software in Java

    官方网站: Weka 3: Data Mining Software in Java 相关使用方法博客 WEKA使用教程(经典教程转载) (实例数据:bank-data.csv) Weka初步一.二. ...

  3. The Economist

      The turning point in the process of growing up is when you discover the core of strength within yo ...

  4. [redis] windwos下安装和使用redis

    一篇很好的入门学习文章:Redis学习 Redis是一种nosql数据库,在开发中常用做缓存. 1.下载地址: 低版本下载地址:https://github.com/dmajkic/redis/dow ...

  5. python实现微信打飞机游戏

    环境:Ubuntu 16.04 LTS Python 2.7.11 +  Pygame + Pycharm 代码: # -*- coding: UTF-8 -*- import pygame, ran ...

  6. WPF:ListView数据绑定及Style

    DrugRecordsWin.xaml <ListView Grid.Row="4" Grid.Column="1" Name="list_Dr ...

  7. hdu------(4300)Clairewd’s message(kmp)

    Clairewd’s message Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Other ...

  8. PHP 页面编码声明方法详解(header或meta)

    php的header来定义一个php页面为utf编码或GBK编码 php页面为utf编码 header("Content-type: text/html; charset=utf-8&quo ...

  9. C# + winserver2008 openfiledialog 写入 textbox1 中的 路径不正确

    System.IO.Path.GetFullPath(openFileDialog1.FileName);//绝对路径 System.IO.Path.GetExtension(openFileDial ...

  10. MYSQL 日期函数【转】

    MySQL日期时间函数大全 DAYOFWEEK(date) 返回日期date是星期几(=星期六,ODBC标准) mysql> select DAYOFWEEK('1998-02-03'); WE ...