Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Client-Server

5,782 views

Published on

Published in: Education, Technology
  • Be the first to comment

Client-Server

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

×