Android nad RESTEM - Skeleton

1 view
Skip to first unread message

kali

unread,
Dec 14, 2011, 10:37:36 AM12/14/11
to CTU VIA
Ahoj spoluzacky a spoluzaci,

napsal jsem pro android pripitomeleho REST klienta. muzete se jim
nechat inspirovat - spolu se serverem z meho predchoziho postu by Vam
mely pomoct rozchodit zakladni klient-server prostredi - lokalne a
rychle debugovatelne a deployovatelne na GAE.

doufam ze se najde nekdo, komu to pomuze! zdravi,
petr a Eager Programmers


nize je zkopirovano to, co je napsano v README ktere je v rootu
projektu:

Petr Kalina: A REST client skeleton
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Usage:
~~~~~~

- to be run against the server skeleton:
https://github.com/petrkalina/via2011/tree/master/devel/samples/gae-jdo-spring-rest-skeleton
- necessary configuration steps to enable debugging on USB connected
tablet agains server run in "DevAppServer" from Google Plugin (Eclipse
+ Jetty)
* make sure the firewall is not blocking the port you use for server
http iface
* bind the server to your actual IP (not to loopback interface) using
--address=YOUR_IP commandline argument for the "DevAppServer" (Eclipse
-> run configurations -> choose the server-skeleton project ->
Arguments..)
* enable USB Debugging on your device (Settings -> Applications ->
Development)
* replace the YOUR_SERVER_URL_NO_END_SLASH in SkeletonActivity with
your actual IP

Notes:
~~~~~~

- The client uses standardized Spring RestTemplate adapter to invoke
HTTP methods and to plug-in marshalling/unmarshalling for json. Read
more at:
* http://www.springsource.org/spring-android
* http://static.springsource.org/spring-android/docs/1.0.x/reference/html/rest-template.html
* http://static.springsource.org/spring/docs/3.0.x/javadoc-api/org/springframework/web/client/RestTemplate.html

- Marshalling/demarshalling is done via GSon library (http://
code.google.com/p/google-gson/) - in above posts you can see some
alternatives

- To my knowledge RestTemplate does not support any status codes
returned from DELETE and PUT methods. I'm not sure why. Should you
want to rigorously implement correct status passing and parsing, you
may be interested in doing so according to:
* http://www.josecgomez.com/2010/04/30/android-accessing-restfull-web-services-using-json/
- also using spring/httpclient but on lower levels..

!!!!!WARNING!!!!!
- there is one UGLY and INCORRECT hack in this application. note the
// DISABLE policy not allowing network connections from main
application thread
// these calls normally HAVE to be done from worker threads/
asynchronous threads/services ..
StrictMode.ThreadPolicy policy = new
StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
lines at the beginning of the SkeletonActivity - in fact it is not
possible to issue network requests from apps main thread. I did it for
the sake of simplicity, but you'll have to move it to separate
threads..


Integration HOWTO:
~~~~~~~~~~~~~~~~~~

- to my knowledge there are just a few more things to note when
integrating this approach to your application. I may not remember all
of the steps I made :-)
* not the
<uses-permission android:name="android.permission.INTERNET"></uses-
permission>
<uses-permission
android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-
permission>
the second is probably not necessary, but the first is VITAL for the
phone to allow for internet calls


GUI:
~~~~

- is idiotic - SEE THE CODE!!!!
- in lower part you have some snippets from the requests being sent
- in upper part you have some snippets from the responses being
received

IT'S a PROOF OF CONCEPT :-)

Reply all
Reply to author
Forward
0 new messages