gpt4 book ai didi

java - 如何使用 Exchangeratesapi.io 和 okhttp 更改汇率基础货币,并在微调器中选择所有基础货币?

转载 作者:行者123 更新时间:2023-12-01 19:12:54 25 4
gpt4 key购买 nike

我想要做的是将汇率基础(默认为欧元)更改为任何其他货币,并将它们全部放在旋转器中,就像我要更改的货币一样,以便用户可以选择任何基础货币以及要使用微调器转换的货币。

喜欢这里,但针对基础货币

这是我的代码

public static BreakIterator data;
List<String> keysList;
Spinner toCurrency;
TextView textView;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

toCurrency = findViewById(R.id.planets_spinner);
final EditText edtEuroValue = findViewById(R.id.editText4);
final Button btnConvert = findViewById(R.id.button);
textView = findViewById(R.id.textView7);
try {
//From the API loads the conversion types from a url and makes a request(gets all of the currencies)
loadConversionTypes();
} catch (IOException e) {
e.printStackTrace();
}

btnConvert.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//If the inserted currency to convert from is not empty the program will initialize the toCurency(will get the
//selected item and then will get the double value of the currency to convert from(weird as hell ik)
if(!edtEuroValue.getText().toString().isEmpty())
{
String toCurr = toCurrency.getSelectedItem().toString();
double euroValue = Double.valueOf(edtEuroValue.getText().toString());

Toast.makeText(MainActivity.this, "Calculating..", Toast.LENGTH_SHORT).show();
try {
//calls the convertCurrency() method and takes in the parameters of the currrency to convert to, and the
//currency to convert from(2nd paramter)
convertCurrency(toCurr, euroValue);
}
//will catch an error if the conversion doesn't work
catch (IOException e) {
e.printStackTrace();
Toast.makeText(MainActivity.this, e.getMessage(), Toast.LENGTH_SHORT).show();
}
}
//If the value of the currency to convert from is empty, it will show a message:
else
{
Toast.makeText(MainActivity.this, "Please enter a value first.", Toast.LENGTH_SHORT).show();
}

}
});

}

public void loadConversionTypes() throws IOException {

String url = "https://api.exchangeratesapi.io/latest";

OkHttpClient client = new OkHttpClient();

Request request = new Request.Builder()
.url(url)
.header("Content-Type", "application/json")
.build();


//passes in parameter request from the api(code above)
client.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Request request, IOException e) {
//takes the failure message and displays it as a toast
String mMessage = e.getMessage().toString();
Log.w("failure Response", mMessage);
Toast.makeText(MainActivity.this, mMessage, Toast.LENGTH_SHORT).show();
}

@Override
public void onResponse(Response response) throws IOException {
final String mMessage = response.body().string();


MainActivity.this.runOnUiThread(new Runnable() {
@Override
public void run() {
//Toast.makeText(MainActivity.this, mMessage, Toast.LENGTH_SHORT).show();
try {
JSONObject obj = new JSONObject(mMessage);
JSONObject b = obj.getJSONObject("rates"); //b is a collection of rate data
//JSONObject base = obj.getJSONObject("base");

Iterator keysToCopyIterator = b.keys(); //the Iterator gets all of the data from the b rates and
//stores it in keysToCopyIterator
//a new scalable ArrayList of keys is created, the keys are stored as strings
keysList = new ArrayList<String>();

//while the key iterator keeps returning true for any tokens to be scanned it will keep on checking
//for the next keys possible until it returns false(all tokens have been scanned)
while(keysToCopyIterator.hasNext()) {

//finds and returns the complete key and saves it a string, which is later saved to an arraylist
String key = (String) keysToCopyIterator.next();

//adds the key to the ArrayList
keysList.add(key);

}


//An ArrayAdapter is created and sets all of the currencies(keysLists or exchange rates) to the spinner
ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_spinner_item, keysList );
toCurrency.setAdapter(spinnerArrayAdapter); //the to currency spinner is set to the adapter






} catch (JSONException e) {
e.printStackTrace();
}

}
});
}




});
}

public void convertCurrency(final String toCurr, final double euroValue) throws IOException {

String url = "https://api.exchangeratesapi.io/latest";

OkHttpClient client = new OkHttpClient();

Request request = new Request.Builder()
.url(url)
.header("Content-Type", "application/json")
.build();



//catches execption incase of failure or error
client.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Request request, IOException e) {
String mMessage = e.getMessage().toString();
Log.w("failure Response", mMessage);
Toast.makeText(MainActivity.this, mMessage, Toast.LENGTH_SHORT).show();
}

@Override
public void onResponse(Response response) throws IOException {
final String mMessage = response.body().string();
MainActivity.this.runOnUiThread(new Runnable() {
@Override
public void run() {
//Toast.makeText(MainActivity.this, mMessage, Toast.LENGTH_SHORT).show();
try {
JSONObject obj = new JSONObject(mMessage);
JSONObject b = obj.getJSONObject("rates");

String val = b.getString(toCurr);

double output = /*selected currency */euroValue*Double.valueOf(val);


textView.setText(String.valueOf(output));

} catch (JSONException e) {
e.printStackTrace();
}

}
});
}





});
}
}

不要介意评论,我是初学者,我正在努力学习编码和注释所有内容如果你不能帮助我,那么也许你可以找到如何做到这一点的文档,我一直在到处寻找,但找不到合适的java代码。谢谢!

最佳答案

由于没有人回答,我想出了自己的解决方案。

我创建了 2 个微调器,并将它们命名为 baseCurrency 和 toCurrency,然后我所做的是这样的 - 我创建了另一个微调器,然后将微调器适配器设置为它 baseCurrency.setAdapter(spinnerArrayAdapter);

然后在convertCurrency()中我输入这两行

String fromCurrVal = b.getString(baseCurrency);
double output = baseValue/Double.valueOf(frommCurrVal)*Double.valueOf(val);

此处获取输入数字的基值,将其除以所选货币汇率,然后乘以您要转换为的货币值(value)。有点令人困惑,但它可以非常有效地完成工作。

关于java - 如何使用 Exchangeratesapi.io 和 okhttp 更改汇率基础货币,并在微调器中选择所有基础货币?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59454679/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com