java - Getting a "money converter" Android app to convert one currency to another based on user input -


so here instructions:

retrieve current currency conversion rates :

 http://api.fixer.io 

for example, convert dollars british pounds call:

 http://api.fixer.io/latest?base=usd&symbols=gbp 

2- provide 2 spinners 5 currencies of choosing.

3 - use async calls conversion lookups, shouldn't block ui thread.

now i've gotten far pulling values json api , getting display conversion rate of 1 usd 1 gbp (this done hard coding in usd , gbp pull json api. i'm struggling trying hard coded currencies instead pulled pair of spinners containing 5 different currencies. additionally i'm struggling rate multiply user specified amount (for example if rate 0.69 , user wants convert 10usd gbp app should report converted amount 6.90gbp) @ present getting "operator * cannot applied double" @ present app crashes @ launch , first priority getting run , convert 1 monetary unit equivalent amount of monetary unit.

this fetchconversionratetask.java file

and android studio throwing error

package edu.ggc.tkeating.currencyconverter;  import android.content.context; import android.media.mediaplayer; import android.os.asynctask; import android.util.log; import android.widget.edittext; import android.widget.textview;  import org.json.jsonexception; import org.json.jsonobject;  import java.io.bufferedinputstream; import java.io.ioexception; import java.net.httpurlconnection; import java.net.url; import java.text.decimalformat; import java.util.scanner;  /**  * created t3p0 on 6/9/2016.  */  //http://api.fixer.io/latest?base=usd&symbols=gbp public class fetchconversionratetask extends asynctask<string, string, double> {      private httpurlconnection conn;     private textview tv;     private context context;     private edittext amount;     private textview conversiontv;      public fetchconversionratetask(context _context, textview _tv, edittext _amount){         this.tv = _tv;         this.context = _context;         this.amount = _amount;     }      private final static string tag = "currencyconverter";         @override     protected void onpreexecute() {         super.onpreexecute();         mediaplayer tingmp = mediaplayer.create(context, r.raw.ting);         tingmp.start();     }      @override     protected double doinbackground(string... currencies){         string = currencies[0];         string = currencies[1];         double rate = 0.0d;         double conversion = 0.0d;           try{             url url = new url("http://api.fixer.io/latest?base="+ +                     "&symbols="+ to);             conn =  (httpurlconnection) url.openconnection();             scanner scanner = null;             scanner = new scanner(new bufferedinputstream(conn.getinputstream()));               string rslt ="";             while (scanner.hasnext()) rslt += scanner.nextline();             log.i(tag, rslt);              // {"base":"usd","date":"2016-06-09","rates":{"gbp":0.69151}}             rate = new jsonobject(rslt).getjsonobject("rates").getdouble(to);             conversion = new jsonobject(rslt).getjsonobject("conversion").getdouble(string.valueof(amount));                scanner.close();             return conversion;          } catch (ioexception | jsonexception e){             e.printstacktrace();         }         return rate;      }      @override     protected void onpostexecute(double rate){         super.onpostexecute(rate);         mediaplayer tingmp = mediaplayer.create(context, r.raw.ting);         tingmp.start();         decimalformat format = new decimalformat("###.##");         string msg = "rate=" + format.format(rate);         log.i(tag, msg);         tv.settext(msg);       } } 

this mainactivity.java

package edu.ggc.tkeating.currencyconverter; import android.os.bundle; import android.support.v7.app.appcompatactivity; import android.view.view; import android.widget.arrayadapter; import android.widget.button; import android.widget.edittext; import android.widget.spinner; import android.widget.textview; public class mainactivity extends appcompatactivity {     private textview result;     private edittext amount;     private string fromcurr;     private string tocurr;     @override     protected void oncreate(bundle savedinstancestate) {         super.oncreate(savedinstancestate);         setcontentview(r.layout.activity_main);         button button = (button) findviewbyid(r.id.btnconvert);         result = (textview) findviewbyid(r.id.tvresult);         amount = (edittext) findviewbyid(r.id.amount);         spinner spinner2 = (spinner) findviewbyid(r.id.spinnerfrom);         fromcurr = spinner2.getselecteditem().tostring();         spinner spinner = (spinner) findviewbyid(r.id.spinnerto);         tocurr = spinner.getselecteditem().tostring(); // create arrayadapter using string array , default spinner layout         arrayadapter<charsequence> adapter = arrayadapter.createfromresource(this,                 r.array.currency_array, android.r.layout.simple_spinner_item); // specify layout use when list of choices appears         adapter.setdropdownviewresource(android.r.layout.simple_spinner_dropdown_item); // apply adapter spinner         spinner.setadapter(adapter);         button.setonclicklistener(new view.onclicklistener() {             @override             public void onclick(view v) {                 new fetchconversionratetask(getapplicationcontext(), result,                         amount).execute(fromcurr, tocurr);             }         });     } } 

activity_main.xml

<?xml version="1.0" encoding="utf-8"?> <android.support.design.widget.coordinatorlayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:fitssystemwindows="true" tools:context="edu.ggc.tkeating.currencyconverter.mainactivity">  <android.support.design.widget.appbarlayout     android:layout_width="match_parent"     android:layout_height="wrap_content"     android:theme="@style/apptheme.appbaroverlay">      <android.support.v7.widget.toolbar         android:id="@+id/toolbar"         android:layout_width="match_parent"         android:layout_height="?attr/actionbarsize"         android:background="?attr/colorprimary"         app:popuptheme="@style/apptheme.popupoverlay"/>  </android.support.design.widget.appbarlayout>  <include layout="@layout/content_main"/>  </android.support.design.widget.coordinatorlayout> 

androidmanifest.xml

<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android"           package="edu.ggc.tkeating.currencyconverter">      <uses-permission android:name="android.permission.internet"/>      <application         android:allowbackup="true"         android:icon="@mipmap/ic_launcher"         android:label="@string/app_name"         android:supportsrtl="true"         android:theme="@style/apptheme">         <activity             android:name=".mainactivity"             android:label="@string/app_name"             android:theme="@style/apptheme.noactionbar">             <intent-filter>                 <action android:name="android.intent.action.main"/>                  <category android:name="android.intent.category.launcher"/>             </intent-filter>         </activity>     </application>  </manifest> 

content_main.xml

<?xml version="1.0" encoding="utf-8"?> <relativelayout     xmlns:android="http://schemas.android.com/apk/res/android"     xmlns:app="http://schemas.android.com/apk/res-auto"     xmlns:tools="http://schemas.android.com/tools"     android:layout_width="match_parent"     android:layout_height="match_parent"     app:layout_behavior="@string/appbar_scrolling_view_behavior"     tools:context="edu.ggc.tkeating.currencyconverter.mainactivity"     tools:showin="@layout/activity_main">      <button         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:text="convert"         android:id="@+id/btnconvert"         android:layout_centervertical="true"         android:layout_centerhorizontal="true"/>      <textview         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:textappearance="?android:attr/textappearancelarge"         android:text="result"         android:id="@+id/tvresult"         android:layout_margintop="44dp"         android:layout_below="@+id/btnconvert"         android:layout_centerhorizontal="true" />      <edittext         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:inputtype="numberdecimal"         android:ems="10"         android:id="@+id/amount"         android:layout_above="@+id/btnconvert"         android:layout_centerhorizontal="true"         android:layout_marginbottom="79dp"         android:hint="how want convert?"/>      <spinner         android:layout_width="match_parent"         android:layout_height="wrap_content"         android:id="@+id/spinnerto"         android:spinnermode="dropdown"         android:layout_above="@+id/btnconvert"         android:layout_alignparentright="true"         android:layout_alignparentend="true"         android:layout_torightof="@+id/tvresult"         android:layout_toendof="@+id/tvresult"/>      <spinner         android:layout_width="match_parent"         android:layout_height="wrap_content"         android:id="@+id/spinnerfrom"         android:spinnermode="dropdown"         android:layout_above="@+id/btnconvert"         android:layout_toleftof="@+id/btnconvert"         android:layout_tostartof="@+id/btnconvert"/>      <textview         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:textappearance="?android:attr/textappearancelarge"         android:text="result"         android:id="@+id/conversiontv"         android:layout_margintop="22dp"         android:layout_below="@+id/tvresult"         android:layout_centerhorizontal="true"/>  </relativelayout> 

i feel i'm missing incredibly simple , obvious , i'm hoping fresh eyes (and more experienced programmers) able figure code out. thank in advanced


Comments

Popular posts from this blog

wordpress - (T_ENDFOREACH) php error -

Export Excel workseet into txt file using vba - (text and numbers with formulas) -

Using django-mptt to get only the categories that have items -