java - Currency converter not converting properly -
ok, new android studio , trying make currency converter. supposed consume api , code converting , taking rates api. when run it, works fine other fact converting 0, disregarding user input.
i had hard code way because of api , have no idea how easier way. sorry trouble. please inform me if more information needed.
here code mainactivity2 converter resides in:
package com.example.justin.currencyconverter20; import android.app.progressdialog; import android.os.asynctask; import android.support.v7.app.appcompatactivity; import android.os.bundle; import android.util.log; import android.view.menu; import android.view.menuitem; import android.view.view; import android.widget.arrayadapter; import android.widget.edittext; import android.widget.spinner; import android.widget.textview; import android.widget.toast; import org.json.jsonarray; import org.json.jsonexception; import org.json.jsonobject; import org.w3c.dom.text; import java.util.list; public class mainactivity2 extends appcompatactivity { private string[] arrayspinner; private static final string login_url = "https://api.fixer.io/latest?base=sgd"; list<singapore> mycountries; singapore tempsingapore; string operation; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main2); this.arrayspinner = new string[] { "aud", "bgn", "brl", "cad", "chf", "cny", "czk", "dkk", "gbp", "hkd", "hrk", "huf", "idr", "ils", "inr", "jpy", "krw", "mxn", "myr", "nok", "nzd", "php", "pln", "ron", "rub", "sek", "thb", "try", "usd", "zar", "eur" }; spinner s = (spinner) findviewbyid(r.id.countryspinner1); arrayadapter<string> adapter = new arrayadapter<string>(this,android.r.layout.simple_spinner_item, arrayspinner); s.setadapter(adapter); new getrates().execute(); } jsonparser jsonparser = new jsonparser(); class getrates extends asynctask<string, string, jsonarray> { private progressdialog progressdialog; @override protected jsonarray doinbackground(string... args) { try { jsonarray json = jsonparser.makehttprequest( login_url, "get"); if (json != null) { log.d("json result", json.tostring()); return json; } } catch (exception e) { e.printstacktrace(); } return null; } protected void onpostexecute(jsonarray json) { if (progressdialog != null && progressdialog.isshowing()) { progressdialog.dismiss(); } if (json != null) { // looping through records (int = 0; < json.length(); i++) { try { jsonobject c = json.getjsonobject(i); tempsingapore.setname("singapore"); tempsingapore.setcurrencycode("sgd"); tempsingapore.setaud(c.getdouble("aud")); tempsingapore.setbrl(c.getdouble("brl")); tempsingapore.setbgn(c.getdouble("bgn")); tempsingapore.setcad(c.getdouble("cad")); tempsingapore.setcny(c.getdouble("cny")); tempsingapore.setchf(c.getdouble("chf")); tempsingapore.setczk(c.getdouble("czk")); tempsingapore.setdkk(c.getdouble("dkk")); tempsingapore.seteur(c.getdouble("eur")); tempsingapore.setgbp(c.getdouble("gbp")); tempsingapore.sethkd(c.getdouble("hkd")); tempsingapore.sethrk(c.getdouble("hrk")); tempsingapore.sethuf(c.getdouble("huf")); tempsingapore.setidr(c.getdouble("idr")); tempsingapore.setils(c.getdouble("ils")); tempsingapore.setinr(c.getdouble("inr")); tempsingapore.setjpy(c.getdouble("jpy")); tempsingapore.setkrw(c.getdouble("krw")); tempsingapore.setmxn(c.getdouble("mxn")); tempsingapore.setmyr(c.getdouble("myr")); tempsingapore.setnok(c.getdouble("nok")); tempsingapore.setnzd(c.getdouble("nzd")); tempsingapore.setphp(c.getdouble("php")); tempsingapore.setpln(c.getdouble("pln")); tempsingapore.setron(c.getdouble("ron")); tempsingapore.setrub(c.getdouble("rub")); tempsingapore.setsek(c.getdouble("sek")); tempsingapore.settry(c.getdouble("try")); tempsingapore.setthb(c.getdouble("thb")); tempsingapore.setusd(c.getdouble("usd")); tempsingapore.setzar(c.getdouble("zar")); string aud = string.valueof(c.getdouble("aud")); toast mytoast = toast.maketext(getapplicationcontext(), aud, toast.length_short); mytoast.show(); } catch (jsonexception e) { e.printstacktrace(); } } populate(); } } } @override public boolean oncreateoptionsmenu(menu menu) { // inflate menu; adds items action bar if present. getmenuinflater().inflate(r.menu.menu_main, menu); return true; } @override public boolean onoptionsitemselected(menuitem item) { // handle action bar item clicks here. action bar // automatically handle clicks on home/up button, long // specify parent activity in androidmanifest.xml. int id = item.getitemid(); //noinspection simplifiableifstatement if (id == r.id.action_settings) { return true; } return super.onoptionsitemselected(item); } public void populate() { } public void btngetrates (view v){ new getrates().execute(); textview textview = (textview) findviewbyid(r.id.textview3); spinner tempspinner = (spinner) findviewbyid(r.id.countryspinner1); edittext edittext = (edittext) findviewbyid(r.id.edittext); double usertext = double.parsedouble(edittext.gettext().tostring()); string valofspinner = tempspinner.getselecteditem().tostring(); singapore mysingapore = new singapore(); double rates = 0; if (valofspinner.equals("aud") ) { mysingapore.getaud(); rates = mysingapore.getaud(); } else if (valofspinner.equals("bgn")) { mysingapore.getbgn(); rates = mysingapore.getbgn(); } else if (valofspinner.equals("brl")) { mysingapore.getbrl(); rates = mysingapore.getbrl(); } else if (valofspinner.equals("cad")) { mysingapore.getcad(); rates = mysingapore.getcad(); } else if (valofspinner.equals("chf")) { mysingapore.getchf(); rates = mysingapore.getchf(); } else if (valofspinner.equals("cny")) { mysingapore.getcny(); rates = mysingapore.getcny(); } else if (valofspinner.equals("czk")) { mysingapore.getczk(); rates = mysingapore.getczk(); } else if (valofspinner.equals("dkk")) { mysingapore.getdkk(); rates = mysingapore.getdkk(); } else if (valofspinner.equals("gbp")) { mysingapore.getgbp(); rates = mysingapore.getgbp(); } else if (valofspinner.equals("hkd")) { mysingapore.gethkd(); rates = mysingapore.gethkd(); } else if (valofspinner.equals("hrk")) { mysingapore.gethrk(); rates = mysingapore.gethrk(); } else if (valofspinner.equals("huf")) { mysingapore.gethuf(); rates = mysingapore.gethuf(); } else if (valofspinner.equals("idr")) { mysingapore.getidr(); rates = mysingapore.getidr(); } else if (valofspinner.equals("ils")) { mysingapore.getils(); rates = mysingapore.getils(); } else if (valofspinner.equals("inr")) { mysingapore.getinr(); rates = mysingapore.getinr(); } else if (valofspinner.equals("jpy")) { mysingapore.getjpy(); rates = mysingapore.getjpy(); } else if (valofspinner.equals("krw")) { mysingapore.getkrw(); rates = mysingapore.getkrw(); } else if (valofspinner.equals("myr")) { mysingapore.getmyr(); rates = mysingapore.getmyr(); } else if (valofspinner.equals("mxn")) { mysingapore.getmxn(); rates = mysingapore.getmxn(); } else if (valofspinner.equals("nok")) { mysingapore.getnok(); rates = mysingapore.getnok(); } else if (valofspinner.equals("nzd")) { mysingapore.getnzd(); rates = mysingapore.getnzd(); } else if (valofspinner.equals("php")) { mysingapore.getphp(); rates = mysingapore.getphp(); } else if (valofspinner.equals("pln")) { mysingapore.getpln(); rates = mysingapore.getpln(); } else if (valofspinner.equals("ron")) { mysingapore.getron(); rates = mysingapore.getron(); } else if (valofspinner.equals("rub")) { mysingapore.getrub(); rates = mysingapore.getrub(); } else if (valofspinner.equals("sek")) { mysingapore.getsek(); rates = mysingapore.getsek(); } else if (valofspinner.equals("thb")) { mysingapore.getthb(); rates = mysingapore.getthb(); } else if (valofspinner.equals("try")) { mysingapore.gettry(); rates = mysingapore.gettry(); } else if (valofspinner.equals("usd")) { mysingapore.getusd(); rates = mysingapore.getusd(); } else if (valofspinner.equals("zar")) { mysingapore.getzar(); rates = mysingapore.getzar(); } else if (valofspinner.equals("eur")) { mysingapore.geteur(); rates = mysingapore.geteur(); } double rateresult = usertext * rates; string result = string.valueof(rateresult); textview.settext(result); } }
edit2 jsonparser class:
package com.example.justin.currencyconverter20; import android.util.log; import org.json.jsonarray; import org.json.jsonexception; import org.json.jsonobject; import java.io.bufferedinputstream; import java.io.bufferedreader; import java.io.dataoutputstream; import java.io.ioexception; import java.io.inputstream; import java.io.inputstreamreader; import java.io.unsupportedencodingexception; import java.net.httpurlconnection; import java.net.url; import java.net.urlencoder; import java.util.hashmap; public class jsonparser { string charset = "utf-8"; httpurlconnection conn; dataoutputstream wr; stringbuilder result; url urlobj; jsonarray obj = null; stringbuilder sbparams; string paramsstring; public jsonarray makehttprequest(string url, string method) { int = 0; if(method.equals("get")){ try { urlobj = new url(url); conn = (httpurlconnection) urlobj.openconnection(); conn.setdooutput(false); conn.setrequestmethod("get"); conn.setconnecttimeout(15000); conn.connect(); } catch (ioexception e) { e.printstacktrace(); } } try { //receive response server inputstream in = new bufferedinputstream(conn.getinputstream()); bufferedreader reader = new bufferedreader(new inputstreamreader(in)); result = new stringbuilder(); string line; while ((line = reader.readline()) != null) { result.append(line); } log.d("json parser", "result: " + result.tostring()); } catch (ioexception e) { // e.printstacktrace(); } conn.disconnect(); // try parse string json object try { jsonobject jobj = new jsonobject(result.tostring()); jsonobject ratesobject = jobj.getjsonobject("rates"); double aud = ratesobject.getdouble("aud"); double bgn = ratesobject.getdouble("bgn"); double brl = ratesobject.getdouble("brl"); double cad = ratesobject.getdouble("cad"); double chf = ratesobject.getdouble("chf"); double cny = ratesobject.getdouble("cny"); double czk = ratesobject.getdouble("czk"); double dkk = ratesobject.getdouble("dkk"); double gbp = ratesobject.getdouble("gbp"); double hkd = ratesobject.getdouble("hkd"); double hrk = ratesobject.getdouble("hrk"); double huf = ratesobject.getdouble("huf"); double idr = ratesobject.getdouble("idr"); double ils = ratesobject.getdouble("ils"); double inr = ratesobject.getdouble("inr"); double jpy = ratesobject.getdouble("jpy"); double krw = ratesobject.getdouble("krw"); double mxn = ratesobject.getdouble("mxn"); double myr = ratesobject.getdouble("myr"); double nok = ratesobject.getdouble("nok"); double nzd = ratesobject.getdouble("nzd"); double php = ratesobject.getdouble("php"); double pln = ratesobject.getdouble("pln"); double ron = ratesobject.getdouble("ron"); double rub = ratesobject.getdouble("rub"); double sek = ratesobject.getdouble("sek"); double thb = ratesobject.getdouble("thb"); double try = ratesobject.getdouble("try"); double usd = ratesobject.getdouble("usd"); double zar = ratesobject.getdouble("zar"); double eur = ratesobject.getdouble("eur"); } catch (jsonexception e) { log.e("json parser", "error parsing data " + e.tostring()); } // return json object return obj; } }
edit3 json using jsonlint:
{ "base": "sgd", "date": "2016-02-26", "rates": { "aud": 0.99008, "bgn": 1.2677, "brl": 2.8102, "cad": 0.96636, "chf": 0.70839, "cny": 4.6639, "czk": 17.542, "dkk": 4.8354, "gbp": 0.5104, "hkd": 5.5426, "hrk": 4.941, "huf": 201.27, "idr": 9537.9, "ils": 2.7826, "inr": 49.002, "jpy": 80.646, "krw": 881.73, "mxn": 12.914, "myr": 3.0044, "nok": 6.1735, "nzd": 1.0596, "php": 33.884, "pln": 2.828, "ron": 2.8934, "rub": 53.827, "sek": 6.074, "thb": 25.432, "try": 2.0966, "usd": 0.71338, "zar": 11.183, "eur": 0.64817 } }
edit4:
parsing okay cannot seem in converter after calling on method btngetrates. below singapore.class set , rates:
public class singapore { private string name; private double aud; private double bgn; private double brl; private double cad; private double chf; private double cny; private double czk; private double dkk; private double gbp; private double hkd; private double hrk; private double huf; private double idr; private double ils; private double inr; private double jpy; private double krw; private double mxn; private double myr; private double nok; private double nzd; private double php; private double pln; private double ron; private double rub; private double sek; private double thb; private double try; private double usd; private double zar; private double eur; public string getname() { return name; } public void setname(string name) { this.name = name; } public double getaud() { return aud; } public void setaud(double aud) { this.aud = aud; } public double getbgn() { return bgn; } public void setbgn(double bgn) { this.bgn = bgn; } public double getbrl() { return brl; } public void setbrl(double brl) { this.brl = brl; } public double getcad() { return cad; } public void setcad(double cad) { this.cad = cad; } public double getchf() { return chf; } public void setchf(double chf) { this.chf = chf; } public double getcny() { return cny; } public void setcny(double cny) { this.cny = cny; } public double getczk() { return czk; } public void setczk(double czk) { this.czk = czk; } public double getdkk() { return dkk; } public void setdkk(double dkk) { this.dkk = dkk; } public double getgbp() { return gbp; } public void setgbp(double gbp) { this.gbp = gbp; } public double gethkd() { return hkd; } public void sethkd(double hkd) { this.hkd = hkd; } public double gethrk() { return hrk; } public void sethrk(double hrk) { this.hrk = hrk; } public double gethuf() { return huf; } public void sethuf(double huf) { this.huf = huf; } public double getidr() { return idr; } public void setidr(double idr) { this.idr = idr; } public double getils() { return ils; } public void setils(double ils) { this.ils = ils; } public double getinr() { return inr; } public void setinr(double inr) { this.inr = inr; } public double getjpy() { return jpy; } public void setjpy(double jpy) { this.jpy = jpy; } public double getkrw() { return krw; } public void setkrw(double krw) { this.krw = krw; } public double getmxn() { return mxn; } public void setmxn(double mxn) { this.mxn = mxn; } public double getmyr() { return myr; } public void setmyr(double myr) { this.myr = myr; } public double getnok() { return nok; } public void setnok(double nok) { this.nok = nok; } public double getnzd() { return nzd; } public void setnzd(double nzd) { this.nzd = nzd; } public double getphp() { return php; } public void setphp(double php) { this.php = php; } public double getpln() { return pln; } public void setpln(double pln) { this.pln = pln; } public double getron() { return ron; } public void setron(double ron) { this.ron = ron; } public double getrub() { return rub; } public void setrub(double rub) { this.rub = rub; } public double getsek() { return sek; } public void setsek(double sek) { this.sek = sek; } public double getthb() { return thb; } public void setthb(double thb) { this.thb = thb; } public double gettry() { return try; } public void settry(double try) { this.try = try; } public double getusd() { return usd; } public void setusd(double usd) { this.usd = usd; } public double getzar() { return zar; } public void setzar(double zar) { this.zar = zar; } public double geteur() { return eur; } public void seteur(double eur) { this.eur = eur; } }
and result of parsing:
03-01 05:23:36.050 13219-13400/com.example.justin.currencyconverter20 d/json parser: result: {"base":"sgd","date":"2016-02-29","rates":{"aud":0.99576,"bgn":1.2762,"brl":2.8316,"cad":0.96359,"chf":0.71217,"cny":4.6559,"czk":17.655,"dkk":4.868,"gbp":0.51276,"hkd":5.5237,"hrk":4.9764,"huf":203.11,"idr":9500.4,"ils":2.7769,"inr":48.537,"jpy":80.352,"krw":879.31,"mxn":12.92,"myr":2.9931,"nok":6.2018,"nzd":1.0804,"php":33.68,"pln":2.8413,"ron":2.9205,"rub":53.927,"sek":6.0828,"thb":25.336,"try":2.1059,"usd":0.71047,"zar":11.391,"eur":0.65253}}
still converting value 0 after changes parser.
you need use object in have saved rates (i.e; tempsingapore
). rather doing using new object has no data set inside causing conversion 0.
initialize variable singapore tempsingapore = new singapore ();
then
change
singapore mysingapore = new singapore();
to
singapore mysingapore = tempsingapore;
in btngetrates
update: saw json not valid. check json @ http://jsonlint.com/
Comments
Post a Comment