执行HTTP发布后20秒,Android冻结屏幕 [英] Android freeze screen 20 seconds after execute a HTTP Post

查看:74
本文介绍了执行HTTP发布后20秒,Android冻结屏幕的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从我的android设备执行HTTP Post,但是由于某些原因,post会在几秒钟内执行,但屏幕仍会冻结18或20秒.

I'm executing a HTTP Post from my android device, but for some reason, the post is executed in few seconds but the screen is still freeze for 18 or 20 seconds more.

我正在使用的代码是:

    public boolean CommentPost(String comment, String requestId, String deviceId){
            List<NameValuePair> params = new ArrayList<NameValuePair>(6);
            params.add(new BasicNameValuePair("requestId", requestId));
            params.add(new BasicNameValuePair("comment", URLEncoder.encode(requestId)));
            params.add(new BasicNameValuePair("deviceId", URLEncoder.encode(deviceId)));

            return ExecutePost(params, "Comment/Add");
        }

private boolean ExecutePost(List<NameValuePair> params, String url){
        String queryString = RewriteParams(params);
        url = BaseURL + url + queryString;
        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost(url);

        try {
            StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
            StrictMode.setThreadPolicy(policy);

            httppost.setEntity(new UrlEncodedFormEntity(params));
            HttpResponse response = httpclient.execute(httppost);

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

我使用ajax和一些Http客户端(例如邮递员)测试了该行为,并且该请求花费了不到1或2秒的时间才能执行并获得响应. 为什么在android中谈论更多的时间?

I tested the behavior with ajax and some Http clients (like postman) and the request takes less than 1 or 2 second to be executed and get the response. Why it's talking more time in android?

推荐答案

class Check extends AsyncTask<String, Integer, Boolean> {

    @Override
    protected Boolean doInBackground(String... params) {



            //Add main arguments here; Connection that reffers to other methods but 
    String queryString = RewriteParams(params);
    url = BaseURL + url + queryString;
    HttpClient httpclient = new DefaultHttpClient();
    HttpPost httppost = new HttpPost(url);

    try {

        httppost.setEntity(new UrlEncodedFormEntity(params));
        HttpResponse response = httpclient.execute(httppost);

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

并像这样执行:

new Check().execute("http://website.com");

如果需要,您可以在此处添加URL.如果它是固定链接,则不是必需的.

You can add URL there if you want. If it is a fixed link it is not nessesary.

您进行了添加,以便可以在主线程上运行连接程序,但是我认为您的线程没有为此正确构建.可以这样解释:

You added so you can run connection stuff on the main thread, but I believe your thread isn't properly built for it. It can be explained like this:

while(bool = true){
    connection code
}
rendering code

虽然正在连接并执行所有这些操作,但由于连接要求,其他任务无法执行.运行异步使您可以在不干扰主线程的情况下进行连接.

While it is connecting and doing all of that, the other tasks cannot execute due to the connections requirements. Running async allows you to connect without it disturbing the main thread.

如果您需要与String,Integer和Boolean不同的参数,请记住第一个变量是doInBackground中的params变量,最后一个参数将是返回值

If you need different arguments than String, Integer and Boolean, remember that the first variable is the one that is params in doInBackground and the last argument will be the return value

这篇关于执行HTTP发布后20秒,Android冻结屏幕的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆