致命例外:AsyncTask #1,如果飞行模式已激活
FATAL EXCEPTION: AsyncTask #1, if airplane mode is activated
我使用 AsyncTask 对我的服务器进行获取请求。
如果我连接到互联网,Everythink 工作正常,但如果我激活飞行模式,我会遇到致命异常:
01-11 16:01:56.602 3456-3456/com.me.myapp E/ApiHelper: Error at
getting WuerdestDu Stats. Id: 19
01-11 16:01:56.603 3456-3522/com.me.myapp E/AndroidRuntime: FATAL
EXCEPTION: AsyncTask #1
Process: com.me.myapp, PID: 3456
奇怪的是,这是 Logcat 的完整错误日志,详细模式,没有过滤器。
这是我使用 AsyncTask 的地方:
try {
response = new ApiGetHelper().execute(path).get();
} catch (Exception e) {
Log.e(TAG, "Error at getting WuerdestDu Stats. Id: " + question.getId(), e);
}
这是我的 AsyncTask:
public class ApiGetHelper extends AsyncTask<String, String, String> {
private final static String TAG = ApiGetHelper.class.getSimpleName();
@Override
protected String doInBackground(String... strings) {
String urlString = strings[0];
try {
URL url = new URL(urlString);
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setRequestMethod("GET");
urlConnection.connect();
int responseCode = urlConnection.getResponseCode();
if (responseCode >= 200 && responseCode < 300) {
Log.i(TAG, "Response: " + urlConnection.getResponseCode() + ": " + urlConnection.getResponseMessage());
} else {
Log.e(TAG, "Response: " + urlConnection.getResponseCode() + ": " + urlConnection.getResponseMessage());
}
return ApiUtils.readResponseAsString(urlConnection.getInputStream());
} catch (MalformedURLException e) {
throw new IllegalStateException("URL-ERROR", e);
} catch (IOException e) {
throw new IllegalStateException("IO-ERROR", e);
}
}
}
如果您打开飞行模式,您将失去互联网,您需要在尝试 post 连接到您的服务器之前检查是否有可用的互联网连接。
像下面这样的东西应该可以解决问题
private boolean isNetworkConnected() {
ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
return cm.getActiveNetworkInfo() != null;
}
参考:Android check internet connection
我使用 AsyncTask 对我的服务器进行获取请求。 如果我连接到互联网,Everythink 工作正常,但如果我激活飞行模式,我会遇到致命异常:
01-11 16:01:56.602 3456-3456/com.me.myapp E/ApiHelper: Error at
getting WuerdestDu Stats. Id: 19
01-11 16:01:56.603 3456-3522/com.me.myapp E/AndroidRuntime: FATAL
EXCEPTION: AsyncTask #1
Process: com.me.myapp, PID: 3456
奇怪的是,这是 Logcat 的完整错误日志,详细模式,没有过滤器。
这是我使用 AsyncTask 的地方:
try {
response = new ApiGetHelper().execute(path).get();
} catch (Exception e) {
Log.e(TAG, "Error at getting WuerdestDu Stats. Id: " + question.getId(), e);
}
这是我的 AsyncTask:
public class ApiGetHelper extends AsyncTask<String, String, String> {
private final static String TAG = ApiGetHelper.class.getSimpleName();
@Override
protected String doInBackground(String... strings) {
String urlString = strings[0];
try {
URL url = new URL(urlString);
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setRequestMethod("GET");
urlConnection.connect();
int responseCode = urlConnection.getResponseCode();
if (responseCode >= 200 && responseCode < 300) {
Log.i(TAG, "Response: " + urlConnection.getResponseCode() + ": " + urlConnection.getResponseMessage());
} else {
Log.e(TAG, "Response: " + urlConnection.getResponseCode() + ": " + urlConnection.getResponseMessage());
}
return ApiUtils.readResponseAsString(urlConnection.getInputStream());
} catch (MalformedURLException e) {
throw new IllegalStateException("URL-ERROR", e);
} catch (IOException e) {
throw new IllegalStateException("IO-ERROR", e);
}
}
}
如果您打开飞行模式,您将失去互联网,您需要在尝试 post 连接到您的服务器之前检查是否有可用的互联网连接。
像下面这样的东西应该可以解决问题
private boolean isNetworkConnected() {
ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
return cm.getActiveNetworkInfo() != null;
}
参考:Android check internet connection