AsyncTask在android中加载数据的次数非常不同

问题描述 投票:0回答:1

我写了这段代码来发送请求并从服务器获取数据。有时,2G互联网连接的加载过程需要2秒,但有时需要超过20秒的4G互联网连接。我必须考虑一下我没有做过的事吗?

这是我的代码

 @Override
protected void onPreExecute() {
    dialog.show();

    new CountDownTimer(10000, 10000) {
        public void onTick(long millisUntilFinished) {
        }

        public void onFinish() {
            // stop async task if not in progress
            if (getStatus() == Status.RUNNING) {
                Request.this.cancel(false);
                activity.finish();
                font.toast(activity.getResources().getString(R.string.connection_problem));

            }
        }
    }.start();

    super.onPreExecute();
}

@Override
protected String doInBackground(String... params) {
    if (isOnline())
        try {

            data += params[1];
            link += params[0];


            URL url = new URL(link);
            HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
            httpURLConnection.setRequestMethod("POST");
            httpURLConnection.connect();

            BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(httpURLConnection
                    .getOutputStream()));
            writer.write(data);
            writer.flush();

            int responseCode = httpURLConnection.getResponseCode();
            if (responseCode == 200) {//if valid, read result from server
                BufferedReader reader = new BufferedReader(new InputStreamReader
                        (httpURLConnection.getInputStream()));
                String line;
                StringBuilder stringBuilder = new StringBuilder();
                while ((line = reader.readLine()) != null) {
                    stringBuilder.append(line);
                }
                return stringBuilder.toString();
            }
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    else {
        dialog.cancel();
        activity.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                font.toast(activity.getResources().getString(R.string.internet_connection_problem));
                activity.finish();
            }
        });

    }
    return null;
}


@Override
protected void onPostExecute(final String o) {
    try {
        if (o != null) {
            dialog.cancel();
            callBack.onResponse(new Response(o));
        }
    } catch (JSONException e) {
        e.printStackTrace();
    }
    super.onPostExecute(o);
}
 public static boolean isOnline() {
    try {
        int timeoutMs = 2500;
        Socket sock = new Socket();
        SocketAddress sockaddr = new InetSocketAddress("8.8.8.8", 53);

        sock.connect(sockaddr, timeoutMs);
        sock.close();

        return true;
    } catch (IOException e) {
        return false;
    }
}

我使用post方法将请求发送到服务器(php),然后我收到结果。

我用isOnline方法检查互联网连接,并在onPerExecute方法中使用CountDownTimer设置超时。

无论如何要加快连接速度?我如何防止加载时间非常不同?

java android multithreading android-asynctask connection
1个回答
0
投票

这里只是一个建议,当然你总是可以忽略我:)

为什么不使用Retrofit - > https://square.github.io/retrofit/,它非常简单直观。

© www.soinside.com 2019 - 2024. All rights reserved.