I am new to android. What i want to do is just pass a URL to Asynctask and then check if return status code SC_OK or not. The method should return true if the status code SC_OK is returned and false if any other status code.
class checking extends AsyncTask<String, Void, Boolean> {
@Override
protected Boolean doInBackground(String... params) {
// TODO Auto-generated method stub
Boolean a;
HttpClient client = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(l1);
HttpResponse response;
try {
response = client.execute(httpGet);
StatusLine statusLine = response.getStatusLine();
int statusCode = statusLine.getStatusCode();
if (statusCode != HttpStatus.SC_OK) {
Toast.makeText(getApplicationContext(),"No", Toast.LENGTH_LONG).show();
a= false;
}
else {
Toast.makeText(getApplicationContext(),"Yes", Toast.LENGTH_LONG).show();
a= true;
}
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally{
return false;
}
}
@Override
protected void onPostExecute(Boolean result) {
}
}
Change the whole thing to (blind coding):
class checking extends AsyncTask<String, Void, Boolean> {
@Override
protected Boolean doInBackground(String... params) {
// TODO Auto-generated method stub
Boolean a;
HttpClient client = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(l1);
HttpResponse response;
try {
response = client.execute(httpGet);
StatusLine statusLine = response.getStatusLine();
int statusCode = statusLine.getStatusCode();
if (statusCode != HttpStatus.SC_OK) {
a = false;
} else {
a = true;
}
return a;
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return false;
}
@Override
protected void onPostExecute(Boolean result) {
Toast.makeText(getApplicationContext(), (result ? "Yes" : "No"), Toast.LENGTH_LONG).show();
}
}