creating network-enabled applications
DESCRIPTION
CSCI 7010 UGA March 23 rd , 2010. Creating Network-Enabled Applications. Ways to Connect. BES/MDS TCP/IP BIS WiFi WAP 2.0 WAP 1.0. BES/MDS. Blackberry Enterprise Server/Mobile Data System used if the Blackberry device is owned by a company and is set up to run through their servers - PowerPoint PPT PresentationTRANSCRIPT
![Page 1: Creating Network-Enabled Applications](https://reader035.vdocuments.site/reader035/viewer/2022070420/56815e54550346895dccd082/html5/thumbnails/1.jpg)
Creating Network-Enabled Applications
CSCI 7010UGAMarch 23rd, 2010
![Page 2: Creating Network-Enabled Applications](https://reader035.vdocuments.site/reader035/viewer/2022070420/56815e54550346895dccd082/html5/thumbnails/2.jpg)
Ways to Connect
BES/MDSTCP/IPBISWiFiWAP 2.0WAP 1.0
![Page 3: Creating Network-Enabled Applications](https://reader035.vdocuments.site/reader035/viewer/2022070420/56815e54550346895dccd082/html5/thumbnails/3.jpg)
BES/MDS
Blackberry Enterprise Server/Mobile Data System used if the Blackberry device is owned
by a company and is set up to run through their servers
lets the Blackberry device make a secure connection to corporate servers
![Page 4: Creating Network-Enabled Applications](https://reader035.vdocuments.site/reader035/viewer/2022070420/56815e54550346895dccd082/html5/thumbnails/4.jpg)
TCP/IP
Transmission Control Protocol/Internet Protocol
a “regular” internet connectionworks with most devices
![Page 5: Creating Network-Enabled Applications](https://reader035.vdocuments.site/reader035/viewer/2022070420/56815e54550346895dccd082/html5/thumbnails/5.jpg)
BIS
Blackberry Internet Service for devices that aren’t under a BES
used to send email Internet connections, but less secure
than BES but you need to be part of the
Blackberry Alliance Program to use it
![Page 6: Creating Network-Enabled Applications](https://reader035.vdocuments.site/reader035/viewer/2022070420/56815e54550346895dccd082/html5/thumbnails/6.jpg)
WiFi 802.11 B/G and sometimes A allows device to connect to network via a
WiFi router device user has to configure device to
connect to the router Pro: better speed, lower latency, no carrier
data charges Con: WiFi coverage less than wireless
network coverage can write app so that it looks for WiFi first
and then falls back to BES or BIS
![Page 7: Creating Network-Enabled Applications](https://reader035.vdocuments.site/reader035/viewer/2022070420/56815e54550346895dccd082/html5/thumbnails/7.jpg)
WAP 2.0
Wireless Access Protocolconnects through wireless carrier’s
WAP gatewayno Blackberry-specific infrastructure
but user’s plan must support WAP 2.0 (most do)
don’t need to configure as with TCP/IP
![Page 8: Creating Network-Enabled Applications](https://reader035.vdocuments.site/reader035/viewer/2022070420/56815e54550346895dccd082/html5/thumbnails/8.jpg)
WAP 1.0
older version of WAPsupported by all Blackberry devicesbut doesn’t support secure
connections as do other methods
![Page 9: Creating Network-Enabled Applications](https://reader035.vdocuments.site/reader035/viewer/2022070420/56815e54550346895dccd082/html5/thumbnails/9.jpg)
So many choices ...
What should you do? If activated on a BES:
use BES/MDS If not activated on a BES:
use WAP 2.0 fall back to TCP/IP
In either case, might want to check for WiFi
![Page 10: Creating Network-Enabled Applications](https://reader035.vdocuments.site/reader035/viewer/2022070420/56815e54550346895dccd082/html5/thumbnails/10.jpg)
The “Service Book”
how the device maintains info about its configuration
records about optional applicationsemail account configurationconnection methods available on a
device
![Page 11: Creating Network-Enabled Applications](https://reader035.vdocuments.site/reader035/viewer/2022070420/56815e54550346895dccd082/html5/thumbnails/11.jpg)
Service Book
open device Options
click Advanced Options Service Book
Demo on the simulator
![Page 12: Creating Network-Enabled Applications](https://reader035.vdocuments.site/reader035/viewer/2022070420/56815e54550346895dccd082/html5/thumbnails/12.jpg)
Service Book records
Two parts: CID – defines the type of record UID – a unique identifier
most connection methods have a record in the service book
![Page 13: Creating Network-Enabled Applications](https://reader035.vdocuments.site/reader035/viewer/2022070420/56815e54550346895dccd082/html5/thumbnails/13.jpg)
The Connector class
javax.microedition.io.Connector used it before to open files can also use it to open network connections
Example: HttpConnection connection = (HttpConnection)
Connector.open(“http://www.cnn.com”); or HttpConnection connection = (HttpConnection)
Connector.open(“http://www.apress.com”);
![Page 14: Creating Network-Enabled Applications](https://reader035.vdocuments.site/reader035/viewer/2022070420/56815e54550346895dccd082/html5/thumbnails/14.jpg)
In general ... Connection_type connectionName= (Connection_type)
Connector.open(URL);Connection_type is some subclass of ConnectionconnectionName is a variable name that you
chooseURL takes the form
scheme://host:port/path[optional parameters]Example URLs:
file://myfile.htmlhttp://www.apress.com:80/book/catalogsocket://www.apress.com:80https://www.amazon.com/gp/flex/sign-in/select.html
![Page 15: Creating Network-Enabled Applications](https://reader035.vdocuments.site/reader035/viewer/2022070420/56815e54550346895dccd082/html5/thumbnails/15.jpg)
HTTP Basics
Hypertext transfer protocol protocol of the World Wide Web connectionless request-response
![Page 16: Creating Network-Enabled Applications](https://reader035.vdocuments.site/reader035/viewer/2022070420/56815e54550346895dccd082/html5/thumbnails/16.jpg)
http client
opens a connection to an http serversends a request message receives the responsedisplays result
![Page 17: Creating Network-Enabled Applications](https://reader035.vdocuments.site/reader035/viewer/2022070420/56815e54550346895dccd082/html5/thumbnails/17.jpg)
http server
listens for a connection from client receives a requestdelivers a responsecloses the connection
![Page 18: Creating Network-Enabled Applications](https://reader035.vdocuments.site/reader035/viewer/2022070420/56815e54550346895dccd082/html5/thumbnails/18.jpg)
http message
initial line (different for request & response)
header lines (zero or more)blank lineoptional message body
![Page 19: Creating Network-Enabled Applications](https://reader035.vdocuments.site/reader035/viewer/2022070420/56815e54550346895dccd082/html5/thumbnails/19.jpg)
initial request line
METHOD path http_version GET /path/to/file/index.html HTTP/1.0
METHOD: GET – “Please send this resource” POST – “Here are some details about it” HEAD – “Just checking some info about it”
PATH: the part of the URL after the host name
![Page 20: Creating Network-Enabled Applications](https://reader035.vdocuments.site/reader035/viewer/2022070420/56815e54550346895dccd082/html5/thumbnails/20.jpg)
initial response line
also know as “status line” HTTP_version status_code reason_phrase
HTTP/1.0 200 OK HTTP/1.1 404 Not Found
status codes: 100s – informational 200s – success of some kind 300s – redirect to another URL 400s – client error 500s – server error
![Page 21: Creating Network-Enabled Applications](https://reader035.vdocuments.site/reader035/viewer/2022070420/56815e54550346895dccd082/html5/thumbnails/21.jpg)
The Sample Application
Let’s run it from a web browser first ...
![Page 22: Creating Network-Enabled Applications](https://reader035.vdocuments.site/reader035/viewer/2022070420/56815e54550346895dccd082/html5/thumbnails/22.jpg)
NetworkingApplication.javapackage com.beginningblackberry.networking;
import net.rim.device.api.ui.UiApplication;
public class NetworkingApplication extends UiApplication {
public NetworkingApplication() {NetworkingMainScreen scr = new NetworkingMainScreen();pushScreen(scr);}
public static void main(String[] args) {NetworkingApplication app = new NetworkingApplication();app.enterEventDispatcher();}
}
![Page 23: Creating Network-Enabled Applications](https://reader035.vdocuments.site/reader035/viewer/2022070420/56815e54550346895dccd082/html5/thumbnails/23.jpg)
NetworkingMainScreen.javapackage com.beginningblackberry.networking;import net.rim.device.api.ui.container.MainScreen;
public class NetworkingMainScreen extends MainScreen {private EditField urlField;private BitmapField imageOutputField;private RichTextField textOutputField;
.... methods on following slides
}
![Page 24: Creating Network-Enabled Applications](https://reader035.vdocuments.site/reader035/viewer/2022070420/56815e54550346895dccd082/html5/thumbnails/24.jpg)
Constructorpublic NetworkingMainScreen() {
setTitle("Networking");urlField = new EditField("URL:", "");textOutputField = new RichTextField();imageOutputField = new BitmapField();
add(urlField); add(new SeparatorField());
postDataField = new EditField("Post data:", "");add(postDataField); add(new SeparatorField());
add(new LabelField("Image retrieved:"));add(imageOutputField); add(new SeparatorField());add(new LabelField("Text retrieved:"));add(textOutputField);
}
![Page 25: Creating Network-Enabled Applications](https://reader035.vdocuments.site/reader035/viewer/2022070420/56815e54550346895dccd082/html5/thumbnails/25.jpg)
Creating a menuprotected void makeMenu(Menu menu, int instance) {
super.makeMenu(menu, instance);menu.add(new MenuItem("Get", 10, 10) {
public void run() {getURL();} }
);menu.add(new MenuItem("Post", 10, 10) {
public void run() {postURL();}}
);menu.add(new MenuItem("Socket Get", 10, 10) {
public void run() {socketGet();}}
);}
![Page 26: Creating Network-Enabled Applications](https://reader035.vdocuments.site/reader035/viewer/2022070420/56815e54550346895dccd082/html5/thumbnails/26.jpg)
Threads
http://www.purpletech.com/talks/Threads.ppt
More on multi-threading (if you’re interested):
http://www.cs.uga.edu/~eileen/Concurrency_tutorials
![Page 27: Creating Network-Enabled Applications](https://reader035.vdocuments.site/reader035/viewer/2022070420/56815e54550346895dccd082/html5/thumbnails/27.jpg)
MenuItem, in more detailYou can learn about any component
by looking at the API documentation.
See:http://www.blackberry.com/developers/docs/4.1api/index.htmlto learn more about the MenuItem component
![Page 28: Creating Network-Enabled Applications](https://reader035.vdocuments.site/reader035/viewer/2022070420/56815e54550346895dccd082/html5/thumbnails/28.jpg)
When the user clicks “Get”private void getURL() {
HttpRequestDispatcher dispatcher = new HttpRequestDispatcher(urlField.getText(), "GET",
this);dispatcher.start();
}
... creates a new thread for the network-related operation
... and starts it up
![Page 29: Creating Network-Enabled Applications](https://reader035.vdocuments.site/reader035/viewer/2022070420/56815e54550346895dccd082/html5/thumbnails/29.jpg)
HttpRequestDispatcherpackage com.beginningblackberry.networking;
/* * Class to handle creating the request, sending it off, * and receiving the response */
public class HttpRequestDispatcher extends Thread { private String url; private String method; // GET or POST private NetworkingMainScreen screen; private byte[] postData;...
}
![Page 30: Creating Network-Enabled Applications](https://reader035.vdocuments.site/reader035/viewer/2022070420/56815e54550346895dccd082/html5/thumbnails/30.jpg)
Constructors public HttpRequestDispatcher(String url, String method, NetworkingMainScreen screen) { this.url = url; this.method = method; this.screen = screen; }
public HttpRequestDispatcher(String url, String method,NetworkingMainScreen screen, byte[] postData) {
this.url = url; this.method = method; this.screen = screen; this.postData = postData; }
![Page 31: Creating Network-Enabled Applications](https://reader035.vdocuments.site/reader035/viewer/2022070420/56815e54550346895dccd082/html5/thumbnails/31.jpg)
run() – simple versionpublic void run(){try{HttpConnection connection = (HttpConnection) Connector.open(url);
int responseCode = connection.getResponseCode();
if (responseCode != HttpConnection.HTTP_OK){screen.requestFailed(“Unexpected response code: “ + responseCode);
connection.close(); return; }...
![Page 32: Creating Network-Enabled Applications](https://reader035.vdocuments.site/reader035/viewer/2022070420/56815e54550346895dccd082/html5/thumbnails/32.jpg)
run() – simple version, continuedString contentType =
connection.getHeaderField(“Content-type”);ByteArrayOutputStream baos = new ByteArrayOutputStream();
InputStream responseData = connection.openInputStream();byte[] buffer = new byte[10000];
int bytesRead = responseData.read(buffer);while (bytesRead > 0){
baos.write(buffer, 0, bytesRead);bytesRead = responseData.read(buffer);
}baos.close();connection.close();screen.requestSucceeded(baos.toByteArray(), contentType);} catch (IOException ex){ screen.requestFailed(ex.toString());}}
![Page 33: Creating Network-Enabled Applications](https://reader035.vdocuments.site/reader035/viewer/2022070420/56815e54550346895dccd082/html5/thumbnails/33.jpg)
requestSucceeded(...)public void requestSucceeded(byte[] result, String contentType) {if (contentType.equals("image/png") ||
contentType.equals("image/jpeg") ||contentType.equals("image/gif")) {
Bitmap bitmap = Bitmap.createBitmapFromBytes(result, 0, result.length, 1);
synchronized (UiApplication.getEventLock()) {imageOutputField.setBitmap(bitmap);}
}else if (contentType.startsWith("text/")) {
String strResult = new String(result);synchronized (UiApplication.getEventLock()) {textOutputField.setText(strResult);}
}else {
synchronized (UiApplication.getEventLock()) {Dialog.alert("Unknown content type: " + contentType);}
}}
![Page 34: Creating Network-Enabled Applications](https://reader035.vdocuments.site/reader035/viewer/2022070420/56815e54550346895dccd082/html5/thumbnails/34.jpg)
requestFailed(...)public void requestFailed(final String message) {UiApplication.getUiApplication().invokeLater(new Runnable() {
public void run() {Dialog.alert("Request failed. Reason: " + message);
}});
}
}
![Page 35: Creating Network-Enabled Applications](https://reader035.vdocuments.site/reader035/viewer/2022070420/56815e54550346895dccd082/html5/thumbnails/35.jpg)
...
.... and we’ve skipped some gory detail for now
![Page 36: Creating Network-Enabled Applications](https://reader035.vdocuments.site/reader035/viewer/2022070420/56815e54550346895dccd082/html5/thumbnails/36.jpg)
Let’s run the web application from a browser again ...See:
http://beginningblackberry.appspot.com
Enter some words in the form – apple berry cinnamon doughnut
returns doughnut cinnamon berry apple
![Page 37: Creating Network-Enabled Applications](https://reader035.vdocuments.site/reader035/viewer/2022070420/56815e54550346895dccd082/html5/thumbnails/37.jpg)
Two-way interaction:Sending data with HTTP POST What’s in the web application?
<html> <head><title>Form</title></head>
<body><img src="img/apress_logo.png" /><br /> <form action="/" method="POST"><br /> <input type="text"
name="content"></input> <input type="submit" value="Go!"/>
</form> </body></html>
![Page 38: Creating Network-Enabled Applications](https://reader035.vdocuments.site/reader035/viewer/2022070420/56815e54550346895dccd082/html5/thumbnails/38.jpg)
<form>
<form action="/" method="POST">- defines a form that the browser uses
to send data to the web application- send data to the ULR “/” (the base
URL )- using HTTP POST
![Page 39: Creating Network-Enabled Applications](https://reader035.vdocuments.site/reader035/viewer/2022070420/56815e54550346895dccd082/html5/thumbnails/39.jpg)
<input>
<input type="text" name="content"></input>
defines the text boxgives it the name “content”
the application expects the content to be something like:
content = ONE+TWO+THREE“+” interpreted as a space
![Page 40: Creating Network-Enabled Applications](https://reader035.vdocuments.site/reader035/viewer/2022070420/56815e54550346895dccd082/html5/thumbnails/40.jpg)
<input type="submit" value="Go!"/> defines the “Go” button as invoking
POST
</form> indicates end of the form
![Page 41: Creating Network-Enabled Applications](https://reader035.vdocuments.site/reader035/viewer/2022070420/56815e54550346895dccd082/html5/thumbnails/41.jpg)
Modified HttpRequestDispatcherprivate byte[] postData;- to pass the POST body to the dispatcher
public HttpRequestDispatcher(String url, String method, NetworkingMainScreen screen, byte[] postData){
this.url = url;this.method = method;this.screen = screen;this.postData = postData;
}- constructor to initialize
![Page 42: Creating Network-Enabled Applications](https://reader035.vdocuments.site/reader035/viewer/2022070420/56815e54550346895dccd082/html5/thumbnails/42.jpg)
check for POST data before sending
if (method.equals(“POST”) && postData != null){connection.setRequestProperty(“Content-type”, “application/x-www-form-urlencoded”);
OutputStream requestOutput = connection.openOutputStream();
requestOutput.write(postData);requestOutput.close();
}
![Page 43: Creating Network-Enabled Applications](https://reader035.vdocuments.site/reader035/viewer/2022070420/56815e54550346895dccd082/html5/thumbnails/43.jpg)
postURL
private void postURL(){ String postString = postDataField.getText();
URLEncodedPostData encodedData = new URLEncodedPostData(null, false);
encodedData.append(“content”, postString);
HttpRequestDispatcher dispatcher = new HttpRequestDispatcher(urlField.getText(), “
POST”, this, encodedData.getBytes());
dispatcher.start();}
![Page 44: Creating Network-Enabled Applications](https://reader035.vdocuments.site/reader035/viewer/2022070420/56815e54550346895dccd082/html5/thumbnails/44.jpg)
... stop here today ...
![Page 45: Creating Network-Enabled Applications](https://reader035.vdocuments.site/reader035/viewer/2022070420/56815e54550346895dccd082/html5/thumbnails/45.jpg)
HEAD
let’s look at the code
![Page 46: Creating Network-Enabled Applications](https://reader035.vdocuments.site/reader035/viewer/2022070420/56815e54550346895dccd082/html5/thumbnails/46.jpg)
Socket Basics
explanation ... and let’s look at the code ...
![Page 47: Creating Network-Enabled Applications](https://reader035.vdocuments.site/reader035/viewer/2022070420/56815e54550346895dccd082/html5/thumbnails/47.jpg)
private void socketGet() { SocketConnector connector = new
SocketConnector(urlField.getText(), this); connector.start(); }
private void postURL() { String postString = postDataField.getText(); URLEncodedPostData encodedData = new
URLEncodedPostData(null, false); encodedData.append("content", postString); HttpRequestDispatcher dispatcher = new
HttpRequestDispatcher(urlField .getText(), "POST", this, encodedData.getBytes()); dispatcher.start(); }
![Page 48: Creating Network-Enabled Applications](https://reader035.vdocuments.site/reader035/viewer/2022070420/56815e54550346895dccd082/html5/thumbnails/48.jpg)