Android read text file from internet

2019-02-15 18:17发布

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条回答
叼着烟拽天下
2楼-- · 2019-02-15 18:26
  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();
查看更多
登录 后发表回答