writing a web service client app for android
DESCRIPTION
While most Android Phones are relatively powerful devices, writing a Web Service client for a small memory constrained device requires some careful consideration when it comes to selecting the right framework and tools. We will use one of the fasted binary web service protocols to exchange objects between client and server, without requiring a large framework. This is not your typical Android 101 talk but even if you haven’t done a lot of Android or Embedded Programming yet, this should still be a lot fun.TRANSCRIPT
Writing a Web Service Client App for Android
Copyright © 2010-2012, Wolf Paulus - http://wolfpaulus.com - A Tech Casita Production. All rights reserved.
Agenda
Server Side (e.g. Tomcat)
Hessian, Binary Protocol
Http-Session Support
Client
hessdroid
remote calls / async
Copyright © 2010-2012, Wolf Paulus - http://wolfpaulus.com - A Tech Casita Production. All rights reserved.
Quick Tips
Use Android’s Resources ( R.class )
Auto-Launch an Intent at boot time
Client
Things that can will go wrong
AgendaCopyright © 2010-2012, Wolf Paulus - http://wolfpaulus.com - A Tech Casita Production. All rights reserved.
Copyright © 2010-2012, Wolf Paulus - http://wolfpaulus.com - A Tech Casita Production. All rights reserved.
Switch Activity
Switch Activity
Remote call
reload users
Remote Call to getNext
Four item main menu
Copyright © 2010-2012, Wolf Paulus - http://wolfpaulus.com - A Tech Casita Production. All rights reserved.
Exposing a Service Class: Hessian Binary Web Service Protocol
Copyright © 2010-2012, Wolf Paulus - http://wolfpaulus.com - A Tech Casita Production. All rights reserved.
Copyright © 2010-2012, Wolf Paulus - http://wolfpaulus.com - A Tech Casita Production. All rights reserved.
Java Hessian
Copyright © 2010-2012, Wolf Paulus - http://wolfpaulus.com - A Tech Casita Production. All rights reserved.
Java Hessian
Copyright © 2010-2012, Wolf Paulus - http://wolfpaulus.com - A Tech Casita Production. All rights reserved.
package com.carlsbadcubes.api;
import java.util.Collection;
public interface CommService { Collection<Message> getMessages();
Collection<User> getUsers();}
Copyright © 2010-2012, Wolf Paulus - http://wolfpaulus.com - A Tech Casita Production. All rights reserved.
<?xml version="1.0" encoding="UTF-8"?><web-app>
<servlet> <servlet-name>myservice</servlet-name> <servlet-class>com.caucho.hessian.server.HessianServlet</servlet-class>
<init-param> <param-name>service-class</param-name> <param-value>com.carlsbadcubes.impl.CommServiceImpl</param-value> </init-param>
<init-param> <param-name>api-class</param-name> <param-value>com.carlsbadcubes.api.CommService</param-value> </init-param>
</servlet>
<servlet-mapping> <servlet-name>myservice</servlet-name> <url-pattern>/service/*</url-pattern> </servlet-mapping>
</web-app>
Interface
Implementation
defined in hessian.jar
No SessionNo DiscoveryCopyright © 2010-2012, Wolf Paulus - http://wolfpaulus.com - A Tech Casita Production. All rights reserved.
Servlet
Thread
Thread
Thread
m_var
http requests
ServiceClassimpl.
ServiceAPI
created in servlet.init()
(1)
(1)
(1)
(n)
::
Copyright © 2010-2012, Wolf Paulus - http://wolfpaulus.com - A Tech Casita Production. All rights reserved.
http response
JSESSIONID = 123
JSESSIONID = 789
JSESSIONID = 357
SessionMap
123
789357
Servlet
Thread
Thread
Thread
m_var
ServiceClassimpl.
ServiceAPI
::
request.getSession()
Copyright © 2010-2012, Wolf Paulus - http://wolfpaulus.com - A Tech Casita Production. All rights reserved.
http response
JSESSIONID = 123
JSESSIONID = 789
JSESSIONID = 357
SessionMap
123
789357
http request Servlet
Thread
Thread
Thread
m_var
ServiceClassimpl.
ServiceAPI
::
123ThreadLocal
789ThreadLocal
357ThreadLocal
Copyright © 2010-2012, Wolf Paulus - http://wolfpaulus.com - A Tech Casita Production. All rights reserved.
The ThreadLocal Class:
public class ThreadLocal<T> { public void set( T newValue ); public T get(); public void remove(); protected T initialValue();}
Copyright © 2010-2012, Wolf Paulus - http://wolfpaulus.com - A Tech Casita Production. All rights reserved.
HessianHttpServlet extends HessianServlet implements HessianHttpService {
... }
... not in hessian.jar
Copyright © 2010-2012, Wolf Paulus - http://wolfpaulus.com - A Tech Casita Production. All rights reserved.
.. all of HessianServlet
Discovery Service for Android Clients
Map<String, String> getServices()
puts HttpSession Map into ThreadLocal
memory
HessianHttpServlet extends HessianServlet implements HessianHttpService
Copyright © 2010-2012, Wolf Paulus - http://wolfpaulus.com - A Tech Casita Production. All rights reserved.
HessianHttpServlet@Overrideinit() .. // // introspect service implementation to find out if we have to put the servlet into a session // if ( getInitParameter("service-class") != null ) { String className = getInitParameter("service-class"); if (className != null) { this.requiresSessionSupport = StateKeeper.class.isAssignableFrom(loadClass(className)); } if (this.requiresSessionSupport) { System.out.println(className + " has been registered for session support."); } }
@Overrideservice() ..
if (this.requiresSessionSupport) { HttpServletRequest req = (HttpServletRequest) request; ThreadLocalAttrMap.set( req.getSession( true ) ); }
Copyright © 2010-2012, Wolf Paulus - http://wolfpaulus.com - A Tech Casita Production. All rights reserved.
= Client
Copyright © 2010-2012, Wolf Paulus - http://wolfpaulus.com - A Tech Casita Production. All rights reserved.
String url = “http://hostname.domain:port/service”;
ServiceProxyFactory factory = new HessianProxyFactory();factory.set ..(..);factory.set ..(..).
CommService cs = factory.create( CommService.class, url , getClassLoader());
hessdroidhttp://code.google.com/p/hessdroid/
Copyright © 2010-2012, Wolf Paulus - http://wolfpaulus.com - A Tech Casita Production. All rights reserved.
HessDroid Generified ...HessianProxyFactory:public <T>T create( Class<T> api,
String urlName, ClassLoader loader);
Clientside code without casting:Service s = (Service) factory.create( Service.class, url, cl );
hessdroidhttp://code.google.com/p/hessdroid/
Copyright © 2010-2012, Wolf Paulus - http://wolfpaulus.com - A Tech Casita Production. All rights reserved.
Creates a Cookie Map,Maintains HttpSessions
HessianHttpProxyFactory extends HessianProxyFactory {
... }
hessdroidhttp://code.google.com/p/hessdroid/
Copyright © 2010-2012, Wolf Paulus - http://wolfpaulus.com - A Tech Casita Production. All rights reserved.
Service Discovery:maps Interfaces to URLs
HessianFactory.initialize( )
... not in hessdroid.jar
Copyright © 2010-2012, Wolf Paulus - http://wolfpaulus.com - A Tech Casita Production. All rights reserved.
Hessian Client
CommService cs = HessianFactory.create( CommService.class, getClassLoader() )
Copyright © 2010-2012, Wolf Paulus - http://wolfpaulus.com - A Tech Casita Production. All rights reserved.
DemoTomcat Server with a Hessian Service and a Hessian based Android Client
Copyright © 2010-2012, Wolf Paulus - http://wolfpaulus.com - A Tech Casita Production. All rights reserved.
Async ....
Don’t run HessionFactory.create(..) in the main threadDon’t run remote method calls in the main thread either.
Copyright © 2010-2012, Wolf Paulus - http://wolfpaulus.com - A Tech Casita Production. All rights reserved.
Interface Garage { void openDoor();
Car getCar();}
Interface Car {void openDoor();Color getColor();
}
Copyright © 2010-2012, Wolf Paulus - http://wolfpaulus.com - A Tech Casita Production. All rights reserved.
Garage garage = HessianFactory.create( Garage.class, getClassLoader() );
// The interface Garage and the class CarImpl// need to be available on the client to make this work:
garage.openDoor(); // remote callgarage.getCar().openDoor(); // local call
Copyright © 2010-2012, Wolf Paulus - http://wolfpaulus.com - A Tech Casita Production. All rights reserved.
Unlocking Android
Android Wireless Application Development
Thanks For Coming!
http://twitter.com/wolfpaulus