サーバーに誰かをサインインさせようとするチャット アプリがあります。サインインのコードは、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)