【发布时间】:2016-09-01 16:59:06
【问题描述】:
我尝试从互联网上获取数据并将它们保存在变量中,但是这个过程太慢了......这是我的代码:
`public class CurrConvert extends AsyncTask {
boolean connection = true;
float exchangeRate(final String currencyFrom, final String currencyTo) throws IOException {
URL url = new URL("http://quote.yahoo.com/d/quotes.csv?s=" + currencyFrom + currencyTo + "=X&f=l1&e=.csv");
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setUseCaches(false);
InputStream inputStream = urlConnection.getInputStream();
String result = IOUtils.toString(inputStream);
return Float.parseFloat(result);
}
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(getActivity());
pDialog.setMessage(getContext().getString(R.string.updating));
pDialog.setCancelable(false);
pDialog.show();
}
@Override
protected Object doInBackground(Object[] objects) {
try {
USD_AUD = exchangeRate("USD", "AUD");
USD_CAD = exchangeRate("USD", "CAD");
USD_EUR = exchangeRate("USD", "EUR");
USD_GBP = exchangeRate("USD", "GBP");
USD_INR = exchangeRate("USD", "INR");
USD_JPY = exchangeRate("USD", "JPY");
USD_SGD = exchangeRate("USD", "SGD");
EUR_AUD = exchangeRate("EUR", "AUD");
EUR_CAD = exchangeRate("EUR", "CAD");
EUR_GBP = exchangeRate("EUR", "GBP");
EUR_INR = exchangeRate("EUR", "INR");
EUR_JPY = exchangeRate("EUR", "JPY");
EUR_SGD = exchangeRate("EUR", "SGD");
EUR_USD = exchangeRate("EUR", "USD");
AUD_CAD = exchangeRate("AUD", "CAD");
AUD_EUR = exchangeRate("AUD", "EUR");
AUD_GBP = exchangeRate("AUD", "GBP");
AUD_INR = exchangeRate("AUD", "INR");
AUD_JPY = exchangeRate("AUD", "JPY");
AUD_SGD = exchangeRate("AUD", "SGD");
AUD_USD = exchangeRate("AUD", "USD");
CAD_AUD = exchangeRate("CAD", "AUD");
CAD_EUR = exchangeRate("CAD", "EUR");
CAD_GBP = exchangeRate("CAD", "GBP");
CAD_INR = exchangeRate("CAD", "INR");
CAD_JPY = exchangeRate("CAD", "JPY");
CAD_SGD = exchangeRate("CAD", "SGD");
CAD_USD = exchangeRate("CAD", "USD");
SGD_AUD = exchangeRate("SGD", "AUD");
SGD_CAD = exchangeRate("SGD", "CAD");
SGD_EUR = exchangeRate("SGD", "EUR");
SGD_GBP = exchangeRate("SGD", "GBP");
SGD_INR = exchangeRate("SGD", "INR");
SGD_JPY = exchangeRate("SGD", "JPY");
SGD_USD = exchangeRate("SGD", "USD");
GBP_AUD = exchangeRate("GBP", "AUD");
GBP_CAD = exchangeRate("GBP", "CAD");
GBP_EUR = exchangeRate("GBP", "EUR");
GBP_INR = exchangeRate("GBP", "INR");
GBP_JPY = exchangeRate("GBP", "JPY");
GBP_SGD = exchangeRate("GBP", "SGD");
GBP_USD = exchangeRate("GBP", "USD");
JPY_AUD = exchangeRate("JPY", "AUD");
JPY_CAD = exchangeRate("JPY", "CAD");
JPY_EUR = exchangeRate("JPY", "EUR");
JPY_GBP = exchangeRate("JPY", "GBP");
JPY_INR = exchangeRate("JPY", "INR");
JPY_SGD = exchangeRate("JPY", "SGD");
JPY_USD = exchangeRate("JPY", "USD");
INR_AUD = exchangeRate("INR", "AUD");
INR_CAD = exchangeRate("INR", "CAD");
INR_EUR = exchangeRate("INR", "EUR");
INR_GBP = exchangeRate("INR", "GBP");
INR_JPY = exchangeRate("INR", "JPY");
INR_SGD = exchangeRate("INR", "SGD");
INR_USD = exchangeRate("INR", "USD");
} catch (IOException e) {
connection = false;
}
return 0;
}
@Override
protected void onPostExecute(Object o) {
super.onPostExecute(o);
pDialog.dismiss();
if (connection) {
Toast.makeText(getContext(), "Done", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(getContext(), R.string.no_connection, Toast.LENGTH_LONG).show();
}
}
}`
doInBackground 方法需要将近 30 秒才能完成该过程...有没有办法保持同步?
【问题讨论】:
-
嗯,您正在下载一个 CSV,并将 56 个请求中的每一个都存储在内存中……您为什么希望这会很快?
-
您可以加快速度,例如,不请求反向转换。如果您知道美元到欧元,那么您不需要查找欧元到美元。事实上,如果您对所有其他货币都了解一种货币,那么您应该能够应用一些数学来对其余货币进行转换。
-
@cricket_007 感谢您的建议!这样我总共有 7 个请求,所以我可以加快我的代码速度
-
对于 JSON 格式的所有货币,我可能会选择以下答案。那将是一个请求。不建议在不必要时发出多个请求,因为它会更快耗尽电池
标签: java android android-asynctask httpurlconnection