Android read text file from internet

断了今生、忘了曾经 提交于 2019-12-30 07:41:39

问题


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:


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


来源:https://stackoverflow.com/questions/17141829/android-read-text-file-from-internet

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!