执行网络操作以检查用户是否通过异步任务连接了互联网 [英] Perform network operation to check if user has internet connection with async task
问题描述
我正处于例外之下
android.os.NetworkOnMainThreadException
因为我不使用异步任务来进行特定的网络操作.我已经搜索过了,但是这让我很困惑.有人可以使其与异步任务和特定功能一起使用吗?
because I don't use an async task to make the particular network operation. I have searched for this, but it got me so confused. Could someone make it work with async task and the particular functions?
以下是我使用的两个功能:
Below are two functions i use :
1) isNetworkAvailable()
private boolean isNetworkAvailable() {
ConnectivityManager connectivityManager
= (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
return activeNetworkInfo != null;
}
2)具有Internet访问(布尔showMessage)
当我想显示烤面包时,我调用此功能,将参数设置为 true .
When i want to display a toast i call this function, setting the parameter to 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的内部类来使用AsyncTask的方法.
This is how you can use an AsyncTask by creating an inner class which extends 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
You can execute the AsyncTask as follows
new NetworkInAsync(this).execute();
我仍然建议您仔细阅读文档此处进行澄清自己了解AsyncTask在Android中的工作原理.
I would still recommend you go through the docs here to clarify yourself how AsyncTask works in Android.
这篇关于执行网络操作以检查用户是否通过异步任务连接了互联网的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!