2016-08-04 39 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(); 
 
      } 
 
    } 
 
}); 
 

 
}