Writing a Web Service Client App for Android

31
Writing a Web Service Client App for Android Copyright © 2010-2012, Wolf Paulus - http://wolfpaulus.com - A Tech Casita Production. All rights reserved.

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 of Writing a Web Service Client App for Android

Page 1: Writing a Web Service Client App for Android

Writing a Web Service Client App for Android

Copyright © 2010-2012, Wolf Paulus - http://wolfpaulus.com - A Tech Casita Production. All rights reserved.

Page 2: Writing a Web Service Client App for Android

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.

Page 3: Writing a Web Service Client App for Android

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.

Page 4: Writing a Web Service Client App for Android

Copyright © 2010-2012, Wolf Paulus - http://wolfpaulus.com - A Tech Casita Production. All rights reserved.

Page 5: Writing a Web Service Client App for Android

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.

Page 6: Writing a Web Service Client App for Android

Exposing a Service Class: Hessian Binary Web Service Protocol

Copyright © 2010-2012, Wolf Paulus - http://wolfpaulus.com - A Tech Casita Production. All rights reserved.

Page 7: Writing a Web Service Client App for Android

Copyright © 2010-2012, Wolf Paulus - http://wolfpaulus.com - A Tech Casita Production. All rights reserved.

Page 8: Writing a Web Service Client App for Android

Java Hessian

Copyright © 2010-2012, Wolf Paulus - http://wolfpaulus.com - A Tech Casita Production. All rights reserved.

Page 9: Writing a Web Service Client App for Android

Java Hessian

Copyright © 2010-2012, Wolf Paulus - http://wolfpaulus.com - A Tech Casita Production. All rights reserved.

Page 10: Writing a Web Service Client App for Android

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.

Page 11: Writing a Web Service Client App for Android

<?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.

Page 12: Writing a Web Service Client App for Android

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.

Page 13: Writing a Web Service Client App for Android

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.

Page 14: Writing a Web Service Client App for Android

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.

Page 15: Writing a Web Service Client App for Android

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.

Page 16: Writing a Web Service Client App for Android

HessianHttpServlet extends HessianServlet implements HessianHttpService {

... }

... not in hessian.jar

Copyright © 2010-2012, Wolf Paulus - http://wolfpaulus.com - A Tech Casita Production. All rights reserved.

Page 17: Writing a Web Service Client App for Android

.. 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.

Page 18: Writing a Web Service Client App for Android

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.

Page 19: Writing a Web Service Client App for Android

= Client

Copyright © 2010-2012, Wolf Paulus - http://wolfpaulus.com - A Tech Casita Production. All rights reserved.

Page 20: Writing a Web Service Client App for Android
Page 21: Writing a Web Service Client App for Android

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.

Page 22: Writing a Web Service Client App for Android

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.

Page 23: Writing a Web Service Client App for Android

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.

Page 24: Writing a Web Service Client App for Android

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.

Page 25: Writing a Web Service Client App for Android

Hessian Client

CommService cs = HessianFactory.create( CommService.class, getClassLoader() )

Copyright © 2010-2012, Wolf Paulus - http://wolfpaulus.com - A Tech Casita Production. All rights reserved.

Page 26: Writing a Web Service Client App for Android

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.

Page 27: Writing a Web Service Client App for Android

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.

Page 28: Writing a Web Service Client App for Android

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.

Page 29: Writing a Web Service Client App for Android

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.

Page 30: Writing a Web Service Client App for Android

Unlocking Android

Android Wireless Application Development

Page 31: Writing a Web Service Client App for Android

Thanks For Coming!

http://twitter.com/wolfpaulus