与AsyncTask的HttpURLConnection太慢(Android)

时间:2016-09-01 16:59:06

标签: java android android-asynctask httpurlconnection

我尝试从互联网上获取数据并将它们保存在变量中,但过程太慢了...这里是我的代码:

`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秒的时间来完成这个过程......有没有办法让人感觉很好?

2 个答案:

答案 0 :(得分:1)

每次调用exchangeRate方法时,都必须下载csv文件以提取汇率值。您总共有56个请求,这就是您的AsyncTask太慢的原因。

我建议使用此链接:all currencies in JSON format

现在,这很简单!只需解码JSON响应,您就可以参考以下链接:How to parse the Json response in android?

获取responses JSON数组,每个JSON对象具有以下结构(示例):

{
"resource" : { 
"classname" : "Quote",
"fields" : { 
"change" : "-0.006400",
"chg_percent" : "-0.481094",
"name" : "USD/AUD",
"price" : "1.323900",
"symbol" : "AUD=X",
"ts" : "1472750677",
"type" : "currency",
"utctime" : "2016-09-01T17:24:37+0000",
"volume" : "0"
}
}
}

您现在要做的就是获得price值,在您的情况下是汇率。

希望这会有所帮助:)

答案 1 :(得分:0)

默认情况下,AsyncTask以较低的优先级运行,以帮助确保UI线程保持响应,但您可以更改它:

protected Void doInBackground() {
    Thread.currentThread().setPriority(Thread.MAX_PRIORITY);
    // do your work
}