Working with Webservices (Cloud Services)

Post on 15-Jan-2016

32 views 0 download

description

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. - PowerPoint PPT Presentation

Transcript of Working with Webservices (Cloud Services)

1

Working with Webservices (Cloud Services)

Nilanjan Banerjee

Intro to Mobile Computing

University of Maryland, Baltimore County

Baltimore, MDnilanb@umbc.edu

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

Mobile Phone

Intermediary Script (php, perl etc)

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();

<?$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");?>

- db.inc.php

Authenticating userinfo (server side)

<?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();?>

– 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

Adding text to a mysql database

$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();

– 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

Retrieving text from a mysql server and sending it backmysql_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

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

Uploading images to a mysql server

php script

Phone

database

folderName of image and attributes

enter where image is stored and itsCharacteristics (size etc)

Send actual images

storeImage in a folder

Schematic for upload of images

Image

Bitmap

ByteArrayOutputStream

byte

String (Base64 encoded) String

binary (Base64 decoded)

image

AndroidServer

Uploading images to the server

<?php$base=$_REQUEST['image'];echo $base;// base64 encoded utf-8 string$binary=base64_decode($base);// binary, utf-8 bytesheader('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             byte [] byte_arr = stream.toByteArray();30             String image_str = Base64.encodeBytes(byte_arr);31             ArrayList<NameValuePair> nameValuePairs = new  ArrayList<NameValuePair>();32  33             nameValuePairs.add(new BasicNameValuePair("image",image_str));34  35             try{36                 HttpClient httpclient = new DefaultHttpClient();37                 HttpPost httppost = new HttpPost(php_script);38                 httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));39                 HttpResponse response = httpclient.execute(httppost);40                  String the_string_response = convertResponseToString(response);42              }catch(Exception e){ }                                

Sending images from the phone (Response)

              String res = "";          StringBuffer buffer = new StringBuffer();               inputStream = response.getEntity().getContent();

53               int contentLength = (int) response.getEntity().getContentLength(); 55               if (contentLength < 0){56              }57              else{58                     byte[] data = new byte[512];59                     int len = 0;60                     try61                     {62                         while (-1 != (len = inputStream.read(data)) )63                         {64                             buffer.append(new String(data, 0, len)); 65                         }66                     }67                     catch (IOException e)68                     {}71                     try72                     {73                         inputStream.close(); 74                     }75                     catch (IOException e)76                     {}79                      res = buffer.toString();   .83              }84              return res;85         }                           

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=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, sensorhttps://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=CmRYAAAAciqGsTRX1mXRvuXSH2ErwW-jCINE1aLiwP64MCWDN5vkXvXoQGPKldMfmdGyqWSpm7BEYCgDm-iv7Kc2PF7QA7brMAwBbAcqMr5i1f4PwTpaovIZjysCEZTry8Ez30wpEhCNCXpynextCld2EBsDkRKsGhSLayuRyFsex6JA6NPh9dyupoTH3g&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)

You define a Https Socket

port number

Underlying protocol parameters

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 ArrayJSON parser

JSON object

Lets take an example

Speech to Text using RecognizerIntent

Google speech engine

Compressed byte array

Microphone

Text

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