Android的读取互联网文本文件

Android的读取互联网文本文件

本文介绍了Android的读取互联网文本文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想读远程文本文件,并显示其在一个TextView的内容。我已经写了这以下code,但它不会从文本文件中的任何信息。我如何才能找到这个问题的原因或解决吗?是不是有什么错在我的code?

 私人无效READFILE()
    {
         尝试 {
            字符串路径=htt​​p://host.com/info.txt;
            网址U =新的URL(路径);
            HttpURLConnection的C =(HttpURLConnection类)u.openConnection();
            c.setRequestMethod(GET);
            c.setDoOutput(真正的);
            c.connect();
            在的InputStream = c.getInputStream();
            Log.e(价值,in.toString());
            AssetManager mngr = getAssets();
            ByteArrayOutputStream博=新ByteArrayOutputStream();
            byte []的缓冲区=新的字节[1024];
            in.read(缓冲液); //读自缓冲区。
            bo.write(缓冲液); //写入缓冲区。
            TextView的文字=(TextView的)findViewById(R.id.TextView1);
            text.setText(bo.toString());
            bo.close();
            }
         赶上(NetworkOnMainThreadException E){
        }
            赶上(MalformedURLException的E){
            e.printStackTrace();
            }赶上(FileNotFoundException异常E){
            e.printStackTrace();
            }赶上(IOException异常E){
            e.printStackTrace();
            }
    }
}
 

解决方案
  1. 你有没有加入网络权限的清单文件?
  2. 你启动你的code。在单独的线程(请不要赶NetworkOnMainThreadException)
  3. 检查LogCat中你有什么异常?
  4. 删除 c.setDoOutput(真); 这是用来将数据发送到服务器

下面就应该是这样:

 新的Thread(){
            @覆盖
            公共无效的run(){
                字符串路径=htt​​p://host.com/info.txt;
                URL U = NULL;
                尝试 {
                    U =新的URL(路径);
                    HttpURLConnection的C =(HttpURLConnection类)u.openConnection();
                    c.setRequestMethod(GET);
                    c.connect();
                    在的InputStream = c.getInputStream();
                    最后ByteArrayOutputStream博=新ByteArrayOutputStream();
                    byte []的缓冲区=新的字节[1024];
                    in.read(缓冲液); //读自缓冲区。
                    bo.write(缓冲液); //写入缓冲区。

                    runOnUiThread(新的Runnable(){
                        @覆盖
                        公共无效的run(){
                            TextView的文字=(TextView的)findViewById(R.id.TextView1);
                            text.setText(bo.toString());
                            尝试 {
                                bo.close();
                            }赶上(IOException异常E){
                                e.printStackTrace();
                            }
                        }
                    });

                }赶上(MalformedURLException异常E){
                    e.printStackTrace();
                }赶上(的ProtocolException E){
                    e.printStackTrace();
                }赶上(IOException异常E){
                    e.printStackTrace();
                }

            }
        }。开始();
 

I want to read a remote text file and show its content in a textview. I have written this below code, but it doesn't get any information from the text file.How can I find the reason of this problem or solve it?isn't there anything wrong in my code?

    private void readFile()
    {
         try {
            String path ="http://host.com/info.txt";
            URL u = new URL(path);
            HttpURLConnection c = (HttpURLConnection) u.openConnection();
            c.setRequestMethod("GET");
            c.setDoOutput(true);
            c.connect();
            InputStream in = c.getInputStream();
            Log.e("value",in.toString());
            AssetManager mngr=getAssets();
            ByteArrayOutputStream bo = new ByteArrayOutputStream();
            byte[] buffer = new byte[1024];
            in.read(buffer); // Read from Buffer.
            bo.write(buffer); // Write Into Buffer.
            TextView text = (TextView) findViewById(R.id.TextView1);
            text.setText(bo.toString());
            bo.close();
            }
         catch (NetworkOnMainThreadException e) {
        }
            catch (MalformedURLException e) {
            e.printStackTrace();
            } catch (FileNotFoundException e) {
            e.printStackTrace();
            } catch (IOException e) {
            e.printStackTrace();
            }
    }
}
解决方案
  1. Did you added internet permission to your Manifest file?
  2. Are you launching your code in separate thread (please don't catch NetworkOnMainThreadException)
  3. Check LogCat what exception do you have?
  4. Removed c.setDoOutput(true); this is used to send data to server.

Here how it should be:

new Thread() {
            @Override
            public void run() {
                String path ="http://host.com/info.txt";
                URL u = null;
                try {
                    u = new URL(path);
                    HttpURLConnection c = (HttpURLConnection) u.openConnection();
                    c.setRequestMethod("GET");
                    c.connect();
                    InputStream in = c.getInputStream();
                    final ByteArrayOutputStream bo = new ByteArrayOutputStream();
                    byte[] buffer = new byte[1024];
                    in.read(buffer); // Read from Buffer.
                    bo.write(buffer); // Write Into Buffer.

                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            TextView text = (TextView) findViewById(R.id.TextView1);
                            text.setText(bo.toString());
                            try {
                                bo.close();
                            } catch (IOException e) {
                                e.printStackTrace();
                            }
                        }
                    });

                } catch (MalformedURLException e) {
                    e.printStackTrace();
                } catch (ProtocolException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }

            }
        }.start();

这篇关于Android的读取互联网文本文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-25 20:40