我有这样的代码:类AttemptLogin扩展AsyncTask {
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(MyAkan.this);
pDialog.setMessage("Attempting login...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();
}
@Override
protected String doInBackground(String... arg0) {
int success;
String idnumber = etIdnumber.getText().toString();
String password = etPassword.getText().toString();
try {
// Building Parameters
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("idnumber", idnumber));
params.add(new BasicNameValuePair("password", password));
JSONObject json = jsonParser.makeHttpRequest(LOGIN_URL, "POST",
params);
success = json.getInt(TAG_SUCCESS);
if (success == 1) {
Log.d("Login Successful!", json.toString());
Intent i = new Intent("com.gpplsmje.mac.akan.AKANMENU");
String id = json.getString(TAG_IDNUMBER);
i.putExtra("idnumber", id);
finish();
startActivity(i);
return json.getString(TAG_MESSAGE);
} else if (success == 0) {
Log.d("Login Failure!", json.getString(TAG_MESSAGE));
return json.getString(TAG_MESSAGE);
}
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(String file_url) {
// TODO Auto-generated method stub
pDialog.dismiss();
if (file_url != null) {
Toast.makeText(MyAkan.this, file_url, Toast.LENGTH_LONG).show();
}
}
}此应用程序连接到服务器并返回编码的JSON数据。每当我在我的机器上的本地服务器上测试它时,它都工作得很好。它与服务器连接得很好。但当我关闭服务器并使用应用程序时,应用程序加载,但在尝试登录一分钟左右后停止。有没有一种方法可以将进度计时到最多20秒,然后如果它消耗了时间,它必须取消进度对话框并返回到主要活动?
谢谢。
发布于 2013-06-26 16:19:22
你的jsonParser对象使用HTTPURLConnection来做http请求吗?如果是这样的话,就有一个setConnectTimeout方法。
如果您正在使用DefaultHttpClient,请查看此问题How to set HttpResponse timeout for Android in Java
请记住,在Android2.2上,最好使用HTTPURLConnection。有关更多细节,请查看http://android-developers.blogspot.fr/2011/09/androids-http-clients.html。
https://stackoverflow.com/questions/17314446
复制相似问题