我有这个代码: class 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秒,然后如果耗费时间则必须关闭progressdialog并返回主要活动?
感谢。
答案 0 :(得分:0)
你的jsonParser对象使用HTTPURLConnection来做http请求吗?如果有,则有方法setConnectTimeout。
如果您使用的是DefaultHttpClient,请查看此问题How to set HttpResponse timeout for Android in Java
请记住,在Android 2.2之上,最好使用HTTPURLConnection来查看http://android-developers.blogspot.fr/2011/09/androids-http-clients.html以获取更多详细信息。