Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Using AsyncTasks to access WebServices

  • Loading branch information...
commit 13f65d7579844607136158ed3dfea32f5af800a8 1 parent f73bb01
Mike Wolfson authored
1  AndroidManifest.xml
View
@@ -7,6 +7,7 @@
<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="16" />
+ <uses-permission android:name="android.permission.INTERNET"/>
<application
android:allowBackup="true"
2  res/values/strings.xml
View
@@ -13,7 +13,7 @@
<string name="expense_item">Expense Item:</string>
<string name="details_example_text">Example text : $11.11</string>
<string name="tax_deductable">Tax Deductable?</string>
- <string name="receipt">Receipt</string>
+ <string name="receipt">Convert</string>
<string name="expense_detail">Expense Detail</string>
<string name="email_dev">Email Dev</string>
<string name="expenses_main">Expenses Main</string>
126 src/com/andevcon/bootcamp/DetailActivity.java
View
@@ -3,15 +3,28 @@
*/
package com.andevcon.bootcamp;
+import java.io.BufferedReader;
+import java.io.IOException;
+import java.io.InputStreamReader;
+import java.net.URI;
+import java.net.URISyntaxException;
+
+import org.apache.http.HttpResponse;
+import org.apache.http.client.ClientProtocolException;
+import org.apache.http.client.HttpClient;
+import org.apache.http.client.methods.HttpGet;
+import org.apache.http.impl.client.DefaultHttpClient;
+
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
-import android.net.Uri;
+import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
-import android.widget.EditText;
+import android.view.View;
+import android.widget.Button;
import android.widget.ShareActionProvider;
import android.widget.TextView;
import android.widget.Toast;
@@ -32,20 +45,105 @@ protected void onCreate(Bundle savedInstanceState) {
Intent intent = getIntent();
String name = intent.getStringExtra("name");
- int amount = intent.getIntExtra("amount", 0);
+ final int amount = intent.getIntExtra("amount", 0);
- String expense = convertToUsd(name, amount);
+ String expense = formatForDisplay(name, amount);
TextView expenseTextView = (TextView) findViewById(R.id.detail_expense_title);
expenseTextView.setText(expense);
+
+ Button convertButton = (Button) findViewById(R.id.detail_picture);
+ convertButton.setOnClickListener(new View.OnClickListener() {
+ public void onClick(View v) {
+ String usdollars = formatForWebcall(amount);
+ new CurrencyConversionTask().execute(usdollars);
+ }
+ });
+
+ }
+
+
+ /**
+ * AsyncTask to make the webservice call off the main UI thread
+ *
+ */
+ private class CurrencyConversionTask extends AsyncTask<String, Void, Void> {
+
+ protected void onPreExecute() {
+ }
+
+ protected Void doInBackground(final String... args) {
+ String inputValue = args[0];
+ String convertedValue = convertToGBP(inputValue);
+ Log.v("AnDevCon", "The return value from the call is: " + convertedValue);
+ return null;
+ }
+
+ // can use UI thread here
+ protected void onPostExecute(final Void unused) {
+
+ }
}
- private String convertToUsd(String name, int amount) {
+ /**
+ * Makes a call to a web-service to convert currency
+ * @param amount
+ * @return
+ */
+ public static String convertToGBP(String amount) {
+ String page = "";
+
+ BufferedReader in = null;
+ try {
+ HttpClient client = new DefaultHttpClient();
+ HttpGet request = new HttpGet();
+
+ StringBuilder sb = new StringBuilder();
+ sb.append("http://www.google.com/ig/calculator?hl=en&q=");
+ sb.append(amount);
+ sb.append("USD=?GBP");
+ Log.v("AnDevCon", "The URL we are sending is: " + sb.toString());
+
+ request.setURI(new URI(sb.toString()));
+ HttpResponse response = client.execute(request);
+ in = new BufferedReader(new InputStreamReader(response.getEntity()
+ .getContent()));
+ StringBuffer retStr = new StringBuffer("");
+ String line = "";
+ String NL = System.getProperty("line.separator");
+ while ((line = in.readLine()) != null) {
+ retStr.append(line + NL);
+ }
+ in.close();
+ page = retStr.toString();
+ } catch (URISyntaxException e) {
+ Log.v("AnDevCon", "URISyntaxException", e);
+ e.printStackTrace();
+ } catch (ClientProtocolException e) {
+ Log.v("AnDevCon", "ClientProtocolException", e);
+ e.printStackTrace();
+ } catch (IOException e) {
+ Log.v("AnDevCon", "IOException", e);
+ e.printStackTrace();
+ } finally {
+ if (in != null) {
+ try {
+ in.close();
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+ }
+ }
+ return page;
+ }
+
+ private String formatForDisplay(String name, int amount) {
String usd = "$00.00";
if (amount != 0) {
Integer amountInt = Integer.valueOf(amount);
String amountString = amountInt.toString();
- String dollars = amountString.substring(0, amountString.length() - 2);
+ String dollars = amountString.substring(0,
+ amountString.length() - 2);
String cents = amountString.substring(amountString.length() - 2);
usd = "$" + dollars + "." + cents;
}
@@ -53,6 +151,20 @@ private String convertToUsd(String name, int amount) {
String expenseUSD = name + " : " + usd;
return expenseUSD;
}
+
+ private String formatForWebcall(int amount) {
+ String usd = "00.00";
+ if (amount != 0) {
+ Integer amountInt = Integer.valueOf(amount);
+ String amountString = amountInt.toString();
+ String dollars = amountString.substring(0,
+ amountString.length() - 2);
+ String cents = amountString.substring(amountString.length() - 2);
+ usd = dollars + "." + cents;
+ }
+
+ return usd;
+ }
@Override
public boolean onCreateOptionsMenu(Menu menu) {
@@ -117,4 +229,6 @@ public void setShareIntent() {
provider.setShareIntent(intent);
}
}
+
+
}
Please sign in to comment.
Something went wrong with that request. Please try again.