gpt4 book ai didi

java - 从 Android Studio 中的 CSV 文件中获取信息?

转载 作者:行者123 更新时间:2023-11-29 19:14:47 26 4
gpt4 key购买 nike

我的应用正在尝试从雅虎财经获取实时货币汇率。

以下是我的代码,当我点击按钮时,总是返回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;
}
}

最佳答案

试试这个..

        URL url = new URL("http://quote.yahoo.com/d/quotes.csv?s=" + from + to + "=X&f=l1&e=.csv");
HttpClient httpClient = new DefaultHttpClient();
HttpContext localContext = new BasicHttpContext();
HttpGet httpGet = new HttpGet(url);
HttpResponse response = httpClient.execute(httpGet, localContext);
String result = "";
InputStream is = response.getEntity().getContent();
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
try {
String line;
while ((line = reader.readLine()) != null) {
String[] RowData = line.split(",");
date = RowData[0];
value = RowData[1];
// do something with "data" and "value"
}
}
catch (IOException ex) {
// handle exception
}
finally {
try {
is.close();
}
catch (IOException e) {
// handle exception
}
}

关于java - 从 Android Studio 中的 CSV 文件中获取信息?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43976387/

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