1

im trying to create an application where a page has its text created automatically by reading off a website?

I understand an array would be used and string formatting, i am ok with android programming but not an expert lol.

I had tried using a set of coding found on this website but i think this is for only reading off a text file online:

private TextView outtext;
private String HTML;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    /*FROM HERE*/
    outtext= (TextView) findViewById(R.id.textview1); //change id if needed!!!

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






private void getHTML() throws ClientProtocolException, IOException 

{
    HttpClient httpClient = new DefaultHttpClient();
    HttpContext localContext = new BasicHttpContext();
    HttpGet httpGet = new HttpGet("http://www.bbc.co.uk/news/"); //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;
    }

}

}

I tried using bbc.co.uk/news/ as an example. The text view on my app returned 'null'.

Hope someone can help me out, thanks in advance.

Nazul Khan
  • 11
  • 1

1 Answers1

0

Looks like

HTML = result;

Should be outside the while loop?

And then I doubt the HTML will be populated by the time you set the TextView value. You need to process the HTML request in a thread then post the result to the UI with a Handler:

Handler Class API

private final Handler handler = new Handler();

new Thread()
{
    @Override
    public void run()
    {
        getHTML();
        handler.post( updateResults );
    }
}.start();


final Runnable updateResults = new Runnable()
{
    @Override
    public void run()
    {
        updateUi();
    }
};

private void updateUi()
{
    outtext.setText( "" + HTML );
}
R.daneel.olivaw
  • 2,681
  • 21
  • 31
gdonald
  • 984
  • 2
  • 12
  • 23