我有一个应用程序,在第一个检查互联网
然后使用JSON获取数据。
我的问题:当我使用此代码进行检查时,网络结果会崩溃!
但如果我在新项目中使用此代码,它将起作用! :|
public void ShowAlertDialog() {
AlertDialog.Builder alertDialog = new AlertDialog.Builder(MainActivity.this);
alertDialog.setTitle(R.string.title);
alertDialog.setMessage(R.string.message);
alertDialog.setIcon(R.mipmap.ic_launcher);
// Setting Positive "Yes" Button
alertDialog.setPositiveButton(R.string.yes,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
new NetCheck().execute();
}
});
// Setting Negative "NO" Button
alertDialog.setNegativeButton(R.string.no,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// Write your code here to invoke NO event
finish();
}
});
// Showing Alert Message
alertDialog.show();
}
/**
* Async Task to check whether internet connection is working.
**/
private class NetCheck extends AsyncTask<String,String,Boolean>
{
private ProgressDialog nDialog;
@Override
protected void onPreExecute(){
super.onPreExecute();
nDialog = new ProgressDialog(MainActivity.this);
nDialog.setTitle("Checking Network");
nDialog.setMessage("Loading..");
nDialog.setIndeterminate(false);
nDialog.setCancelable(true);
nDialog.show();
}
/**
* Gets current device state and checks for working internet connection by trying Google.
**/
@Override
protected Boolean doInBackground(String... args){
ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo netInfo = cm.getActiveNetworkInfo();
if (netInfo != null && netInfo.isConnected()) {
try {
URL url = new URL("http://www.google.com");
HttpURLConnection urlc = (HttpURLConnection) url.openConnection();
urlc.setConnectTimeout(3000);
urlc.connect();
if (urlc.getResponseCode() == 200) {
return true;
}
} catch (MalformedURLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return false;
}
@Override
protected void onPostExecute(Boolean th){
if(th == true){
nDialog.dismiss();
Toast.makeText(getApplicationContext(), "Your device is connected to internet !", Toast.LENGTH_SHORT).show();
// new GetData().execute();
}
else{
nDialog.dismiss();
ShowAlertDialog();
}
}
}
并在setContentView
之前的onCreate()中try
{
new NetCheck().execute();
}
catch (Exception ex){
ex.printStackTrace();
}
及其我的logcat错误
android.view.WindowLeaked: Activity com.irannezhad.rapcity.MainActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView{103c02ae V.E..... R.....I. 0,0-1041,568} that was originally added here
at android.view.ViewRootImpl.<init>(ViewRootImpl.java:470)
at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:277)
at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:69)
at android.app.Dialog.show(Dialog.java:312)
at android.support.v7.app.AlertDialog$Builder.show(AlertDialog.java:953)
at com.irannezhad.rapcity.MainActivity.ShowAlertDialog(MainActivity.java:262)
at com.irannezhad.rapcity.MainActivity$NetCheck.onPostExecute(MainActivity.java:323)
at com.irannezhad.rapcity.MainActivity$NetCheck.onPostExecute(MainActivity.java:270)
at android.os.AsyncTask.finish(AsyncTask.java:632)
at android.os.AsyncTask.access$600(AsyncTask.java:177)
at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:645)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:5940)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1389)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1184)