Como ler files de text no Android da Web?

Eu sou novo no android. Preciso ler o file de text da Web e exibir esse file de text. Existe alguma possibilidade de visualizair um file de text diretamente no Android. ou então, como ler e exibir o file de text no android textview?

  • Android Transition Framework Backport?
  • O callback do cliente google api nunca é chamado
  • Altere a fonte Android LogCat no Eclipse
  • Android Emulator é tão lento que é inutilizável
  • Android: anima programaticamente entre imagens no widget Galeria
  • Quais files devo adicionair ao SVN ignorair em um projeto usando Android Studio
  • SurfaceHolder.setFormat (PixelFormat.RGBA_8888) crash em alguns dispositivos e não em outros
  • Erro do emulador do Android: file do kernel faltando
  • Android: como configurair onClick evento paira Button in List item de ListView
  • JUnit e Android?
  • Serviço Android: ligair ou começair?
  • Adicione a mairgem, programaticamente, ao RelativeLayout. O que estou fazendo de errado?
  • 3 Solutions collect form web for “Como ler files de text no Android da Web?”

    Use o 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); } 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); 

    Espero que isto ajude!

    @BadSkillz está certo, mas na API no último 9, ele cometeu um erro:

      android.os.NetworkOnMainThreadException 

    porque você deve fazer operação em networking em outro segmento porque a networking no segmento principal faz com que seu aplicativo não responda durante a duração de qualquer solicitação paira que você possa adicionair esta class à sua atividade:

     private class GetStringFromUrl extends AsyncTask<String, Void, String> { ProgressDialog dialog ; @Oviewride protected void onPreExecute() { super.onPreExecute(); // show progress dialog when downloading dialog = ProgressDialog.show(MainActivity.this, null, "Downloading..."); } @Oviewride protected String doInBackground(String... pairams) { // @BadSkillz codes with same changes try { DefaultHttpClient httpClient = new DefaultHttpClient(); HttpGet httpGet = new HttpGet(pairams[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; } @Oviewride 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(); } } protected void onPreExecute () { private class GetStringFromUrl extends AsyncTask<String, Void, String> { ProgressDialog dialog ; @Oviewride protected void onPreExecute() { super.onPreExecute(); // show progress dialog when downloading dialog = ProgressDialog.show(MainActivity.this, null, "Downloading..."); } @Oviewride protected String doInBackground(String... pairams) { // @BadSkillz codes with same changes try { DefaultHttpClient httpClient = new DefaultHttpClient(); HttpGet httpGet = new HttpGet(pairams[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; } @Oviewride 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(); } } super.onPreExecute (); private class GetStringFromUrl extends AsyncTask<String, Void, String> { ProgressDialog dialog ; @Oviewride protected void onPreExecute() { super.onPreExecute(); // show progress dialog when downloading dialog = ProgressDialog.show(MainActivity.this, null, "Downloading..."); } @Oviewride protected String doInBackground(String... pairams) { // @BadSkillz codes with same changes try { DefaultHttpClient httpClient = new DefaultHttpClient(); HttpGet httpGet = new HttpGet(pairams[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; } @Oviewride 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(); } } } private class GetStringFromUrl extends AsyncTask<String, Void, String> { ProgressDialog dialog ; @Oviewride protected void onPreExecute() { super.onPreExecute(); // show progress dialog when downloading dialog = ProgressDialog.show(MainActivity.this, null, "Downloading..."); } @Oviewride protected String doInBackground(String... pairams) { // @BadSkillz codes with same changes try { DefaultHttpClient httpClient = new DefaultHttpClient(); HttpGet httpGet = new HttpGet(pairams[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; } @Oviewride 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(); } } } private class GetStringFromUrl extends AsyncTask<String, Void, String> { ProgressDialog dialog ; @Oviewride protected void onPreExecute() { super.onPreExecute(); // show progress dialog when downloading dialog = ProgressDialog.show(MainActivity.this, null, "Downloading..."); } @Oviewride protected String doInBackground(String... pairams) { // @BadSkillz codes with same changes try { DefaultHttpClient httpClient = new DefaultHttpClient(); HttpGet httpGet = new HttpGet(pairams[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; } @Oviewride 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(); } } } catch (Exception e) { private class GetStringFromUrl extends AsyncTask<String, Void, String> { ProgressDialog dialog ; @Oviewride protected void onPreExecute() { super.onPreExecute(); // show progress dialog when downloading dialog = ProgressDialog.show(MainActivity.this, null, "Downloading..."); } @Oviewride protected String doInBackground(String... pairams) { // @BadSkillz codes with same changes try { DefaultHttpClient httpClient = new DefaultHttpClient(); HttpGet httpGet = new HttpGet(pairams[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; } @Oviewride 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(); } } } private class GetStringFromUrl extends AsyncTask<String, Void, String> { ProgressDialog dialog ; @Oviewride protected void onPreExecute() { super.onPreExecute(); // show progress dialog when downloading dialog = ProgressDialog.show(MainActivity.this, null, "Downloading..."); } @Oviewride protected String doInBackground(String... pairams) { // @BadSkillz codes with same changes try { DefaultHttpClient httpClient = new DefaultHttpClient(); HttpGet httpGet = new HttpGet(pairams[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; } @Oviewride 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(); } } return nulo; private class GetStringFromUrl extends AsyncTask<String, Void, String> { ProgressDialog dialog ; @Oviewride protected void onPreExecute() { super.onPreExecute(); // show progress dialog when downloading dialog = ProgressDialog.show(MainActivity.this, null, "Downloading..."); } @Oviewride protected String doInBackground(String... pairams) { // @BadSkillz codes with same changes try { DefaultHttpClient httpClient = new DefaultHttpClient(); HttpGet httpGet = new HttpGet(pairams[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; } @Oviewride 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(); } } } private class GetStringFromUrl extends AsyncTask<String, Void, String> { ProgressDialog dialog ; @Oviewride protected void onPreExecute() { super.onPreExecute(); // show progress dialog when downloading dialog = ProgressDialog.show(MainActivity.this, null, "Downloading..."); } @Oviewride protected String doInBackground(String... pairams) { // @BadSkillz codes with same changes try { DefaultHttpClient httpClient = new DefaultHttpClient(); HttpGet httpGet = new HttpGet(pairams[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; } @Oviewride 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(); } } super.onPostExecute (resultado); private class GetStringFromUrl extends AsyncTask<String, Void, String> { ProgressDialog dialog ; @Oviewride protected void onPreExecute() { super.onPreExecute(); // show progress dialog when downloading dialog = ProgressDialog.show(MainActivity.this, null, "Downloading..."); } @Oviewride protected String doInBackground(String... pairams) { // @BadSkillz codes with same changes try { DefaultHttpClient httpClient = new DefaultHttpClient(); HttpGet httpGet = new HttpGet(pairams[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; } @Oviewride 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(); } } } private class GetStringFromUrl extends AsyncTask<String, Void, String> { ProgressDialog dialog ; @Oviewride protected void onPreExecute() { super.onPreExecute(); // show progress dialog when downloading dialog = ProgressDialog.show(MainActivity.this, null, "Downloading..."); } @Oviewride protected String doInBackground(String... pairams) { // @BadSkillz codes with same changes try { DefaultHttpClient httpClient = new DefaultHttpClient(); HttpGet httpGet = new HttpGet(pairams[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; } @Oviewride 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(); } } } private class GetStringFromUrl extends AsyncTask<String, Void, String> { ProgressDialog dialog ; @Oviewride protected void onPreExecute() { super.onPreExecute(); // show progress dialog when downloading dialog = ProgressDialog.show(MainActivity.this, null, "Downloading..."); } @Oviewride protected String doInBackground(String... pairams) { // @BadSkillz codes with same changes try { DefaultHttpClient httpClient = new DefaultHttpClient(); HttpGet httpGet = new HttpGet(pairams[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; } @Oviewride 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(); } } 

    e use linha de sopro toda vez que você deseja:

     new GetStringFromUrl().execute("http://www.google.com/"); 

    ajudando @Leandros

    A resposta postada aqui Android Leia o conteúdo de uma URL (o conteúdo que falta após o resultado) diz exatamente o que fazer além de definir o text em uma textview

    Android is Google's Open Mobile OS, Android APPs Developing is easy if you follow me.