1 / 32

Working with Webservices (Cloud Services)

Working with Webservices (Cloud Services). Nilanjan Banerjee. University of Maryland, Baltimore County Baltimore, MD nilanb@umbc.edu. Intro to Mobile Computing. Some of the simple web based services you would use!. Writing data to a database. Querying data from a database.

donnan
Télécharger la présentation

Working with Webservices (Cloud Services)

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Working with Webservices (Cloud Services) Nilanjan Banerjee University of Maryland, Baltimore County Baltimore, MD nilanb@umbc.edu Intro to Mobile Computing

  2. Some of the simple web based services you would use! Writing data to a database Querying data from a database Map queries (already learned about it) (extracting maps, geocoding, reverse geocoding)

  3. Other webservices we will learn about… places API Street View API google messaging service

  4. Usually two ways of accessing webservices over Http/Https HttpGet HttpPost What are the differences?

  5. { "glossary": { "title": "example glossary","GlossDiv": { "title": "S","GlossList": { "GlossEntry": { "ID": "SGML","SortAs": "SGML","GlossTerm": "Standard Generalized Markup Language","Acronym": "SGML","Abbrev": "ISO 8879:1986","GlossDef": { "para": "A meta-markup language, used to create markup languages such as DocBook.","GlossSeeAlso": ["GML", "XML"] },"GlossSee": "markup" } } } }} Formats in which data is sent back to you json (JSONObject) { "glossary": { "title": "example glossary", "GlossDiv": { "title": "S",” GlossList": { "GlossEntry": { "ID": "SGML", "SortAs": "SGML", "GlossTerm": "Standard Generalized Markup Language", "Acronym": "SGML", "Abbrev": "ISO 8879:1986", "GlossDef": { "para": "A meta-markup language, used to create markup", "GlossSeeAlso": ["GML", "XML"] }, "GlossSee": "markup" } } } } }

  6. { "glossary": { "title": "example glossary","GlossDiv": { "title": "S","GlossList": { "GlossEntry": { "ID": "SGML","SortAs": "SGML","GlossTerm": "Standard Generalized Markup Language","Acronym": "SGML","Abbrev": "ISO 8879:1986","GlossDef": { "para": "A meta-markup language, used to create markup languages such as DocBook.","GlossSeeAlso": ["GML", "XML"] },"GlossSee": "markup" } } } }} Formats in which data is sent back to you xml (XmlPullParser) <!DOCTYPE glossary PUBLIC "-//OASIS//DTD DocBook V3.1//EN"> <glossary><title>example glossary</title> <GlossDiv><title>S</title> <GlossList> <GlossEntry ID="SGML" SortAs="SGML"> <GlossTerm>Standard Generalized Markup Language</GlossTerm> <Acronym>SGML</Acronym> <Abbrev>ISO 8879:1986</Abbrev> <GlossDef> <para>A meta-markup language,</para> <GlossSeeAlsoOtherTerm="GML"> <GlossSeeAlsoOtherTerm="XML"> </GlossDef> <GlossSeeOtherTerm="markup"> </GlossEntry> </GlossList> </GlossDiv> </glossary>

  7. General architecture for accessing databases Intermediary Script (php, perl etc) Backend database Mobile Phone

  8. Sending simple text data from a Mobile phone (Server side) • Adding userinfo --- username and password $firstname = $_REQUEST["firstname"]; $lastname = $_REQUEST["lastname"]; $username = $_REQUEST["username"]; $fpassword = $_REQUEST["fpassword"]; require_once('db.inc.php'); $insertquery="INSERT INTO table_name (first_name, last_name, user_name, fpassword) VALUES ('$firstname', '$lastname', '$username', '$fpassword’)"; $result = mysql_query($insertquery); mysql_close(); • - db.inc.php <? $user=”XXX"; $password=”YYY"; $database="weedidapp"; $host="mpss.csce.uark.edu"; mysql_connect($host,$user,$password); @mysql_select_db($database) or die( "Unable to select database"); ?>

  9. Authenticating userinfo (server side) • Check that the username password is present in the database • Note that this is all in plaintext. Ideally you would create a MD5 hash of the password and store the hash <?php foreach ($_GET as $key => $value) { eval("\$" . $key . " = \"" . $value . "\";");} $username = $_REQUEST["username"]; $fpassword = $_REQUEST["fpassword"]; require_once('db.inc.php'); $query="SELECT * from table where user_name='$username' AND fpassword='$fpassword'"; $result = mysql_query($query); if($result && mysql_numrows($result)>0) echo "y”; else echo "n”; mysql_close(); ?>

  10. Adding text to a mysql database • Send the data encoded in the url • For e.g., http://www.csce.uark.edu/~nilanb/insertdb.php?latitude=“-31.5”;longitude=“-94.6” • The url is parsed by the php script and a hashtable with <key, value> pairs are extraced • _REQUEST[“latitude”] = -31.5 • _REQUEST[“longitude”] = -94.6 $latitude = $_REQUEST[”latitude"]; $longitude = $_REQUEST[”longitude"]; require_once('db.inc.php'); $insertquery="INSERT INTO table_name (latitude, longitude) VALUES (’$latitude', '$longitude’)"; $result = mysql_query($insertquery); mysql_close();

  11. Retrieving text from a mysql server and sending it back mysql_select_db("locationgame",$con); $list = mysql_list_tables("locationgame"); $i = 0; $idarray = array(); $latarray = array(); $longarray = array(); $count = 0; while($i < mysql_num_rows($list)) { $tb_names[$i] = mysql_tablename($list,$i); $sql = "SELECT * FROM $tb_names[$i] order by Date desc limit 1"; $result = mysql_query($sql, $con); $num = mysql_numrows($result); $j = 0; while($j < $num) { $fielddate=mysql_result($result,$j,"Date"); $fieldlatitude = mysql_result($result, $j, "Latitude"); $fieldlongitude = mysql_result($result, $j, "Longitude"); $phpdate = strtotime($fielddate); $dist = distance($fieldlatitude, $fieldlongitude, $latitude, $longitude); $idarray[] = $tb_names[$i]; $latarray[] = $fieldlatitude; $longarray[] = $fieldlongitude; $count ++; $j++; } $i++; } for ($i = 0; $i < $count; $i++) {print "$idarray[$i] ”; print "$latarray[$i] ”; print "$longarray[$i] ”; print "\n”;} mysql_close($con);

  12. Code on the phone httpclient = new DefaultHttpClient(); 91                     httppost = new HttpPost(php_script); 92                     // Add your data 93                     nameValuePairs = new ArrayList<NameValuePair>(2); 94                    nameValuePairs.add(newBasicNameValuePair("UserEmail", name.trim())); 95                     nameValuePairs.add(newBasicNameValuePair("Password", pass.trim())); 96                     httppost.setEntity(newUrlEncodedFormEntity(nameValuePairs)); 97   98                     // Execute HTTP Post Request 99                     response = httpclient.execute(httppost); 100                     inputStream = response.getEntity().getContent(); 101   102                     data = new byte[256]; 103   104                     buffer = new StringBuffer(); 105                     intlen = 0; 106                     while (-1 != (len = inputStream.read(data)) ) 107                     { 108                         buffer.append(newString(data, 0, len)); 109                     } 110   111                     inputStream.close();

  13. Uploading images to a mysql server enter where image is stored and its Characteristics (size etc) database php script store Image in a folder Name of image and attributes Send actual images folder Phone

  14. Schematic for upload of images String (Base64 encoded) String byte binary (Base64 decoded) ByteArray OutputStream image Bitmap Image Server Android

  15. Uploading images to the server <?php $base=$_REQUEST['image']; echo $base;// base64 encoded utf-8 string $binary=base64_decode($base);// binary, utf-8 bytes header('Content-Type: bitmap; charset=utf-8’); $file = fopen('test.jpg', 'wb'); fwrite($file, $binary);fclose($file); echo '<imgsrc=test.jpg>'; ?>

  16. Sending images from the phone 29             byte [] byte_arr = stream.toByteArray(); 30             String image_str = Base64.encodeBytes(byte_arr); 31             ArrayList<NameValuePair> nameValuePairs = new  ArrayList<NameValuePair>(); 32   33             nameValuePairs.add(newBasicNameValuePair("image",image_str)); 34   35             try{ 36                 HttpClienthttpclient = new DefaultHttpClient(); 37                 HttpPosthttppost = new HttpPost(php_script); 38                 httppost.setEntity(newUrlEncodedFormEntity(nameValuePairs)); 39                 HttpResponse response = httpclient.execute(httppost);                  String the_string_response = convertResponseToString(response);              }catch(Exceptione){ }

  17. Sending images from the phone (Response)               String res = ""; StringBuffer buffer = new StringBuffer(); inputStream = response.getEntity().getContent(); intcontentLength = (int) response.getEntity().getContentLength(); 55               if (contentLength < 0){ 56              } 57              else{ 58                     byte[] data = new byte[512]; 59                     intlen = 0; 60                     try 61                     { 62                         while (-1 != (len = inputStream.read(data)) ) 63                         { 64                             buffer.append(newString(data, 0, len)); 65                         } 66                     } 67                     catch (IOExceptione)                     {} 71                     try 72                     { 73                         inputStream.close(); 74                     } 75                     catch (IOExceptione)                     {}                      res = buffer.toString();   . 83              } 84              return res; 85         }

  18. Difference between AsyncTask and Headless Fragment • A Fragment which does not have a UI • onCreateView() should return a null • Headless fragments are a way of implementing background tasks • Usually you define a headless fragment and start an AsyncTask in a headless fragment • Advantage of using a AsyncTask in a headless fragment: It provides the ability to save state of the AsyncTask in case an activity is restarted (e.g., when the orientation of the phone is changed. You have to set setRetainInstance (true)

  19. More sophisticated web service (Google Places) • Search for place within a radius of a latitude and longitude • Simple text searches, just like Google searches to return places • Restaurants in Baltimore • Radar Searches • 200 places based on your query with little detail • Place Actions • Add actions using crowdsourcing • Place Events • Add events that you see around you, again adding information using crowdsourcing • Place Photos • Read-only API where you can download photographs based on queries.

  20. Lets delve deeper into the Places API • First Step is authentication • Create a API key for your project • The API key is used for all authentication with the Google Place Service • All requests are made over Https (Http over SSL) • We have talked about Http requests in the last lecture • We will chat about Https in this lecture • First step is to check the API using a standard web-browser.

  21. Place Search • Nearby Search • Required parameters: key, location (latitude, longitude), radius, sensor (true or false), rankby=distance https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=-33.8670522,151.1957362&radius=500&types=food&name=harbour&sensor=false&key=AddYourOwnKeyHere Lets take a look at the json results • Text Search • Required parameters: key, query string, sensor (true or false) https://maps.googleapis.com/maps/api/place/textsearch/json?query=restaurants+in+Baltimore&sensor=true&key=<Addyourkey> • Radar Search • Required parameters: key, location, radius, sensor https://maps.googleapis.com/maps/api/place/radarsearch/json?location=51.503186,-0.126446&radius=5000&types=museum&sensor=false&key=AddYourOwnKeyHere

  22. Place Details • Once you have access to the reference id of a place, you can ask for more information on the place • Required parameters: key, reference_id, sensor = (true, false) https://maps.googleapis.com/maps/api/place/details/json?reference=CmRYAAAAciqGsTRX1mXRvuXSH2ErwW-jCINE1aLiwP64MCWDN5vkXvXoQGPKldMfmdGyqWSpm7BEYCgDm-iv7Kc2PF7QA7brMAwBbAcqMr5i1f4PwTpaovIZjysCEZTry8Ez30wpEhCNCXpynextCld2EBsDkRKsGhSLayuRyFsex6JA6NPh9dyupoTH3g&sensor=true&key=AddYourOwnKeyHere Lets take a look at the json results

  23. How do we use this webservice from an Android App Step 1: Create a HttpClient that supports Https Each machine has a trustStore (a trustStore has the set of certificates from trusted servers) You define a Https Socket port number Underlying protocol parameters

  24. Place Details (Https client) public class MyHttpClient extends DefaultHttpClient { final Context context; public static HttpClientgetNewHttpClient() {try { KeyStoretrustStore = KeyStore.getInstance(KeyStore.getDefaultType()); trustStore.load(null, null); SSLSocketFactorysf = new MySSLSocketFactory(trustStore);sf.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER); HttpParamsparams = new BasicHttpParams(); HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1); HttpProtocolParams.setContentCharset(params, HTTP.UTF_8); SchemeRegistryregistry = new SchemeRegistry(); registry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80)); registry.register(new Scheme("https", sf, 443)); ClientConnectionManagerccm = new ThreadSafeClientConnManager(params, registry); return new DefaultHttpClient(ccm, params); } catch (Exception e) { return new DefaultHttpClient(); } } public MyHttpClient(Context context) {this.context= context; } }

  25. How do we use this webservice from an Android App Step 2: Parsing the Json output/Xml output Json Array JSON parser JSON object • Lets take an example

  26. Speech to Text using RecognizerIntent Google speech engine Compressed byte array Text Microphone

  27. Tradeoffs of using webservices • Energy consumption • Remember you are using the Wi-Fi or 3G radio to transfer data to a backend webservice and download information from a webservice • Bandwidth caps • People usually have caps on the amount of data that they can transfer for a given amount of money for a month • Latency • 3G uses a shared medium. Speed can be an issue if you are building a multiplayer game

  28. Other Google services • Google Drive API: add files to google drive • Need OS2 client ID for authentication • Google cloud messaging API • Used for push notifications for applications • Google+ API • Search API for shopping • Google Prediction API: machine learning API • Diagnostics • Document and email classifications • Recommendation systems

  29. Google Cloud Messaging Service • Used to send messages from a third party server to an Android application • Basis of push notifications • Allows third party applications to send small messages or larger payloads upto 4kb asynchronously from a third party server • Substitute for polling the server to get data • An android application does not need to be awake to receive the message • The system will wake up the application to provide the message given that the proper broadcast receiver and permissions. • Phone must have installed Google Play Services.

  30. Architecture (Demo next lecture) • 3rd party servers send messages to the Google GCM servers • The Google server enqueues and stores messages in case the device is offline • When the device is online, the GCM server sends over the message to the client • On the device, the system broadcasts the message to the specified Android application via Intent broadcast with proper permission • The application processes the message.

  31. Midterm review Basic Android Concepts • What is an activity? What is an Intent? • What is an implicit and explicit Intent? • Start new activities • What is a broadcast receiver? • Android lifecycle Sensors and GPS units • How the locationManager and sensorManager works? • Different sensors, hardware sensors, and virtual sensors • Accelerometers, compass, gyroscope, orientation sensor • Localization (using GPS unit, using Network)

  32. Midterm review UI concepts • Fragments • How do Fragments communicate amongst each other • What are the lifecycle functions for Fragments • Different types of layouts, views, and widgets. Maps and webservices • Simple custom webservices that access a backend database

More Related