I came across from ConnectivityManager and wifi . But it doesn't solve my problem. We are from lower internet bandwidth countries. Although data service is activated, there is no internet connection in some(every)time .
So , data service connected and wifi connected conditions can not able to decide whether our devices having an active internet connection .
.. So, I tried http post with AsyncTask . But it can't catch no active connection. But works well while in active connection.
here is my code -
class RequestTask extends AsyncTask<String, String, String>{
@Override
protected String doInBackground(String... uri) {
String responseString = null;
try {
HttpClient httpclient = new DefaultHttpClient();
HttpResponse response;
response = httpclient.execute(new HttpGet(uri[0]));
StatusLine statusLine = response.getStatusLine();
if(statusLine.getStatusCode() == HttpStatus.SC_OK){
ByteArrayOutputStream out = new ByteArrayOutputStream();
response.getEntity().writeTo(out);
responseString = out.toString();
out.close();
} else{
//Closes the connection.
//response.getEntity().getContent().close();
Toast.makeText(getApplicationContext(),"No Internet Connection :", Toast.LENGTH_SHORT).show();
}
} catch (ClientProtocolException e) {
//TODO Handle problems..
Toast.makeText(getApplicationContext(),"No Internet Connection :", Toast.LENGTH_SHORT).show();
} catch (IOException e) {
//TODO Handle problems..
Toast.makeText(getApplicationContext(),"No Internet Connection :", Toast.LENGTH_SHORT).show();
}
catch (Exception e){
Toast.makeText(getApplicationContext(),"No Internet Connection :", Toast.LENGTH_SHORT).show();
}
return responseString;
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
if(result.equals("null") || result.equals("")){
Toast.makeText(getApplicationContext(),"Account Not Found : ", Toast.LENGTH_SHORT).show();
}
else{
getpass=result;
Toast.makeText(getApplicationContext(),"Connecting to Server :", Toast.LENGTH_SHORT).show();
if(getpass.equals(edtpass.getText().toString())){
new RequestTaskname().execute("http://www.yangoninnovation.com/*****?****="+email);
}
}
}
}
All Catch process not work when no internet connection. Please kindly help me. I want to toast "No Internet connection" if http post can't find active connection .