Writing a Web ServiceClient App forAndroid  Copyright © 2010-2012, Wolf Paulus - http://wolfpaulus.com - A Tech Casita Pro...
Server Side (e.g. Tomcat)        Hessian, Binary Protocol        Http-Session SupportClient        hessdroid        remote...
Quick Tips         Use Android’s Resources ( R.class )         Auto-Launch an Intent at boot timeClient         Things tha...
Copyright © 2010-2012, Wolf Paulus - http://wolfpaulus.com - A Tech Casita Production. All rights reserved.
Switch                                               Activity                                               Remote        ...
Exposing a Service Class:Hessian Binary Web Service Protocol   Copyright © 2010-2012, Wolf Paulus - http://wolfpaulus.com ...
Copyright © 2010-2012, Wolf Paulus - http://wolfpaulus.com - A Tech Casita Production. All rights reserved.
Java                                                                 Hessian       Copyright © 2010-2012, Wolf Paulus - ht...
Hessian                                                             Java Copyright © 2010-2012, Wolf Paulus - http://wolfp...
package com.carlsbadcubes.api;import java.util.Collection;public interface CommService {    Collection<Message> getMessage...
<?xml version="1.0" encoding="UTF-8"?><web-app>  <servlet>                                           defined in hessian.jar...
(1)                                               Thread                                                Thread            ...
JSESSIONID = 123                                              Thread                                                      ...
JSESSIONID = 123                               123                                              Thread                    ...
The ThreadLocal Class:public class ThreadLocal<T> {  public void set( T newValue );  public T get();  public void remove()...
... not in hessian.jarHessianHttpServlet extends HessianServlet    implements HessianHttpService {                    ...}...
.. all of                                                                                HessianServletHessianHttpServlet ...
@Override                  HessianHttpServletinit() .. // // introspect service implementation to find out if we have to pu...
= ClientCopyright © 2010-2012, Wolf Paulus - http://wolfpaulus.com - A Tech Casita Production. All rights reserved.
hessdroidhttp://code.google.com/p/hessdroid/String url = “http://hostname.domain:port/service”;ServiceProxyFactory factory...
hessdroid                                                                             http://code.google.com/p/hessdroid/H...
hessdroid                                                                       http://code.google.com/p/hessdroid/       ...
... not in hessdroid.jar HessianFactory.initialize( )                                   Service Discovery:                ...
Hessian                                                                                  ClientCommService cs = HessianFac...
Demo              Tomcat Server with a Hessian Service               and a Hessian based Android ClientCopyright © 2010-20...
Async ....Don’t run HessionFactory.create(..) in the main threadDon’t run remote method calls in the main thread either.  ...
Interface Garage {  void openDoor();  Car getCar();}Interface Car {  void openDoor();  Color getColor();}     Copyright © ...
Garage garage =HessianFactory.create( Garage.class, getClassLoader() );// The interface Garage and the class CarImpl// nee...
Unlocking Android            Android Wireless Application Development
http://twitter.com/wolfpaulusThanks For Coming!
Writing a Web Service Client App for Android
Upcoming SlideShare
Loading in …5
×

Writing a Web Service Client App for Android

12,925 views

Published on

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.

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
12,925
On SlideShare
0
From Embeds
0
Number of Embeds
3,142
Actions
Shares
0
Downloads
115
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Writing a Web Service Client App for Android

  1. 1. Writing a Web ServiceClient App forAndroid Copyright © 2010-2012, Wolf Paulus - http://wolfpaulus.com - A Tech Casita Production. All rights reserved.
  2. 2. Server Side (e.g. Tomcat) Hessian, Binary Protocol Http-Session SupportClient hessdroid remote calls / async AgendaCopyright © 2010-2012, Wolf Paulus - http://wolfpaulus.com - A Tech Casita Production. All rights reserved.
  3. 3. Quick Tips Use Android’s Resources ( R.class ) Auto-Launch an Intent at boot timeClient Things that can will go wrong AgendaCopyright © 2010-2012, Wolf Paulus - http://wolfpaulus.com - A Tech Casita Production. All rights reserved.
  4. 4. Copyright © 2010-2012, Wolf Paulus - http://wolfpaulus.com - A Tech Casita Production. All rights reserved.
  5. 5. Switch Activity Remote call reload Switch users Activity Four item main menu Remote Call to getNextCopyright © 2010-2012, Wolf Paulus - http://wolfpaulus.com - A Tech Casita Production. All rights reserved.
  6. 6. Exposing a Service Class:Hessian Binary Web Service Protocol Copyright © 2010-2012, Wolf Paulus - http://wolfpaulus.com - A Tech Casita Production. All rights reserved.
  7. 7. Copyright © 2010-2012, Wolf Paulus - http://wolfpaulus.com - A Tech Casita Production. All rights reserved.
  8. 8. Java Hessian Copyright © 2010-2012, Wolf Paulus - http://wolfpaulus.com - A Tech Casita Production. All rights reserved.
  9. 9. Hessian Java Copyright © 2010-2012, Wolf Paulus - http://wolfpaulus.com - A Tech Casita Production. All rights reserved.
  10. 10. 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.
  11. 11. <?xml version="1.0" encoding="UTF-8"?><web-app> <servlet> defined in hessian.jar <servlet-name>myservice</servlet-name> <servlet-class>com.caucho.hessian.server.HessianServlet</servlet-class> <init-param> Implementation <param-name>service-class</param-name> <param-value>com.carlsbadcubes.impl.CommServiceImpl</param-value> </init-param> <init-param> Interface <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> No Session</web-app> Copyright © 2010-2012, Wolf Paulus - http://wolfpaulus.com - A Tech Casita Production. All Noreserved. rights Discovery
  12. 12. (1) Thread Thread Servlethttp requests : (1) : m_var created in servlet.init() (n) (1) Thread ServiceClass impl. ServiceAPI Copyright © 2010-2012, Wolf Paulus - http://wolfpaulus.com - A Tech Casita Production. All rights reserved.
  13. 13. JSESSIONID = 123 Thread 357 request. 789 JSESSIONID = 789 getSession() Thread 123 Servlet Sessionhttp response : Map : m_var JSESSIONID = 357 Thread ServiceClass impl. ServiceAPI Copyright © 2010-2012, Wolf Paulus - http://wolfpaulus.com - A Tech Casita Production. All rights reserved.
  14. 14. JSESSIONID = 123 123 Thread ThreadLocal 357 789 JSESSIONID = 789 789 Thread 123 ThreadLocal Servlet Sessionhttp request : Maphttp response : m_var JSESSIONID = 357 357 Thread ServiceClass ThreadLocal impl. ServiceAPI Copyright © 2010-2012, Wolf Paulus - http://wolfpaulus.com - A Tech Casita Production. All rights reserved.
  15. 15. 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.
  16. 16. ... not in hessian.jarHessianHttpServlet extends HessianServlet implements HessianHttpService { ...} Copyright © 2010-2012, Wolf Paulus - http://wolfpaulus.com - A Tech Casita Production. All rights reserved.
  17. 17. .. all of HessianServletHessianHttpServlet extends HessianServlet implements HessianHttpServiceputs HttpSession Discovery Service forMap into ThreadLocal Android Clients memory Map<String, String> getServices() Copyright © 2010-2012, Wolf Paulus - http://wolfpaulus.com - A Tech Casita Production. All rights reserved.
  18. 18. @Override HessianHttpServletinit() .. // // 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.
  19. 19. = ClientCopyright © 2010-2012, Wolf Paulus - http://wolfpaulus.com - A Tech Casita Production. All rights reserved.
  20. 20. hessdroidhttp://code.google.com/p/hessdroid/String url = “http://hostname.domain:port/service”;ServiceProxyFactory factory = new HessianProxyFactory();factory.set ..(..);factory.set ..(..).CommService cs = factory.create( CommService.class, url , getClassLoader()); Copyright © 2010-2012, Wolf Paulus - http://wolfpaulus.com - A Tech Casita Production. All rights reserved.
  21. 21. hessdroid http://code.google.com/p/hessdroid/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 ); Copyright © 2010-2012, Wolf Paulus - http://wolfpaulus.com - A Tech Casita Production. All rights reserved.
  22. 22. hessdroid http://code.google.com/p/hessdroid/ Creates a Cookie Map, Maintains HttpSessions HessianHttpProxyFactory extends HessianProxyFactory { ... }Copyright © 2010-2012, Wolf Paulus - http://wolfpaulus.com - A Tech Casita Production. All rights reserved.
  23. 23. ... not in hessdroid.jar HessianFactory.initialize( ) Service Discovery: maps Interfaces to URLsCopyright © 2010-2012, Wolf Paulus - http://wolfpaulus.com - A Tech Casita Production. All rights reserved.
  24. 24. Hessian ClientCommService cs = HessianFactory.create( CommService.class, getClassLoader() ) Copyright © 2010-2012, Wolf Paulus - http://wolfpaulus.com - A Tech Casita Production. All rights reserved.
  25. 25. Demo Tomcat Server with a Hessian Service and a Hessian based Android ClientCopyright © 2010-2012, Wolf Paulus - http://wolfpaulus.com - A Tech Casita Production. All rights reserved.
  26. 26. 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.
  27. 27. 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.
  28. 28. 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.
  29. 29. Unlocking Android Android Wireless Application Development
  30. 30. http://twitter.com/wolfpaulusThanks For Coming!

×