Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Connecting to Web Services on Android

145,600 views

Published on

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

Published in: Technology, Education
  • Hello! Get Your Professional Job-Winning Resume Here - Check our website! https://vk.cc/818RFv
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
  • 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

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

×