货币转换器没有正确转换

时间:2016-02-29 09:15:33

标签: java android

好的,所以我是android studio的新手,并尝试制作货币转换器。我应该使用API​​,然后对其进行编码以进行转换并从API中获取费率。但是当我运行它时,一切正常,除了它将所有内容都转换为0,忽略用户输入。

由于API,我不得不以这种方式对其进行硬编码,我不知道如何以更简单的方式进行操作。抱歉,添麻烦了。如果需要更多信息,请告知我。

以下是我的转换器所在的MainActivity2的代码:

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 All records
                for (int i = 0; i < 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 the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a 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类:

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 i = 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 the response from the 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 the string to a 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;
    }
}

使用JSONlint编辑我的JSON:

{
    "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:

解析是可以的,但在调用方法btnGetRates之后似乎无法在我的转换器中获取它。以下是我的新加坡。设置并获得费率:

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;
    }
}

解析的结果:

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}}

在更改解析器后仍将任何值转换为0。

1 个答案:

答案 0 :(得分:2)

您需要使用已保存费率的对象(即tempSingapore)。你没有这样做,而是使用一个没有数据集的新对象,导致转换为0。

初始化您的变量,如Singapore tempSingapore = new Singapore ();然后

更改

 Singapore mySingapore = new Singapore();

 Singapore mySingapore = tempSingapore;
btnGetRates

中的

更新:刚看到您的JSON也无效。在http://jsonlint.com/

检查您的JSON
相关问题