简体   繁体   中英

Put webpage text into a textview (Android)

I want to put a text from a webpage to a textview on Android 3.0. I have this code:

public class Biografie extends Activity {
    private TextView outtext;
    private String HTML;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_biografie);

        outtext= (TextView) findViewById(R.id.textview1); 

        try { 
        getHTML();
    } catch (Exception e) {
        e.printStackTrace();
    }       
    outtext.setText("" + HTML);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.biografie, menu);
        return true;
    }

    private void getHTML() throws ClientProtocolException, IOException 

    {
        HttpClient httpClient = new DefaultHttpClient();
        HttpContext localContext = new BasicHttpContext();
        HttpGet httpGet = new HttpGet("http://artistone.appone.nl/api/biografie.php?dataid=998"); //URL!
        HttpResponse response = httpClient.execute(httpGet, localContext);
        String result = "";

        BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));

        String line = null;
        while ((line = reader.readLine()) != null) {
            result += line + "\n";
            HTML = result;
        }

    }

}

My TextView returns "null" instead of the text from the page. Please help me to fix this. Thanks in regard.

Change your code to:

while ((line = reader.readLine()) != null) {
    result += line + "\n";
}
HTML = result;

and try this: outtext.setText(Html.fromHtml(HTML));

And instead of performing network action in main thread i will suggest you to do this in separate thread using AsyncTask

The problem is that you are getting NetworkOnMainThreadException That is because you are downloading network content on the Main Thread (Activity's Thread).

Instead you need to use a background thread to download that content, or use AsynchTask .

A simple code that should fix this issue:

    final Handler handler = new Handler();
    Thread thread = new Thread() {
        public void run() {
            try {
                getHTML();
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        outtext.setText("" + HTML);

                    }
                });
            } catch (Exception e) {
                e.printStackTrace();
                    handler.post(new Runnable() {
                    @Override
                    public void run() {
                        outtext.setText(e.toString());

                    }
            }
        }
    };
thread.start(); // I forgot to start the thread. sorry !

Instead of :

    try { 
           getHTML();
    } catch (Exception e) {
        e.printStackTrace();
     }       
    outtext.setText("" + HTML);

Also take a look at this tutorial about android threads : Tutorial

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM