This part of code can help you:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
new HttpTask().execute("http://www.google.com");
}
public String getWebPage(String adresse) {
HttpClient httpClient = new DefaultHttpClient();
HttpGet httpGet = new HttpGet();
InputStream inputStream = null;
String response = null;
try {
URI uri = new URI(adresse);
httpGet.setURI(uri);
HttpResponse httpResponse = httpClient.execute(httpGet);
int statutCode = httpResponse.getStatusLine().getStatusCode();
int length = (int) httpResponse.getEntity().getContentLength();
Log.v(LOG_THREAD_ACTIVITY, "HTTP GET: " + adresse);
Log.v(LOG_THREAD_ACTIVITY, "HTTP StatutCode: " + statutCode);
Log.v(LOG_THREAD_ACTIVITY, "HTTP Lenght: " + length + " bytes");
inputStream = httpResponse.getEntity().getContent();
Reader reader = new InputStreamReader(inputStream, "UTF-8");
int inChar;
StringBuffer stringBuffer = new StringBuffer();
while ((inChar = reader.read()) != -1) {
stringBuffer.append((char) inChar);
}
response = stringBuffer.toString();
} catch (ClientProtocolException e) {
Log.e(LOG_THREAD_ACTIVITY, "HttpActivity.getPage() ClientProtocolException error", e);
} catch (IOException e) {
Log.e(LOG_THREAD_ACTIVITY, "HttpActivity.getPage() IOException error", e);
} catch (URISyntaxException e) {
Log.e(LOG_THREAD_ACTIVITY, "HttpActivity.getPage() URISyntaxException error", e);
} finally {
try {
if (inputStream != null)
inputStream.close();
} catch (IOException e) {
Log.e(LOG_THREAD_ACTIVITY, "HttpActivity.getPage() IOException error lors de la fermeture des flux", e);
}
}
return response;
}
private class HttpTask extends AsyncTask<String, Integer, String> {
@Override
protected String doInBackground(String... urls) {
// TODO Auto-generated method stub
String response = getWebPage(urls[0]);
return response;
}
@Override
protected void onPostExecute(String response) {
Log.i(LOG_THREAD_ACTIVITY, "HTTP RESPONSE" + response);
textViewConsole.setText(response);
}
@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
super.onPreExecute();
}
@Override
protected void onProgressUpdate(Integer... values) {
// TODO Auto-generated method stub
super.onProgressUpdate(values);
}
}