How to read text file in android from web?

后端 未结 3 524
伪装坚强ぢ
伪装坚强ぢ 2020-12-05 15:44

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 di

相关标签:
3条回答
  • 2020-12-05 15:54

    @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

    0 讨论(0)
  • 2020-12-05 15:59

    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!

    0 讨论(0)
  • 2020-12-05 16:17

    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

    0 讨论(0)
提交回复
热议问题