I am new to android.I need to read text file from Web and display that text file.Is there any possibility to view a text file directly in android. or else how to read and display the text file in android textview ?
            Asked
            
        
        
            Active
            
        
            Viewed 2.8k times
        
    3 Answers
20
            Use the DefaultHttpClient  httpclient = new DefaultHttpClient();
HttpGet httppost = new HttpGet("http://www.urlOfThePageYouWantToRead.nl/text.txt");
HttpResponse response = httpclient.execute(httppost);
        HttpEntity ht = response.getEntity();
        BufferedHttpEntity buf = new BufferedHttpEntity(ht);
        InputStream is = buf.getContent();
        BufferedReader r = new BufferedReader(new InputStreamReader(is));
        StringBuilder total = new StringBuilder();
        String line;
        while ((line = r.readLine()) != null) {
            total.append(line + "\n");
        }
        TextView.setText(total);
Hope this helps!
 
    
    
        BadSkillz
        
- 1,993
- 19
- 37
- 
                    Why did you used append, if you used + after? It should be append(line).append('\n') – Boldijar Paul Oct 04 '15 at 11:25
- 
                    2This is now deprecated – Zoe Aug 28 '16 at 19:14
6
            
            
        @BadSkillz is right but in API last 9 it make error:
    android.os.NetworkOnMainThreadException
because you must do networking operation in another thread because networking in main thread makes your application unresponsive for duration of any request so you can add this class into your activity:
private class GetStringFromUrl extends AsyncTask<String, Void, String> {
    ProgressDialog dialog ;
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        // show progress dialog when downloading 
        dialog = ProgressDialog.show(MainActivity.this, null, "Downloading...");
    }
    @Override
    protected String doInBackground(String... params) {
        // @BadSkillz codes with same changes
        try {
            DefaultHttpClient httpClient = new DefaultHttpClient();
            HttpGet httpGet = new HttpGet(params[0]);
            HttpResponse response = httpClient.execute(httpGet);
            HttpEntity entity = response.getEntity();
            BufferedHttpEntity buf = new BufferedHttpEntity(entity);
            InputStream is = buf.getContent();
            BufferedReader r = new BufferedReader(new InputStreamReader(is));
            StringBuilder total = new StringBuilder();
            String line;
            while ((line = r.readLine()) != null) {
                total.append(line + "\n");
            }
            String result = total.toString();
            Log.i("Get URL", "Downloaded string: " + result);
            return result;
        } catch (Exception e) {
            Log.e("Get Url", "Error in downloading: " + e.toString());
        }
        return null;
    }
    @Override
    protected void onPostExecute(String result) {
        super.onPostExecute(result);
        // TODO change text view id for yourself
        TextView textView = (TextView) findViewById(R.id.textView1);
        // show result in textView
        if (result == null) {
            textView.setText("Error in downloading. Please try again.");
        } else {
            textView.setText(result);
        }
        // close progresses dialog
        dialog.dismiss();
    }
}
and use blow line every time that you want:
new GetStringFromUrl().execute("http://www.google.com/");
by helping @Leandros
 
    
    
        golkarm
        
- 1,021
- 1
- 11
- 22
- 
                    I had to make 'textView' a member of the class and link it through the constructor, because 'findViewById' is not accessable in a AsyncTask. For the rest, works like a charm! – Jon V Dec 22 '14 at 14:02
- 
                    
2
            
            
        The answer posted in here Android Read contents of a URL (content missing after in result) tells you exactly what to do apart from setting the text in a textview
 
     
    