1.what can you use the http request for? 2.what is json?nutthanon/...pro android s - day 2 overview...
TRANSCRIPT
![Page 1: 1.What can you use the HTTP Request for? 2.What is JSON?nutthanon/...Pro Android S - Day 2 Overview An asynchronous callback-based Http client for Android built on top of Apache’s](https://reader034.vdocuments.net/reader034/viewer/2022052104/603eb838fb205c3965105f7d/html5/thumbnails/1.jpg)
Question for you1. What can you use the HTTP Request for?2. What is JSON?
Pro Android S - Day 2
![Page 2: 1.What can you use the HTTP Request for? 2.What is JSON?nutthanon/...Pro Android S - Day 2 Overview An asynchronous callback-based Http client for Android built on top of Apache’s](https://reader034.vdocuments.net/reader034/viewer/2022052104/603eb838fb205c3965105f7d/html5/thumbnails/2.jpg)
Today's Agenda
1. Integrate HTTP Request to the app2. Parse JSON Data3. Sharing
Pro Android S - Day 2
![Page 3: 1.What can you use the HTTP Request for? 2.What is JSON?nutthanon/...Pro Android S - Day 2 Overview An asynchronous callback-based Http client for Android built on top of Apache’s](https://reader034.vdocuments.net/reader034/viewer/2022052104/603eb838fb205c3965105f7d/html5/thumbnails/3.jpg)
HTTP Request & Response
Pro Android S - Day 2
![Page 4: 1.What can you use the HTTP Request for? 2.What is JSON?nutthanon/...Pro Android S - Day 2 Overview An asynchronous callback-based Http client for Android built on top of Apache’s](https://reader034.vdocuments.net/reader034/viewer/2022052104/603eb838fb205c3965105f7d/html5/thumbnails/4.jpg)
HTTP Operations via Apache HttpComponents
Pro Android S - Day 2
HttpClient client = new DefaultHttpClient();HttpGet getMethod = new HttpGet(url + search);try { ResponseHandler<String> responseHandle = new BasicResponseHandler(); String responseBody= client.execute(getMethod, responseHandle);
} catch (Exception e) {
}
![Page 5: 1.What can you use the HTTP Request for? 2.What is JSON?nutthanon/...Pro Android S - Day 2 Overview An asynchronous callback-based Http client for Android built on top of Apache’s](https://reader034.vdocuments.net/reader034/viewer/2022052104/603eb838fb205c3965105f7d/html5/thumbnails/5.jpg)
Pro Android S - Day 2
OverviewAn asynchronous callback-based Http client for Android built on top of Apache’s HttpClient libraries. All requests are made outside of your app’s main UI thread, but any callback logic will be executed on the same thread as the callback was created using Android’s Handler message passing.
http://loopj.com/android-async-http/
Android Asynchronous Http Client(loopj Library)
![Page 6: 1.What can you use the HTTP Request for? 2.What is JSON?nutthanon/...Pro Android S - Day 2 Overview An asynchronous callback-based Http client for Android built on top of Apache’s](https://reader034.vdocuments.net/reader034/viewer/2022052104/603eb838fb205c3965105f7d/html5/thumbnails/6.jpg)
Features
Pro Android S - Day 2
● Make asynchronous HTTP requests, handle responses in anonymous callbacks
● HTTP requests happen outside the UI thread● Requests use a threadpool to cap concurrent resource usage● GET/POST params builder (RequestParams)● Multipart file uploads with no additional third party libraries● Tiny size overhead to your application, only 19kb for everything● Automatic smart request retries optimized for spotty mobile
connections● Automatic gzip response decoding support for super-fast requests● Optional built-in response parsing
into JSON (JsonHttpResponseHandler)● Optional persistent cookie store, saves cookies into your app’s
SharedPreferences
![Page 7: 1.What can you use the HTTP Request for? 2.What is JSON?nutthanon/...Pro Android S - Day 2 Overview An asynchronous callback-based Http client for Android built on top of Apache’s](https://reader034.vdocuments.net/reader034/viewer/2022052104/603eb838fb205c3965105f7d/html5/thumbnails/7.jpg)
Android Asynchronous Http Client(loopj Library)
Pro Android S - Day 2
AsyncHttpClient client = new AsyncHttpClient(); client.get("http://www.google.com", new AsyncHttpResponseHandler() { @Override public void onSuccess(String response) { System.out.println(response); } });
![Page 8: 1.What can you use the HTTP Request for? 2.What is JSON?nutthanon/...Pro Android S - Day 2 Overview An asynchronous callback-based Http client for Android built on top of Apache’s](https://reader034.vdocuments.net/reader034/viewer/2022052104/603eb838fb205c3965105f7d/html5/thumbnails/8.jpg)
Parsing Responses
Pro Android S - Day 2
● The response you get will be formatted using some system—HTML, XML, JSON, whatever.
● It is up to you, of course, to pick out what information you need and do something useful with it.
![Page 9: 1.What can you use the HTTP Request for? 2.What is JSON?nutthanon/...Pro Android S - Day 2 Overview An asynchronous callback-based Http client for Android built on top of Apache’s](https://reader034.vdocuments.net/reader034/viewer/2022052104/603eb838fb205c3965105f7d/html5/thumbnails/9.jpg)
Parsing JSON Responses
Pro Android S - Day 2
http://www.makathon.com/weather/?weather=bangkok
Relevant classes & methods
● JSONObject● JSONArray
● getJSONObject()● getJSONArray()
● getString()
Weather Forecast APIhttp://www.worldweatheronline.com
![Page 10: 1.What can you use the HTTP Request for? 2.What is JSON?nutthanon/...Pro Android S - Day 2 Overview An asynchronous callback-based Http client for Android built on top of Apache’s](https://reader034.vdocuments.net/reader034/viewer/2022052104/603eb838fb205c3965105f7d/html5/thumbnails/10.jpg)
Parsing XML Responses
Pro Android S - Day 2
www.google.com/ig/api?weather=bangkok
Relevant classes & methods
● getElementsByTagName()● getAttribute()
![Page 11: 1.What can you use the HTTP Request for? 2.What is JSON?nutthanon/...Pro Android S - Day 2 Overview An asynchronous callback-based Http client for Android built on top of Apache’s](https://reader034.vdocuments.net/reader034/viewer/2022052104/603eb838fb205c3965105f7d/html5/thumbnails/11.jpg)
Task 12: Hello Weather
Pro Android S - Day 2
● Create Custom ListView● HTTP Request● Parsing Data● Create Menu
![Page 12: 1.What can you use the HTTP Request for? 2.What is JSON?nutthanon/...Pro Android S - Day 2 Overview An asynchronous callback-based Http client for Android built on top of Apache’s](https://reader034.vdocuments.net/reader034/viewer/2022052104/603eb838fb205c3965105f7d/html5/thumbnails/12.jpg)
Reference
Pro Android S - Day 2
● Apress - Beginning Android, Mark L. Murphy
● Wrox - Professional Android 2 Application development, Reto Meier
![Page 13: 1.What can you use the HTTP Request for? 2.What is JSON?nutthanon/...Pro Android S - Day 2 Overview An asynchronous callback-based Http client for Android built on top of Apache’s](https://reader034.vdocuments.net/reader034/viewer/2022052104/603eb838fb205c3965105f7d/html5/thumbnails/13.jpg)
Questions?