Connecting to Web Services



            Sean Sullivan
    Android Dev Camp Amsterdam
           8 January 2009
+
•   HTTP
•   XML
•   JSON
•   OAuth
HTTP request


XML response
http://code.google.com/p/jfireeagle
jfireeagle uses:
     • HttpClient API
     • XStream
     • OAuth Java library
“An open protocol to allow secure API
authorization in a simple and standard method
from desktop and web applications”
• use OAuth to access protected data
• your application needs to obtain an access
  token from the service provider
• serv...
+




http://code.google.com/p/oauth
+


jfireeagle

   http://code.google.com/p/jfireeagle/wiki/Android

jpoco

    http://code.google.com/p/jpoco/wiki/Android
+ HTTP
Option 1
  • java.net.URL
  • java.net.HttpURLConnection
Option 2
  • HttpClient 4.0 API
HttpClient 4.0 API


•   org.apache.http.client.HttpClient
•   org.apache.http.impl.client.DefaultHttpClient
HttpClient 4.0 API

• org.apache.http.client.methods.HttpGet
• org.apache.http.client.methods.HttpPost
• org.apache.http.c...
HttpClient 4.0 API

•   org.apache.http.HttpResponse
•   org.apache.http.StatusLine
•   org.apache.http.HttpEntity
•   org...
HttpClient programming

• HTTP response status code
• I/O exceptions
• timeouts
• gzip?
HttpClient programming

synchronous invocation
  public HttpResponse execute(HttpUriRequest request)




asynchronous invo...
Android UI thread

• user clicks on button
• onClick listener
• Don’t block the UI thread!
• Tip: use java.lang.Runnable
Response formats

• XML
• JSON
• RSS, Atom
• other
XML processing
• org.w3c.dom package (DOM)
• org.xml.sax package (SAX)
• org.xmlpull package (XPP3)
• XStream
• JDOM
• Apa...
JSON processing
• org.json package
• Google GSON library   *


• XStream + Jettison
                   *


• json-lib
    ...
Client libraries
• code.google.com/p/jfireeagle
• code.google.com/p/jpoco
• code.google.com/p/meetup-java-client
• code.goo...
Conclusion

• HttpClient API
• use XStream for XML processing
• use org.json package for JSON processing
• OAuth has a lea...
Thank you
Resources

•   http://code.google.com/android/documentation.html

•   http://hc.apache.org/httpcomponents-client

•   http...
Connecting to Web Services on Android
Connecting to Web Services on Android
Connecting to Web Services on Android
Upcoming SlideShare
Loading in...5
×

Connecting to Web Services on Android

140,871

Published on

Connecting to Web Services on Android
Android Dev Camp Amsterdam
January 2009

Published in: Technology, Education
14 Comments
54 Likes
Statistics
Notes
  • http://sidharthkumargth.github.io/UserConnection or https://github.com/sidharthkumargth/UserConnection download this userconnection.jar from above link and import to your library EXAMPLE: public class MainActivity extends Activity implements ConnectionStatusListener { UserConnection uc,uc1; ImageView iv; TextView tv; ProgressBar pb; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); getActionBar().setSubtitle("Only for test"); iv=(ImageView)findViewById(R.id.imageView1); tv=(TextView)findViewById(R.id.ttt); pb=(ProgressBar)findViewById(R.id.progressBar1); uc=new UserConnection(2); // here 2 is number of objects sent to web service email and password, if the number of objects is n then it will be uc=new UserConnection(n); uc.setHTTPMethod("POST"); uc.setupValues("email", "test@gmail.com"); uc.setupValues("password", "test"); uc.setConnectionStatusListener(this); //uc.startConnection("http://test.com/tb/login.php"); //uc.startDownloadfrom("http://www.kingsoftstore.com/images/presentation-templates/report-ppt-template-059.dpt","/sdcard/downloadedfile.jpg"); //uc.startDownloadImage("http://i00.i.aliimg.com/wsphoto/v0/1157302568/2013-girls-new-style-popular-purple-sexy-lingeries-LC1105-free-shipping.jpg"); uc.startDownloadfrom("http://www.technotrigger.com/wp-content/uploads/2014/01/house-in-green-field.jpg","/sdcard/downloadedfile1.jpg"); uc.startDownloadfrom("http://upload.wikimedia.org/wikipedia/commons/3/36/Hopetoun_falls.jpg","/sdcard/downloadedfile2.jpg"); uc.startDownloadfrom("http://upload.wikimedia.org/wikipedia/commons/1/1a/Bachalpseeflowers.jpg","/sdcard/downloadedfile3.jpg"); uc.startDownloadfrom("http://tmacfitness.com/wp-content/uploads/2013/04/Beauty-of-nature-random-4884759-1280-800.jpg","/sdcard/downloadedfile4.jpg"); uc.startDownloadfrom("http://freedwallpaper.com/wp-content/uploads/2014/03/6-autumn-nature-sandbox-images_2053316.jpg","/sdcard/downloadedfile5.jpg"); //uc.startMultipartConnection("http://www.google.co.in"); uc1=new UserConnection(0); uc1.setHTTPMethod("GET"); //"/sdcard/downloadedfile.jpg" uc1.setConnectionStatusListener(this); uc1.startConnection("http://www.facebook.com"); } //Toast.makeText(getApplicationContext(), message, 1).show(); @Override public void onError(Status arg0) { // TODO Auto-generated method stub try { Toast.makeText(getApplicationContext(), "error"+arg0.getErrorMessage(), 1).show(); } catch (UserException e) { // TODO Auto-generated catch block e.printStackTrace(); } } @Override public void onStart(Status arg0) { // TODO Auto-generated method stub try{ if(arg0.getConnectionHolder()==uc){ Toast.makeText(getApplicationContext(), "uc"+arg0.getMessage(), 1).show(); }else if(arg0.getConnectionHolder()==uc1){ Toast.makeText(getApplicationContext(), "uc1"+arg0.getMessage(), 1).show(); } }catch(UserException e){} } @Override public void onStop(Status arg0) { // TODO Auto-generated method stub //Log.d(arg0.getConnectionHolder().toString(), arg0.getResponse()+""); try { iv.setImageDrawable(Drawable.createFromPath(arg0.getFile())); //Toast.makeText(getApplicationContext(), arg0.getConnectionHolder()+"++"+arg0.getResponse(), Toast.LENGTH_LONG).show(); } catch (UserException e) { // TODO Auto-generated catch block e.printStackTrace(); } } @Override public void onProgressUpdate(Status arg0) { // TODO Auto-generated method stub try { tv.setText("Progress "+arg0.getProgress()); pb.setProgress(arg0.getProgress()); } catch (UserException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
  • thank you vary much ,this knowledge is very useful :-* ...
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
  • mantapp
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
  • gud
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
  • Thank you
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
No Downloads
Views
Total Views
140,871
On Slideshare
0
From Embeds
0
Number of Embeds
19
Actions
Shares
0
Downloads
3,324
Comments
14
Likes
54
Embeds 0
No embeds

No notes for slide

Connecting to Web Services on Android

  1. 1. Connecting to Web Services Sean Sullivan Android Dev Camp Amsterdam 8 January 2009
  2. 2. +
  3. 3. • HTTP • XML • JSON • OAuth
  4. 4. HTTP request XML response
  5. 5. http://code.google.com/p/jfireeagle
  6. 6. jfireeagle uses: • HttpClient API • XStream • OAuth Java library
  7. 7. “An open protocol to allow secure API authorization in a simple and standard method from desktop and web applications”
  8. 8. • use OAuth to access protected data • your application needs to obtain an access token from the service provider • service provider will issue access token after the user grants permission
  9. 9. + http://code.google.com/p/oauth
  10. 10. + jfireeagle http://code.google.com/p/jfireeagle/wiki/Android jpoco http://code.google.com/p/jpoco/wiki/Android
  11. 11. + HTTP Option 1 • java.net.URL • java.net.HttpURLConnection Option 2 • HttpClient 4.0 API
  12. 12. HttpClient 4.0 API • org.apache.http.client.HttpClient • org.apache.http.impl.client.DefaultHttpClient
  13. 13. HttpClient 4.0 API • org.apache.http.client.methods.HttpGet • org.apache.http.client.methods.HttpPost • org.apache.http.client.methods.HttpPut • org.apache.http.client.methods.HttpDelete
  14. 14. HttpClient 4.0 API • org.apache.http.HttpResponse • org.apache.http.StatusLine • org.apache.http.HttpEntity • org.apache.http.utils.EntityUtils
  15. 15. HttpClient programming • HTTP response status code • I/O exceptions • timeouts • gzip?
  16. 16. HttpClient programming synchronous invocation public HttpResponse execute(HttpUriRequest request) asynchronous invocation public HttpResponse execute(HttpUriRequest request, ResponseHandler handler)
  17. 17. Android UI thread • user clicks on button • onClick listener • Don’t block the UI thread! • Tip: use java.lang.Runnable
  18. 18. Response formats • XML • JSON • RSS, Atom • other
  19. 19. XML processing • org.w3c.dom package (DOM) • org.xml.sax package (SAX) • org.xmlpull package (XPP3) • XStream • JDOM • Apache XmlBeans * * does not work on Android 1.0
  20. 20. JSON processing • org.json package • Google GSON library * • XStream + Jettison * • json-lib * * does not work on Android 1.0
  21. 21. Client libraries • code.google.com/p/jfireeagle • code.google.com/p/jpoco • code.google.com/p/meetup-java-client • code.google.com/p/upcoming-java-client • code.google.com/p/gdata-java-client • flickrj.sourceforge.net
  22. 22. Conclusion • HttpClient API • use XStream for XML processing • use org.json package for JSON processing • OAuth has a learning curve • OAuth affects user experience
  23. 23. Thank you
  24. 24. Resources • http://code.google.com/android/documentation.html • http://hc.apache.org/httpcomponents-client • http://code.google.com/p/oauth • http://xstream.codehaus.org
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×