Skip to content Skip to sidebar Skip to footer

How To Read Text File In Android From Web?

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 t

Solution 1:

Use the DefaultHttpClient httpclient = new DefaultHttpClient();

HttpGethttppost=newHttpGet("http://www.urlOfThePageYouWantToRead.nl/text.txt");
HttpResponseresponse= httpclient.execute(httppost);
        HttpEntityht= response.getEntity();

        BufferedHttpEntitybuf=newBufferedHttpEntity(ht);

        InputStreamis= buf.getContent();


        BufferedReaderr=newBufferedReader(newInputStreamReader(is));

        StringBuildertotal=newStringBuilder();
        String line;
        while ((line = r.readLine()) != null) {
            total.append(line + "\n");
        }

        TextView.setText(total);

Hope this helps!

Solution 2:

@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:

privateclassGetStringFromUrlextendsAsyncTask<String, Void, String> {

    ProgressDialog dialog ;

    @OverrideprotectedvoidonPreExecute() {
        super.onPreExecute();

        // show progress dialog when downloading 
        dialog = ProgressDialog.show(MainActivity.this, null, "Downloading...");
    }

    @Overrideprotected String doInBackground(String... params) {

        // @BadSkillz codes with same changestry {
            DefaultHttpClienthttpClient=newDefaultHttpClient();
            HttpGethttpGet=newHttpGet(params[0]);
            HttpResponseresponse= httpClient.execute(httpGet);
            HttpEntityentity= response.getEntity();

            BufferedHttpEntitybuf=newBufferedHttpEntity(entity);

            InputStreamis= buf.getContent();

            BufferedReaderr=newBufferedReader(newInputStreamReader(is));

            StringBuildertotal=newStringBuilder();
            String line;
            while ((line = r.readLine()) != null) {
                total.append(line + "\n");
            }
            Stringresult= total.toString();
            Log.i("Get URL", "Downloaded string: " + result);
            return result;
        } catch (Exception e) {
            Log.e("Get Url", "Error in downloading: " + e.toString());
        }
        returnnull;
    }

    @OverrideprotectedvoidonPostExecute(String result) {
        super.onPostExecute(result);

        // TODO change text view id for yourselfTextViewtextView= (TextView) findViewById(R.id.textView1);

        // show result in textViewif (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

Solution 3:

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

Post a Comment for "How To Read Text File In Android From Web?"