3. Mobile Is Different
✦Network is unreliable
✦iOS Prohibits HTTP and insecure communication
✦Sockets are possible but might be unreliable
✦Server sockets are difficult and less portable
✦WebSockets are rising
6. 3 Options
c.addResponseListener(e -> {
byte[] response = e.getConnectionRequest().getResponseData();
// do something with the response data
});
ConnectionRequest c = new ConnectionRequest(url, false) {
@Override
protected void readResponse(InputStream input) throws IOException {
// read input stream
}
};
addToQueueAndWait(c);
byte[] response = c.getResponseData();
// do something with the response data
7. Builtin Sockets
Socket.connect(server, port, new SocketConnection() {
@Override
public void connectionError(int errorCode, String message) {
Log.p("Error connecting " + errorCode + ": " + message);
}
@Override
public void connectionEstablished(InputStream is, OutputStream os) {
while(connected) {
// read and write to input/output stream
}
}
});
8. WebSockets
WebSocket sock = new WebSocket(websocketUrl) {
@Override
protected void onOpen() {
// we can start using the websocker
}
@Override
protected void onMessage(String string) {
// received string message from server
}
@Override
protected void onMessage(byte[] bytes) {
}
@Override
protected void onError(Exception excptn) {
Log.e(excptn);
}
};
sock.connect()
9. URL
✦You can port code that uses java.net.URL to use
com.codename1.io.URL
✦You will need to handle threading on your own
✦There is no builtin support for events on the event
dispatch thread
✦You might inadvertently rely on low level platform
specific behaviors since URL is low level
10. WebServices
✦WebService wizard can generate server client stubs
for remote method call over HTTP
✦ConnectionRequest can be used to invoke typical
restful webservices
✦Several cn1lib’s and builtin API’s exist to simplify
webservice calls e.g. com.codename1.io.rest.Rest
✦JSONParser & XMLParser support parsing common
formats the Result class supports XPath expressions