我正在处理 AsyncTask 以登录服务器,但是当我尝试在其中显示 progressdialog 时遇到错误。但是,它之前确实运行良好。这是我的代码。
登录活动.java
public class LoginActivity extends Activity {
public void loginButtonClick(View view) {
//TODO.Validatioin check here
LoginTask loginTask = new LoginTask();
loginTask.execute(app.getPortalWsUrl(app.AUTHENTICATION_URI) , getUserId(), getPassword());
}
private class LoginTask extends AsyncTask<String, String, JsonResult<Subscriber>> {
private SimpleHttpClient mClient;
private ProgressDialog mDialog;
public LoginTask(){
this.mClient = new SimpleHttpClient();
this.mDialog = new ProgressDialog(LoginActivity.this);
}
@Override
protected void onPreExecute() {
super.onPreExecute();
mDialog.setTitle(R.string.m_logging);
mDialog.setMessage(LoginActivity.this.getString(R.string.m_wait));
mDialog.show();
}
@Override
protected JsonResult<Subscriber> doInBackground(String... params) {
JsonResult<Subscriber> jsonResult = new JsonResult<Subscriber>();
if (params.length!=3) {
//jsonResult.setMessage("Invalid arguments to call method.");
return null;
}
String url = params[0];
String userId = params[1];
String password = params[2];
try {
StringBuilder sb = new StringBuilder();
sb.append(url)
.append("?")
.append("userid=").append(userId)
.append("&")
.append("password=").append(password);
String receivedText = mClient.postJson(sb.toString(), "");
JSONObject jsonObj = new JSONObject(receivedText);
jsonResult.setOk(jsonObj.optBoolean("ok"))
.setMessage(jsonObj.optString("message"));
if (jsonResult.isOk()){
JSONObject result = jsonObj.optJSONObject("result");
SubscriptionService service = parseResult(result);
Subscriber subsriber = new Subscriber();
subsriber.userId = userId;
subsriber.password = "";
subsriber.userName = result.optString("accountHolder");
subsriber.service = service;
jsonResult.setResult(subsriber);
}
} catch (Exception e) {
logger.error(e.getMessage()); //==>This was problem.
jsonResult.setOk(false).setMessage(e.getMessage());
}
logger.debug("###JsonResult###" + jsonResult);
return jsonResult;
}
@Override
protected void onPostExecute(JsonResult<Subscriber> result) {
super.onPostExecute(result);
if (mDialog.isShowing())
mDialog.dismiss();
//TODO.LoginDone(result)
}
/**
* @param value
* @return
* @throws UnsupportedEncodingException
*/
private String encode(String value) throws UnsupportedEncodingException{
return URLEncoder.encode(value, "UTF-8");
}
private SubscriptionService parseResult(JSONObject jsonObj) throws JSONException{
SubscriptionService service = new SubscriptionService();
//Do soething
return service;
}
}
}
日志
11-15 15:21:37.910: E/WindowManager(1748): Activity stalker.activity.LoginActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@40ffa4e0 that was originally added here
11-15 15:21:37.910: E/WindowManager(1748): android.view.WindowLeaked: Activity stalker.activity.LoginActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@40ffa4e0 that was originally added here
11-15 15:21:37.910: E/WindowManager(1748): at android.view.ViewRootImpl.<init>(ViewRootImpl.java:344)
11-15 15:21:37.910: E/WindowManager(1748): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:267)
11-15 15:21:37.910: E/WindowManager(1748): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:215)
11-15 15:21:37.910: E/WindowManager(1748): at android.view.WindowManagerImpl$CompatModeWrapper.addView(WindowManagerImpl.java:140)
11-15 15:21:37.910: E/WindowManager(1748): at android.view.Window$LocalWindowManager.addView(Window.java:537)
11-15 15:21:37.910: E/WindowManager(1748): at android.app.Dialog.show(Dialog.java:278)
11-15 15:21:37.910: E/WindowManager(1748): at stalker.activity.LoginActivity$LoginTask.onPreExecute(LoginActivity.java:262)
11-15 15:21:37.910: E/WindowManager(1748): at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:561)
11-15 15:21:37.910: E/WindowManager(1748): at android.os.AsyncTask.execute(AsyncTask.java:511)
11-15 15:21:37.910: E/WindowManager(1748): at activity.LoginActivity.onLoginButtonClick(LoginActivity.java:126)
如您所见,AsyncTask 中没有特殊代码。你能帮我解决这个问题吗?任何帮助,将不胜感激。
谢谢。