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
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…