1. Android Application Development Android and Internet Ahsanul Karim ahsanul.karim@sentinelbd.com Sentinel Solutions Ltd. http://www.sentinelbd.com
2. Connecting to the Internet Http Connection Example In this example we’ll see: How to use HttpClient object to send http requests How to get response data and show them For manipulating HTTP requests/responses in an Android environment, the standard classes from the java.net package can be used. Thus, classes such as URL, URLConnection, HttpURLConnection etc., can all be used in the known way.
3. Connecting to the Internet Http Connection Example We’ll create a simple project to see HTTP Connection Example We create a project with following: Project Name: HttpConnectionDemo Build Target: 1.6 Application name:HttpConnectionDemo Package name: com.basistraining.httpdemo Create Activity: HttpConnectionDemoActivity Min SDK Version: 4 Add INTERNET permission
4. Connecting to the Internet Http Connection Example We’ll start with a class named HttpRetriever. In this class method: String retrieve (String url) will do the work
5. Connecting to the Internet Http Connection Example Now from our main Activity, we’ll use this class. To input the url we make a layout
6. Connecting to the Internet Http Connection Example Now we start with UI components
7. Connecting to the Internet Http Connection Example On button’s action, we add:
9. Connecting to the Internet Http Connection Example Now we need to add some checks for internet connection
10. Connecting to the Internet How to connect MySQL DB on Web server We have: a database named httptest a table named users with following fields Id (integer) name (VARCHAR (50)) email (VARCHAR (50)) lat (VARCHAR (50)) long (VARCHAR (50)) We keep a very simple code in server side- connect to the database- run an SQL query- output it in JSON format
11. Connecting to the Internet How to connect MySQL DB on Web server As response we get JSON data: [ {“id”:”1”, "name":"john", "email":"john@a.com", "lat":"23.4567", "long":"90.86777"}, {“id”:”2”, "name":"smith", "email":"smith@b.com", "lat":"23.6767", "long":"90.986765"} ]