Mahdi Hr Mahdi Hr - 3 months ago 27
Android Question

Dismiss progress dialog when no network connection

I am trying to show up a progress dialog when user is signing in.(the app connects to an online mysqldatabase).
I turned off my network connection but the progress dialog is not dismissed.

How to dismiss the progress dialog if there's no internet connection?

public class BackgroundLogin extends AsyncTask<String,Void,String> {
Context ctx;
ProgressDialog progressDialog;
BackgroundLogin(Context ctx)
{
this.ctx=ctx;
}


@Override
protected void onPreExecute() {
progressDialog = new ProgressDialog(ctx);
progressDialog.setTitle("Please Wait..");
progressDialog.setMessage("Signing in..");
progressDialog.setMax(5);
progressDialog.setCancelable(true);
progressDialog.show();

}


@Override
protected void onPostExecute(String result) {
try {

if (result.equals("interdit")) {
progressDialog.dismiss();
Toast.makeText(ctx, "Access forbidden", Toast.LENGTH_SHORT).show();
}
else

if (result.equals("invalid")) {
progressDialog.dismiss();
Toast.makeText(ctx, "Invalid username or password", Toast.LENGTH_SHORT).show();
}
else

{
progressDialog.dismiss();
Intent i = new Intent(ctx, HomeScreen.class);
i.putExtra("username", result);


ctx.startActivity(i);

}


} catch (NullPointerException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}


@Override
protected String doInBackground(String... params) {


String reg_url="http://www.androidiut20.netai.net/verification.php";
String username=params[0];
String password=params[1];
String langue=params[2];
String connection=params[3];


try {
URL url = new URL(reg_url);


HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
httpURLConnection.setRequestMethod("POST");
httpURLConnection.setDoOutput(true);
OutputStream outputStream = httpURLConnection.getOutputStream();
BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream, "UTF-8"));
String data = URLEncoder.encode("username", "UTF-8") + "=" + URLEncoder.encode(username, "UTF-8") + "&" +
URLEncoder.encode("password", "UTF-8") + "=" + URLEncoder.encode(password, "UTF-8") + "&" +
URLEncoder.encode("langue", "UTF-8") + "=" + URLEncoder.encode(langue, "UTF-8");
bufferedWriter.write(data);
bufferedWriter.flush();
bufferedWriter.close();
outputStream.close();
InputStream inputStream = httpURLConnection.getInputStream();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream, "iso-8859-1"));
String response = "";
String line = "";
while ((line = bufferedReader.readLine()) != null) {
response += line;


}
Thread.sleep(1000);
bufferedReader.close();
inputStream.close();
httpURLConnection.disconnect();
return response;

} catch (MalformedURLException e) {
e.printStackTrace();
} catch (NullPointerException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}


return null;


}


}

Answer

inside doInBackground() catch UnknownHostException...it is a subtype of IOException

@Override
protected String doInBackground(String... params) {
   try{
      //whatever code
   } catch(UnknownHostException e) {
       if (progressDialog.isShowing()) progressDialog.dismiss();
   }

}
Comments