Working with Webservices (Cloud Services) Nilanjan Banerjee University of Maryland, Baltimore County Baltimore, MD nilanb@umbc.edu Intro to Mobile Computing 1 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) Other webservices we will learn about… places API Street View API google messaging service Usually two ways of accessing webservices over Http/Https HttpGet HttpPost What are the differences? 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" } } } } } 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> <GlossSeeAlso OtherTerm="GML"> <GlossSeeAlso OtherTerm="XML"> </GlossDef> <GlossSee OtherTerm="markup"> </GlossEntry> </GlossList> </GlossDiv> </glossary> General architecture for accessing databases Intermediary Script (php, perl etc) Mobile Phone Backend database 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"); ?> 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(); ?> 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(); 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); Code on the phone 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 httpclient = new DefaultHttpClient(); httppost = new HttpPost(php_script); // Add your data nameValuePairs = new ArrayList<NameValuePair>(2); nameValuePairs.add(new BasicNameValuePair("UserEmail", name.trim())); nameValuePairs.add(new BasicNameValuePair("Password", pass.trim())); httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); // Execute HTTP Post Request response = httpclient.execute(httppost); inputStream = response.getEntity().getContent(); data = new byte[256]; buffer = new StringBuffer(); int len = 0; while (-1 != (len = inputStream.read(data)) ) { buffer.append(new String(data, 0, len)); } inputStream.close(); 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 Phone Send actual images folder Schematic for upload of images String (Base64 encoded) String byte ByteArray OutputStream binary (Base64 decoded) image Bitmap Image Android Server 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 '<img src=test.jpg>'; ?> Sending images from the phone 29 30 31 32 33 34 35 36 37 38 39 40 42 byte [] byte_arr = stream.toByteArray(); String image_str = Base64.encodeBytes(byte_arr); ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(); nameValuePairs.add(new BasicNameValuePair("image",image_str)); try{ HttpClient httpclient = new DefaultHttpClient(); HttpPost httppost = new HttpPost(php_script); httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); HttpResponse response = httpclient.execute(httppost); String the_string_response = convertResponseToString(response); }catch(Exception e){ } Sending images from the phone (Response) 53 55 56 57 58 59 60 61 62 63 64 65 66 67 68 71 72 73 74 75 76 79 83 84 85 String res = ""; StringBuffer buffer = new StringBuffer(); inputStream = response.getEntity().getContent(); int contentLength = (int) response.getEntity().getContentLength(); if (contentLength < 0){ } else{ byte[] data = new byte[512]; int len = 0; try { while (-1 != (len = inputStream.read(data)) ) { buffer.append(new String(data, 0, len)); } } catch (IOException e) {} try { inputStream.close(); } catch (IOException e) {} res = buffer.toString(); . } return res; } 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) 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. 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. 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=AddYo urOwnKeyHere 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 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=CmRYAAAAciqGsTRX1mXRv uXSH2ErwW-jCINE1aLiwP64MCWDN5vkXvXoQGPKldMfmdGyqWSpm7BEYCgDmiv7Kc2PF7QA7brMAwBbAcqMr5i1f4PwTpaovIZjysCEZTry8Ez30wpEhCNCXpynextCld2EBsDkRKsGhSLay uRyFsex6JA6NPh9dyupoTH3g&sensor=true&key=AddYourOwnKeyHere Lets take a look at the json results 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) port number Underlying protocol parameters You define a Https Socket Place Details (Https client) public class MyHttpClient extends DefaultHttpClient { final Context context; public static HttpClient getNewHttpClient() { try { KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType()); trustStore.load(null, null); SSLSocketFactory sf = new MySSLSocketFactory(trustStore); sf.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER); HttpParams params = new BasicHttpParams(); HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1); HttpProtocolParams.setContentCharset(params, HTTP.UTF_8); SchemeRegistry registry = new SchemeRegistry(); registry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80)); registry.register(new Scheme("https", sf, 443)); ClientConnectionManager ccm = new ThreadSafeClientConnManager(params, registry); return new DefaultHttpClient(ccm, params); } catch (Exception e) { return new DefaultHttpClient(); } } public MyHttpClient(Context context) { this.context = context; } } 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 Speech to Text using RecognizerIntent Google speech engine Compressed byte array Text Microphone 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 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 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. 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. 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) 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