-1

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?

Zoe
  • 27,060
  • 21
  • 118
  • 148
Benjamin RD
  • 11,516
  • 14
  • 87
  • 157
  • 3
    i think its cos you're running this on the main thread. Network requests are required to be run on the background. Try using async task or volley – Mueyiwa Moses Ikomi Sep 03 '16 at 16:01
  • @MueyiwaMosesIkomi, won't `android.os.NetworkOnMainThreadException` rised when we try to run this on main thread ? – mrtpk Sep 03 '16 at 16:05
  • there is that too but as you stated, its a one second execution on postman. – Mueyiwa Moses Ikomi Sep 03 '16 at 16:08
  • 1
    NEVER turn off the strict mode policy. Its there for a reason. In fact on many devices you actually can't turn it off. There's a good reason that Android wants you to not do networking on the main thread- to prevent the exact problem you're having. Stop trying to work around it, and do networking on a second thread. – Gabe Sechan Sep 03 '16 at 16:11

1 Answers1

2
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

Zoe
  • 27,060
  • 21
  • 118
  • 148