Android Lab Test : Using the network with HTTP (english)
Video of tutorial on : https://www.youtube.com/playlist?list=PLL2Z3bzdO25yHwIV3XdMzKs61At0Ldh6L
Visit http://www.AndroidLabTest.com
Android Lab Test : Using the network with HTTP (english)
1. Develop on Android
Android Lab Test
www.AndroidLabTest.com
Youku
By Bruno Delb
www.weibo.com/brunodelb
i.youku.com/brunodelb | www.weibo.com/brunodelb | blog.delb.cn
http://i.youku.com/brunoparis
Weibo
Officialsite
Lesson : Reading HTTP
2. Reading HTTP
• In this lesson, you will learn to launch a HTTP request on
a Web server.
• For this, you will use the DefaultHttpClient, HttpGet /
HttpPost and an InputStream.
3. Reading HTTP
• To do a request of type GET, use the class HttpGet :
HttpGet http = new HttpGet (new URI (url));
• To do a request of type POST, use the class HttpPost :
HttpPost http = new HttpPost (new URI (url));
• To add a HTTP header, use the method addHeader() :
http.addHeader("pragma","no-cache");
4. Reading HTTP
• To add data, use the object List <NameValuePair> in orderto store them
then call the method setEntity :
http.setEntity(new UrlEncodedFormEntity (headers, HTTP.UTF_8));
• To run the HTTP request, use the method execute() :
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpResponse httpResponse = httpClient.execute(http);
• To read the data returned by the server, use an InputStream :
InputStream inputStream = httpResponse.getEntity().getContent();
5. Reading HTTP
• To read a stream InputStream, use an InputStreamReader :
InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
• Read the data line by line thanks to a BufferedReader :
BufferedReader bufferedReader = new BufferedReader (inputStreamReader);
StringBuilder stringBuilder = new StringBuilder();
String line;
while ((line = bufferedReader.readLine()) != null) {
stringBuilder.append(line).append("n");
}
6. Reading HTTP
• Then you have to close the InputStream :
inputStream.close();
• The read text is in the StringBuilder :
String html = stringBuilder.toString();
13. Follow me on my channel PengYooTV …
On my Youku channel
http://i.youku.com/brunoparis
Who am I ?
Bruno Delb (www.delb.cn),
Author of the first french book of development of Java mobile application (2002),
Consultant, project manager and developer of social & mobile applications,
let’s talk about your needs ...
And on Weibo :
http://www.weibo.com/brunodelb