Android从互联网上读取文本文件

时间:2013-06-17 06:49:40

标签: android textview text-files httpurlconnection

我想阅读远程文本文件并在textview中显示其内容。我在下面的代码中写了这个,但它没有从文本文件中获取任何信息。 我怎样才能找到这个问题的原因或解决它? 我的代码没有任何问题吗?

    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 个答案:

答案 0 :(得分:4)

  1. 您是否为您的清单文件添加了互联网权限?
  2. 您是否在单独的线程中启动代码(请不要捕获NetworkOnMainThreadException)
  3. 检查LogCat你有什么例外?
  4. 删除c.setDoOutput(true);这用于向服务器发送数据。
  5. 这是怎么回事:

    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();