我試圖在用戶登錄時顯示進度對話框(應用程序連接到在線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;
}
}
嘗試從doInBackground刪除了Thread.sleep(1000)()方法...在你的例外可能解決這個問題 – tsiro
呼叫dismmiss對話框。 – Amir
刪除Thread.sleep沒有工作 –