Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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.

The code that I'm using is:

    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;
        }
    }

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
141 views
Welcome To Ask or Share your Answers For Others

1 Answer

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;
    }
}

and execute like so:

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

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.

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...