在AsyncHttpClient任务中正确管理ProgressDialog以避免泄露windows Android
我正在开发一个android应用程序 我需要从我的应用程序向服务器发出几个请求,所以我使用的是AsyncHttpClient 我的应用程序的一部分有一个用户配置文件和一个时间表来显示一些事件。当用户登录时,我需要获取他们的个人资料信息和他们的时间线信息,为此,我必须向服务器发出3个不同的请求: 第一个请求:登录->将cookie和会话信息保存到SharedReferences中 第二个请求:获取配置文件->保存用户的个人信息。 第三个请求:获取用户时间线->保存与当前用户相关的帖子和事件 这是我的登录请求:在AsyncHttpClient任务中正确管理ProgressDialog以避免泄露windows Android,android,progressdialog,loopj,asynchttpclient,Android,Progressdialog,Loopj,Asynchttpclient,我正在开发一个android应用程序 我需要从我的应用程序向服务器发出几个请求,所以我使用的是AsyncHttpClient 我的应用程序的一部分有一个用户配置文件和一个时间表来显示一些事件。当用户登录时,我需要获取他们的个人资料信息和他们的时间线信息,为此,我必须向服务器发出3个不同的请求: 第一个请求:登录->将cookie和会话信息保存到SharedReferences中 第二个请求:获取配置文件->保存用户的个人信息。 第三个请求:获取用户时间线->保存与当前用户相关的帖子和事件 这是我
public static void login(final String email, final String password,
final Context context, final Context appContext, final Resources res) {
prgDialog = new ProgressDialog(context);
prgDialog.setMessage(res.getString(R.string.dialog_please_wait));
prgDialog.setCancelable(false);
prgDialog.show();
cookieStore = new PersistentCookieStore(appContext);
client.setCookieStore(cookieStore);
RequestParams params = new RequestParams();
params.put("user_session[email]", email);
params.put("user_session[password]", password);
client.addHeader("Accept", HEADER);
client.post(getAbsoluteUrl(LOGIN_PATH), params,
new JsonHttpResponseHandler() {
@Override
public void onFailure(int statusCode,
org.apache.http.Header[] headers,
java.lang.String responseString,
java.lang.Throwable throwable) {
prgDialog.hide();
if (statusCode == 404) {
Toast.makeText(context,
res.getString(R.string.error_404),
Toast.LENGTH_LONG).show();
} else if (statusCode == 500) {
Toast.makeText(context,
res.getString(R.string.error_500),
Toast.LENGTH_LONG).show();
} else if (statusCode == 401) {
Toast.makeText(context,
res.getString(R.string.login_401),
Toast.LENGTH_LONG).show();
} else {
Toast.makeText(
context,
"Unexpected Error occcured! [Most common Error: Device might not be connected to Internet or remote server is not up and running]",
Toast.LENGTH_LONG).show();
}
}
@Override
public void onSuccess(int statusCode, Header[] headers,
JSONObject response) {
if (statusCode == 200) {
// In this case the JSONOBject has the user
// credentials, such as user_id, person_id
// user_instance_type and user_instance_id
// Parse them into an object that has the same
// attributes
Gson gson = new Gson();
UserCredentials userCredentials = gson.fromJson(
response.toString(), UserCredentials.class);
setInitialPrefs(userCredentials, appContext);
// Get the user profile and save into the
// database
getUserProfile(userCredentials.getUser_id(),
context, appContext, prgDialog);
// Get the timeline
getWalls(true, context, appContext, prgDialog);
}
}
});
}
getUserProfile和getWalls这两个方法本身都是异步请求。代码如下:
public static void getUserProfile(int userId, final Context context,
final Context appContext, final ProgressDialog prgDialog) {
prgDialog.show();
cookieStore = new PersistentCookieStore(appContext);
client.setCookieStore(cookieStore);
client.addHeader("Accept", HEADER);
client.get(getAbsoluteUrl(USERS_PATH + userId),
new JsonHttpResponseHandler() {
@Override
public void onFailure(int statusCode,
org.apache.http.Header[] headers,
java.lang.String responseString,
java.lang.Throwable throwable) {
prgDialog.hide();
if (statusCode == 404) {
Log.d(TAG, "404 getting profile");
} else if (statusCode == 500) {
Toast.makeText(
context,
context.getResources().getString(
R.string.error_500),
Toast.LENGTH_LONG).show();
} else if (statusCode == 401) {
Log.d(TAG, "401 getting profile");
} else {
Log.d(TAG, "Error getting profile");
}
}
@Override
public void onSuccess(int statusCode, Header[] headers,
JSONObject response) {
if (statusCode == 200) {
// In this case the JSONOBject has the user
// profile
// Parse them into an object that has the same
// attributes
Gson gson = new Gson();
UserProfile userProfile = gson.fromJson(
response.toString(), UserProfile.class);
UserProfileController profileController = new UserProfileController(
context);
profileController.insertProfile(userProfile);
}
}
});
}
public static void getWalls(final boolean firstTime, final Context context,
Context appContext, final ProgressDialog prgDialog) {
cookieStore = new PersistentCookieStore(appContext);
prgDialog.show();
client.setCookieStore(cookieStore);
client.addHeader("Accept", HEADER);
client.get(getAbsoluteUrl(WALLS_PATH), new JsonHttpResponseHandler() {
@Override
public void onFailure(int statusCode,
org.apache.http.Header[] headers,
java.lang.String responseString,
java.lang.Throwable throwable) {
prgDialog.hide();
if (statusCode == 404) {
Log.d(TAG, "404 getting walls");
} else if (statusCode == 500) {
Toast.makeText(
context,
context.getResources().getString(
R.string.error_500),
Toast.LENGTH_LONG).show();
} else if (statusCode == 401) {
Log.d(TAG, "401 getting walls");
} else {
Log.d(TAG, "Error getting walls");
}
}
@Override
public void onSuccess(int statusCode, Header[] headers,
JSONObject response) {
if (statusCode == 200) {
Gson gson = new Gson();
TimelineController.getInstance(context);
Timeline timeline = gson.fromJson(response.toString(),
Timeline.class);
TimelineController.insertTimeline(timeline);
if (firstTime) {
prgDialog.hide();
Intent i = new Intent(context, TimelineActivity.class);
i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
| Intent.FLAG_ACTIVITY_CLEAR_TASK
| Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
((AuthActivity) context).finish();
} else {
prgDialog.hide();
Intent i = new Intent(context, TimelineActivity.class);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
| Intent.FLAG_ACTIVITY_CLEAR_TOP);
context.startActivity(i);
}
}
}
});
}
如果您看到了代码,那么我试图对progress对话框所做的是将其保持显示,直到最后一个请求完成(getWalls请求)
问题是,有时当我注销并与同一个或不同的用户再次登录时,会出现android.view.windowsleed异常,我认为这是因为我没有很好地管理我的进度对话框
如何正确管理进度对话框以避免窗口泄漏
希望有人能帮我,提前谢谢 假设问题是进度对话,您可以尝试使用静态显示方法之一。以下代码确保进度对话不会显示两倍或三倍:
if (progressDialog == null || !progressDialog.isShowing()) {
progressDialog = ProgressDialog.show(activity, "Changing password ...", "Please wait.", true, false);
}
你定义:
private ProgressDialog progressDialog;