1
我的應用正試圖從雅虎財經獲得實時貨幣匯率。從Andriod studio的CSV文件中獲取信息?
以下是我的代碼,當我單擊按鈕時,總是返回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;
}
}
嘗試重新格式化您的代碼很難閱讀。此外,您可以更清楚地描述您的問題。 – DKIT
問題是,當我嘗試從雅虎財務API獲得匯率時,它總是在不使用android studio的情況下返回null – Zhaoyuxuan