【发布时间】:2017-05-15 09:52:34
【问题描述】:
我的应用正在尝试从 yahoo Finance 获取实时货币汇率。
以下是我的代码,当我点击按钮时,总是返回0.1(从API返回的值总是NULL)。我已经尝试了我的 java 代码,它可以工作,但是在我粘贴我的代码后它不能在 Android Studio 上工作。
这是读取csv文件的问题还是其他的问题?
public class MainActivity extends AppCompatActivity {
public void startConvert(View view){
EditText amount,from,to;
amount=(EditText)findViewById(R.id.amount);
from=(EditText)findViewById(R.id.from);
to=(EditText)findViewById(R.id.to);
Double many;
//many=Double.parseDouble(amount.toString());
Toast.makeText(this,"haha",Toast.LENGTH_LONG).show();
Double conv =findExchangeRateAndConvert("EUR", "USD", 1000);
Toast.makeText(this,Double.toString(conv),Toast.LENGTH_SHORT).show();
}
private static Double findExchangeRateAndConvert(String from, String to, int amount) {
try {
//Yahoo Finance API
URL url = new URL("http://quote.yahoo.com/d/quotes.csv?s=" + from + to + "=X&f=l1&e=.csv");
BufferedReader reader = new BufferedReader(new InputStreamReader(url.openStream()));
String line = reader.readLine();
if (line.length() > 0) {
return Double.parseDouble(line) * amount;
}
reader.close();
} catch (Exception e) {
System.out.println(e.getMessage());
}
return 0.1;
}
}
【问题讨论】:
-
尝试重新格式化难以阅读的代码。此外,在描述您的问题时,您可能会更清楚。
-
问题是,当我尝试从 yahoo Finance API 获取汇率时,它在不使用 android studio 的情况下工作时总是返回 null
标签: java android api csv yahoo-finance