执行网络操作以检查用户是否具有与异步任务的互联网连接
Perform network operation to check if user has internet connection with async task
我低于异常
android.os.NetworkOnMainThreadException
因为我没有使用异步任务来进行特定的网络操作。我已经搜索过这个,但它让我很困惑。有人可以让它与异步任务和特定功能一起使用吗?
下面是我使用的两个函数:
1) isNetworkAvailable()
private boolean isNetworkAvailable() {
ConnectivityManager connectivityManager
= (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
return activeNetworkInfo != null;
}
2) hasInternetAccess(Boolean showMessage)
当我想显示吐司时,我调用这个函数,将参数设置为 true。
public boolean hasInternetAccess(Boolean showMessage) {
if (isNetworkAvailable()) {
try {
HttpURLConnection urlc = (HttpURLConnection)
(new URL("http://clients3.google.com/generate_204")
.openConnection());
urlc.setRequestProperty("User-Agent", "Android");
urlc.setRequestProperty("Connection", "close");
urlc.setConnectTimeout(1500);
urlc.connect();
return (urlc.getResponseCode() == 204 &&
urlc.getContentLength() == 0);
} catch (IOException e) {
Log.w("connection", "Error checking internet connection", e);
}
} else {
if(showMessage) // If i want to show the toast, it's true
showAToast("No Internet Connection", Toast.LENGTH_SHORT); // Just another function to show a toast
}
return false;
}
这是通过创建一个扩展 AsyncTask 的内部 class 来使用 AsyncTask 的方法。
private class NetworkInAsync extends AsyncTask<String, Void, Boolean> {
private Context context;
private Activity activity;
NetworkInAsync(Activity activity) {
this.context = activity.getApplicationContext();
this.activity = activity;
}
@Override
protected void onPreExecute() {
}
@Override
protected void onPostExecute(Boolean result) {
// Do something with the result here
}
@Override
protected Boolean doInBackground(String... params) {
if (isNetworkAvailable()) {
try {
HttpURLConnection urlc = (HttpURLConnection)
(new URL("http://clients3.google.com/generate_204")
.openConnection());
urlc.setRequestProperty("User-Agent", "Android");
urlc.setRequestProperty("Connection", "close");
urlc.setConnectTimeout(1500);
urlc.connect();
return (urlc.getResponseCode() == 204 &&
urlc.getContentLength() == 0);
} catch (IOException e) {
Log.w("connection", "Error checking internet connection", e);
}
} else {
if(showMessage) // If i want to show the toast, it's true
showAToast("No Internet Connection", Toast.LENGTH_SHORT); // Just another function to show a toast
}
return false;
}
}
您可以按如下方式执行AsyncTask
new NetworkInAsync(this).execute();
我仍然建议您阅读文档 here 以弄清楚 AsyncTask 在 Android 中的工作原理。
当您从 AsyncTask
的 doInBackground
方法
调用代码时,代码应该可以工作
致电test();
private void test() {
HttpURLConnection urlc = (HttpURLConnection)
(new URL("http://clients3.google.com/generate_204")
.openConnection());
urlc.setConnectTimeout(1500);
urlc.connect();
}
你可以在调用之前检查网络连接,但是无论如何,你应该在TimeOut异常中捕获异常。所以我认为,在拨打电话之前检查连接性对您没有多大好处。
我低于异常
android.os.NetworkOnMainThreadException
因为我没有使用异步任务来进行特定的网络操作。我已经搜索过这个,但它让我很困惑。有人可以让它与异步任务和特定功能一起使用吗?
下面是我使用的两个函数:
1) isNetworkAvailable()
private boolean isNetworkAvailable() {
ConnectivityManager connectivityManager
= (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
return activeNetworkInfo != null;
}
2) hasInternetAccess(Boolean showMessage)
当我想显示吐司时,我调用这个函数,将参数设置为 true。
public boolean hasInternetAccess(Boolean showMessage) {
if (isNetworkAvailable()) {
try {
HttpURLConnection urlc = (HttpURLConnection)
(new URL("http://clients3.google.com/generate_204")
.openConnection());
urlc.setRequestProperty("User-Agent", "Android");
urlc.setRequestProperty("Connection", "close");
urlc.setConnectTimeout(1500);
urlc.connect();
return (urlc.getResponseCode() == 204 &&
urlc.getContentLength() == 0);
} catch (IOException e) {
Log.w("connection", "Error checking internet connection", e);
}
} else {
if(showMessage) // If i want to show the toast, it's true
showAToast("No Internet Connection", Toast.LENGTH_SHORT); // Just another function to show a toast
}
return false;
}
这是通过创建一个扩展 AsyncTask 的内部 class 来使用 AsyncTask 的方法。
private class NetworkInAsync extends AsyncTask<String, Void, Boolean> {
private Context context;
private Activity activity;
NetworkInAsync(Activity activity) {
this.context = activity.getApplicationContext();
this.activity = activity;
}
@Override
protected void onPreExecute() {
}
@Override
protected void onPostExecute(Boolean result) {
// Do something with the result here
}
@Override
protected Boolean doInBackground(String... params) {
if (isNetworkAvailable()) {
try {
HttpURLConnection urlc = (HttpURLConnection)
(new URL("http://clients3.google.com/generate_204")
.openConnection());
urlc.setRequestProperty("User-Agent", "Android");
urlc.setRequestProperty("Connection", "close");
urlc.setConnectTimeout(1500);
urlc.connect();
return (urlc.getResponseCode() == 204 &&
urlc.getContentLength() == 0);
} catch (IOException e) {
Log.w("connection", "Error checking internet connection", e);
}
} else {
if(showMessage) // If i want to show the toast, it's true
showAToast("No Internet Connection", Toast.LENGTH_SHORT); // Just another function to show a toast
}
return false;
}
}
您可以按如下方式执行AsyncTask
new NetworkInAsync(this).execute();
我仍然建议您阅读文档 here 以弄清楚 AsyncTask 在 Android 中的工作原理。
当您从 AsyncTask
的 doInBackground
方法
致电test();
private void test() {
HttpURLConnection urlc = (HttpURLConnection)
(new URL("http://clients3.google.com/generate_204")
.openConnection());
urlc.setConnectTimeout(1500);
urlc.connect();
}
你可以在调用之前检查网络连接,但是无论如何,你应该在TimeOut异常中捕获异常。所以我认为,在拨打电话之前检查连接性对您没有多大好处。