私のアプリはyahoo financeからライブ通貨為替レートを取得しようとしています。Andriod studioのCSVファイルから情報を取得しますか?
私のコードは、ボタンをクリックすると0.1
が常に返されます(APIからの戻り値は常にNULLです)。私は自分のコードを貼り付けた後、私は自分のJavaコードを試して、それは動作しますが、それはAndroidスタジオで動作していません。
それはcsvファイルまたはsth elseを読み込むの問題ですか?
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から為替レートを取得しようとすると、アンドロイドスタジオを使わずに作業している間は常にnullを返すことです – Zhaoyuxuan