我正在开发一个使用AsyncTask的Android应用程序。 问题是AsyncTask响应很慢,具有相当高的延迟,因此例如通过检查用户名和密码是否与数据库密码是正确的,因为延迟会导致用户无需验证即可自动登录或有时从不登录,因为这些信息将花费很长时间。
代码示例:
public JSONArray checkLogin(String username, String password) {
// URL for getting all customers
String url = endereco+"/Functions.php?checkLogin=1&username="+username+"&password="+password;
// Get HttpResponse Object from url.
// Get HttpEntity from Http Response Object
HttpEntity httpEntity = null;
try {
DefaultHttpClient httpClient = new DefaultHttpClient(); // Default HttpClient
HttpGet httpGet = new HttpGet(url);
HttpResponse httpResponse = httpClient.execute(httpGet);
httpEntity = httpResponse.getEntity();
} catch (ClientProtocolException e) {
// Signals error in http protocol
e.printStackTrace();
//Log Errors Here
} catch (IOException e) {
e.printStackTrace();
}
// Convert HttpEntity into JSON Array
JSONArray jsonArray = null;
if (httpEntity != null) {
try {
String entityResponse = EntityUtils.toString(httpEntity);
Log.e("Entity Response : ", entityResponse);
jsonArray = new JSONArray(entityResponse);
} catch (JSONException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
return jsonArray;
}
如何绕过延迟?