我有一个聊天应用程序,它试图让某人登录到服务器。登录代码是使用 AsyncTask 实现的。问题是,在登录过程中并且出现网络丢失,尽管我使用了所有 try...catch 语句,应用程序还是崩溃了。请问如何通过通知用户网络丢失而不是应用程序崩溃来优雅地处理这个问题。
我在调用 asynctask 之前检查了网络,但我想避免的是当您处于进程中间并且突然出现网络丢失时
这是代码的一部分
protected String doInBackground(String... args) {
try {
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(LOGIN_URL);
httpPost.setEntity(new UrlEncodedFormEntity(params));
HttpResponse httpResponse = httpClient.execute(httpPost);
int statusCode=httpResponse.getStatusLine().getStatusCode();
if(statusCode!=HttpStatus.SC_OK){
Log.d("latestchat", "Connection Error");
Toast.makeText(Login.this, "Error in Network Connection\n ", Toast.LENGTH_LONG).show();
return null;
}
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
if(is!=null){
BufferedReader reader = new BufferedReader(new InputStreamReader(
is, "iso-8859-1"), 8);
// Declare a string builder to help with the parsing.
StringBuilder sb = new StringBuilder();
// Declare a string to store the JSON object data in string form.
String line = null;
// Build the string until null.
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
// Close the input stream.
is.close();
// Convert the string builder data to an actual string.
json = sb.toString();
jObj = new JSONObject(json);
}
} catch (JSONException e) {
Log.d("latestchat", "JSon error: "+e.toString());
Toast.makeText(Login.this, "Error in Network Connection\n "+e.getMessage(), Toast.LENGTH_LONG).show();
}
catch (UnsupportedEncodingException e) {
Toast.makeText(Login.this, "Unsupported Encoding ", Toast.LENGTH_LONG).show();
} catch (ClientProtocolException e) {
Toast.makeText(Login.this, "Protocol not supported ", Toast.LENGTH_LONG).show();
} catch (IOException e) {
Toast.makeText(Login.this, "Error connecting to Server ", Toast.LENGTH_LONG).show();
Log.e("latestchat", "Error connecting to Server " + e.toString());
}
catch (Exception e) {
Toast.makeText(Login.this, "Error connecting to Server ", Toast.LENGTH_LONG).show();
Log.e("latestchat", "Error connecting to Server " + e.toString());
}
return null;
}
这是日志猫
11-06 13:03:17.169 E/AndroidRuntime(16149): at com.example.latestchat.Login$AttemptLogin.doInBackground(Login.java:303)
11-06 13:03:17.169 E/AndroidRuntime(16149): at com.example.latestchat.Login$AttemptLogin.doInBackground(Login.java:1)
11-06 13:05:58.249 E/AndroidRuntime(16405): at com.example.latestchat.Login$AttemptLogin.doInBackground(Login.java:303)
11-06 13:05:58.249 E/AndroidRuntime(16405): at com.example.latestchat.Login$AttemptLogin.doInBackground(Login.java:1)
11-06 13:05:59.329 E/WindowManager(16405): Activity com.example.latestchat.Login has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@407cb440 that was originally added here
11-06 13:05:59.329 E/WindowManager(16405): android.view.WindowLeaked: Activity com.example.latestchat.Login has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@407cb440 that was originally added here
11-06 13:05:59.329 E/WindowManager(16405): at com.example.latestchat.Login$AttemptLogin.onPreExecute(Login.java:186)
11-06 13:05:59.329 E/WindowManager(16405): at com.example.latestchat.Login.newLogin(Login.java:171)
11-06 13:05:59.329 E/WindowManager(16405): at com.example.latestchat.Login.logIn(Login.java:120)
11-06 13:07:20.429 E/AndroidRuntime(16573): at com.example.latestchat.Login$AttemptLogin.doInBackground(Login.java:303)
11-06 13:07:20.429 E/AndroidRuntime(16573): at com.example.latestchat.Login$AttemptLogin.doInBackground(Login.java:1)
11-06 13:07:21.119 E/WindowManager(16573): Activity com.example.latestchat.Login has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@407c9468 that was originally added here
11-06 13:07:21.119 E/WindowManager(16573): android.view.WindowLeaked: Activity com.example.latestchat.Login has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@407c9468 that was originally added here
11-06 13:07:21.119 E/WindowManager(16573): at com.example.latestchat.Login$AttemptLogin.onPreExecute(Login.java:186)
11-06 13:07:21.119 E/WindowManager(16573): at com.example.latestchat.Login.newLogin(Login.java:171)
11-06 13:07:21.119 E/WindowManager(16573): at com.example.latestchat.Login.logIn(Login.java:120)