我正在尝试在用户登录时显示进度对话框。(应用程序连接到在线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 :(得分: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;
}
答案 1 :(得分:0)
在doInBackground中
catch (Exception e) {
e.printStackTrace();
progressDialog.dismiss();
}
答案 2 :(得分:0)
在启动AsyncTask之前检查互联网
接受类isInternetConnection boolean
picture
检查互联网
NSString *url = @"http://somesite.com/";
NSURL *stringURL = [NSURL URLWithString:url];
NSLog(@"url-------- %@",stringURL);
NSData *urlData = [NSData dataWithContentsOfURL:stringURL];
NSLog(@"data---------%@",urlData);
if (urlData)
{
// JSON successfully downloaded
NSLog(@"yes ");
}
else
{
NSLog(@"not exist");
}
答案 3 :(得分:0)
在doInBackground()里面捕获UnknownHostException ...它是IOException的子类型
@Override
protected String doInBackground(String... params) {
try{
//whatever code
} catch(UnknownHostException e) {
if (progressDialog.isShowing()) progressDialog.dismiss();
}
}
答案 4 :(得分:0)
在doInBackground()方法中,只需添加以下逻辑。
if(network is not available){
runOnUiThread(new Runnable() {
@Override
public void run() {
if(dialog != null){
dialog.dismiss();
}
}
});
}
&#13;