360ITO技术社区
  • 首页
  • 文章
  • 快讯
  • 讨论
  • 问答
  • 小贴士
  • 代码块
  • 开源
  • 老论坛
登录 | 注册

360ITO技术社区  > 问答

订阅问答
smallghost
有用 0 无用

HttpURLConnection.connect的时候出现 异常android.os.NetworkOnMainThreadException怎么解决?

smallghost 发布于 6年前  查看:8322

网络 android

共有2个答案 我要回答»

按时间排 按有用数排 按支持数排
但愿人持久
有用 1 无用
但愿人持久 6年前 | [引述]  [点评] 
1

我给你来个完整的,经过测试的!

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        executorService.execute(new downloadRun());
    }

    private ExecutorService executorService = Executors.newFixedThreadPool(4);
    private class downloadRun implements Runnable {
        @Override
        public void run() {
            InputStream inputStream = null;
            ByteArrayOutputStream byteArrayOutputStream = null;
            byte[] buff;
            int len;
            try {
                URL url = new URL("www.360ito.com");
                try {
                    HttpURLConnection connection = (HttpURLConnection) url.openConnection();
                    connection.setConnectTimeout(15 * 1000);
                    connection.setReadTimeout(10 * 1000);
                    connection.setRequestMethod("GET");
                    connection.setDoInput(true);
                    connection.connect();
                    byteArrayOutputStream = new ByteArrayOutputStream();
                    buff = new byte[1024];
                    
                    if (connection.getResponseCode() == HttpURLConnection.HTTP_OK) {
                        inputStream = connection.getInputStream();
                        while ((len = inputStream.read(buff)) != -1) {
                            byteArrayOutputStream.write(buff, 0, len);
                        }
                        byteArrayOutputStream.flush();
                    }
                    
                } catch (IOException e) {
                    e.printStackTrace();
                }
                finally {
                     if (null != inputStream) {
                         try {
                             inputStream.close();
                         } catch (IOException e) {
                             e.printStackTrace();
                         }
                     }
                     if (null != byteArrayOutputStream) {
                         try {
                             byteArrayOutputStream.close();
                         } catch (IOException e) {
                             e.printStackTrace();
                         }
                     }
                }
            } catch (MalformedURLException e) {
                e.printStackTrace();
            }
        }
    };

埋汰孩兒
有用 0 无用
埋汰孩兒 6年前 | [引述]  [点评] 
0

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        new Thread(downloadRun).start();
    }

    Runnable downloadRun = new Runnable(){

        @Override
        public void run() {
            // 在这里处理网络
        }
    };

网友回复/评论仅代表其个人看法,并不表明本社区同意其观点或证实其描述。

请尽量让自己的回复能够对别人有帮助

1.不欢迎无意义的回复/评论和类似“顶”、“沙发”之类没有营养的文字
如果只是想简单的表个态,请点 有用无用支持反对 等按钮
2.发言之前请再仔细看一遍文章,或许是您遗漏、误解了,理性讨论、切莫乱喷
3.严禁发布违法、违规的信息,请勿到处招贴广告、发布软文;
4.如果您发现自己的回复/评论不见了,请参考以上3条
5.不停制造违规、垃圾信息的,账户将被禁止

热门标签

  • android 23
  • Python 22
  • delphi 13
  • SQLite 8
  • Android Studio 8
  • Flask 4
  • 网络 4
  • MongoDB 4
  • Linux 3
  • Flash 3
  • JavaScript 3
  • 路由器 2
  • Web Server 2
  • C++ 2
  • vi/vim 2
  • uWSGI 2
  • PIL 2
  • GitHub 2
  • git 2
  • peewee 2

相关文章

周热点

月热点

Copyright ©2011-2012 360ITO技术社区 All Rights Reserved. | 关于 | 联系我们 | 杭州精创信息技术有限公司 浙ICP备09019653号-26|
▲