Android异步任务延迟[需要建议]
我正在开发一个使用AsyncTask的android应用程序。 问题是AsyncTask响应速度慢,延迟很高,因此,例如,通过检查用户名和密码与数据库密码是否正确(因为延迟),会导致用户在未经验证的情况下自动登录,或者有时由于信息等待时间太长而从未登录 代码示例:Android异步任务延迟[需要建议],android,android-asynctask,Android,Android Asynctask,我正在开发一个使用AsyncTask的android应用程序。 问题是AsyncTask响应速度慢,延迟很高,因此,例如,通过检查用户名和密码与数据库密码是否正确(因为延迟),会导致用户在未经验证的情况下自动登录,或者有时由于信息等待时间太长而从未登录 代码示例: public JSONArray checkLogin(String username, String password) { // URL for getting all customers String url =
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;
}
我如何绕过延迟?使用一些rest客户端并检查服务器的响应时间是多还是少?您是否可以控制与您交谈的服务器上的代码或数据库?听起来您的数据库缺少一些索引或其他内容。理想情况下,无论有多少条记录,与您交谈的任何web服务在登录时的延迟都不应超过300毫秒。后端延迟应始终计入您的应用程序中。由于延迟,默认情况下允许用户进入是应用程序的一个严重问题。诚然,需要优化备份,但允许用户在没有有效凭据的情况下登录并不好。