我正在尝试为货币兑换创建一个简单的Android应用程序,当我接受这个尝试并抓住时,我陷入困境。我想从try中获取rate的值并捕获并使用它,但是我无法弄清楚这是如何可行的或者是否可能。任何帮助将不胜感激。
String exchange = "http://rate-exchange.appspot.com/currency?from=" + FROM + "&to=" + TO + "&q=1";
double RATE = 0;
JSONObject json;
try {
json = readJsonFromUrl(exchange);
for(int i = 0; i < json.length(); i ++)
RATE = json.getDouble("v");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
total.setText(String.valueOf(RATE*AMOUNT));
答案 0 :(得分:1)
double RATE = 0; // initialize RATE
JSONObject json;
try {
// do something
RATE = json.getDouble("v"); // calculate values for RATE, if try fail
//before assign value RATE will be 0
} catch (IOException e) {
} catch (JSONException e) {
}finnally
{
\\ Set Rate Here
}
答案 1 :(得分:0)
double RATE = 0; // initialize RATE
JSONObject json;
try {
// do something
RATE = json.getDouble("v"); // calculate values for RATE, if try fail
//before assign value RATE will be 0
} catch (IOException e) {
} catch (JSONException e) {
}
total.setText(String.valueOf(RATE*AMOUNT)); // so still RATE 0 and
//wrong calculation