Android Application Development Cookbook by Wei-Meng Lee
Author:Wei-Meng Lee
Language: eng
Format: epub
Publisher: John Wiley & Sons, Inc.
Published: 2012-12-17T16:00:00+00:00
In the OpenHttpPOSTConnection() method, you make use of the HttpPost class to add headers that you need to send to the server. In particular, you need to set the HOST and Content-Type headers. You also need to send the currency information using the setEntity() method of the HttpPost object. Here, the data (FromCurrency and ToCurrency) is passed in as a list of NameValuePair objects.
As in the previous recipe, you call the OpenHttpPOSTConnection() method in the DownloadText() method so that it can download the returned result:
private String DownloadText(String URL) { int BUFFER_SIZE = 2000; InputStream in = null; try { in = OpenHttpPOSTConnection(URL); } catch (Exception e) { Log.d("Networking", e.getLocalizedMessage()); return ""; } InputStreamReader isr = new InputStreamReader(in); int charRead; String str = ""; char[] inputBuffer = new char[BUFFER_SIZE]; try { while ((charRead = isr.read(inputBuffer)) > 0) { // ---convert the chars to a String--- String readString = String .copyValueOf(inputBuffer, 0, charRead); str += readString; inputBuffer = new char[BUFFER_SIZE]; } in.close(); } catch (IOException e) { Log.d("DownloadText", e.getLocalizedMessage()); return ""; } return str; }
You also need to call the DownloadText() method in a separate thread, so wrap it using a subclass of the AsyncTask class:
private class DownloadTextTask extends AsyncTask<String, Void, String> { protected String doInBackground(String... urls) { return DownloadText(urls[0]); } @Override protected void onPostExecute(String result) { Toast.makeText(getBaseContext(), result, Toast.LENGTH_LONG).show(); Log.d("DownloadTextTask", result); } }
Finally, call the web service using the DownloadTextTask class using the following statement:
//---using HTTP POST--- new DownloadTextTask().execute("http://www.webservicex.net/ CurrencyConvertor.asmx/ConversionRate");
NOTE Remember that you need to have the INTERNET permission in the AndroidManifest.xml file.
Download
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.
Secrets of the JavaScript Ninja by John Resig & Bear Bibeault(6532)
Secrets of the JavaScript Ninja by John Resig Bear Bibeault(6413)
Kotlin in Action by Dmitry Jemerov(5062)
Odoo 15 Development Essentials - Fifth Edition by Daniel Reis & Greg Mader(3349)
Odoo 15 Development Essentials by Daniel Reis(2814)
React Native - Building Mobile Apps with JavaScript by Novick Vladimir(2531)
Learning Angular - Second Edition by Christoffer Noring(2358)
Pride and Prejudice by Jane Austen(2350)
Mobile Forensics Cookbook by Igor Mikhaylov(2017)
Computers For Seniors For Dummies by Nancy C. Muir(1996)
Bulletproof Android: Practical Advice for Building Secure Apps (Developer's Library) by Godfrey Nolan(1874)
Android Development with Kotlin by Marcin Moskala & Igor Wojda(1797)
Building Android UIs with Custom Views by Raimon Ràfols Montané(1791)
1936941139 (N) by Bob Rosenthal(1731)
Building Progressive Web Apps: Bringing the Power of Native to the Browser by Ater Tal(1726)
Hands-On Internet of Things with MQTT by Tim Pulver(1706)
Android App Development by Franceschi Hervé J.;(1702)
Ember.js in Action by Joachim Haagen Skeie(1689)
Hands-On Design Patterns with React Native by Mateusz Grzesiukiewicz(1656)
