2016-08-04 90 views
0

我试图在用户登录时显示进度对话框(应用程序连接到在线mysqldatabase)。 我关闭了我的网络连接,但进度对话框没有关闭。当没有网络连接时关闭进度对话框

如果在没有互联网连接的情况下关闭进度对话框?

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; 


} 


} 
+0

尝试从doInBackground删除了Thread.sleep(1000)()方法...在你的例外可能解决这个问题 – tsiro

+0

呼叫dismmiss对话框。 – Amir

+0

删除Thread.sleep没有工作 –

回答

0

内部doInBackground()捕获的UnknownHostException ......它是一个亚型IOException

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

} 
+0

谢谢tsiro我尝试progressdialog.dismiss()NullPointerException catch..and它的工作 –

+0

欢迎您...您可以接受并upvote我的答案.. – tsiro

+0

所有答案都可以很好地工作,但我不能接受只有一个答案。 顺便说一句,我不能为我的低声誉投票 –

0

在doInBackground支票这样

 if(!isNetworkAvailable()){ 
     progressDialog.dismiss(); 
     } 

下面的互联网连接的可用性的方法

public static boolean isNetworkAvailable(final Context context) { 
    boolean isNetAvailable = false; 
    if (context != null) { 
     final ConnectivityManager mConnectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); 
     if (mConnectivityManager != null) { 
      final NetworkInfo activeNetwork = mConnectivityManager.getActiveNetworkInfo(); 
      if(activeNetwork != null) isNetAvailable = true; 
     } 
    } 
    return isNetAvailable; 
} 
+0

不能解决方法getSystemService ..我不知道为什么这个错误显示出来。我在另一个活动中尝试这种方法没有错误显示 –

+0

感谢阿卡什,完美的方法,它的工作! –

0

在doInBackground

catch (Exception e) { 
    e.printStackTrace(); 
    progressDialog.dismiss(); 
} 
+0

感谢兄弟它在NullPointerException异常和进度对话消失! –

0

检查Internet之前启动的AsyncTask

取类isInternetConnection布尔

public static boolean isInternetConnection(Context mContext) 
{ 
    final ConnectivityManager connMgr = (ConnectivityManager) mContext.getSystemService(Context.CONNECTIVITY_SERVICE); 

    final NetworkInfo wifi = connMgr.getNetworkInfo(ConnectivityManager.TYPE_WIFI); 

    final NetworkInfo mobile = connMgr.getNetworkInfo(ConnectivityManager.TYPE_MOBILE); 

    if (wifi.isAvailable()&& wifi.getState() == NetworkInfo.State.CONNECTED) 
    { 


     return true; 
    } 
    else if (mobile.isAvailable()&& mobile.getState() == NetworkInfo.State.CONNECTED) 
    { 
     return true; 
    } 
    else 
    { 
     return false; 
    } 
} 

检查互联网

if(isInternetConnection(Youractivity.this) 
{ 
    new BackgroundLogin().execute(); 
} 
+0

令人敬畏的兄弟Er.Arjun saini,谢谢! –

0

在doInBackground()方法中,只需添加以下逻辑。

if(network is not available){ 
 
runOnUiThread(new Runnable() { 
 
     @Override 
 
     public void run() { 
 
      if(dialog != null){ 
 
      dialog.dismiss(); 
 
      } 
 
    } 
 
}); 
 

 
}