lundi 4 mai 2015

Getting and displaying the source code of a web page in android's textbox or a label

I want to be able to get the Source code of a web page (like http://www.google.com) and display it in the Android main activity's text box or a label box... Here is the current code i've written. But it doesn't load the code in the EditText

public class MainActivity extends ActionBarActivity {
public String html = "";
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_main);
    try {
        HttpClient client = new DefaultHttpClient();
        HttpGet request = new HttpGet("http://www.google.com");
        HttpResponse response = client.execute(request);


        InputStream in = response.getEntity().getContent();
        BufferedReader reader = new BufferedReader(new InputStreamReader(in));
        StringBuilder str = new StringBuilder();
        String line = null;
        while ((line = reader.readLine()) != null) {
            str.append(line);
        }
        in.close();
        html = str.toString();
    }
    catch(Exception e){}
    EditText t = (EditText)findViewById(R.id.editText);
    t.setText(html);
}




Aucun commentaire:

Enregistrer un commentaire