Android 等待从不同请求加载数据的最佳方法
我必须从不同的请求加载同一活动中的数据 请求1将加载一些数据,请求2将从另一个url加载数据 现在我称之为顺序 request1.run() request2.run() 现在的问题是,我需要在它们之前显示对话框,并在所有数据完成后隐藏它们Android 等待从不同请求加载数据的最佳方法,android,android-async-http,Android,Android Async Http,我必须从不同的请求加载同一活动中的数据 请求1将加载一些数据,请求2将从另一个url加载数据 现在我称之为顺序 request1.run() request2.run() 现在的问题是,我需要在它们之前显示对话框,并在所有数据完成后隐藏它们 有人能告诉我做这件事的最佳方法吗?尝试使用异步任务,方法如下: private class RunRequestsTasks extends AsyncTask<Void,Void,Void>{ private Context tCont
有人能告诉我做这件事的最佳方法吗?尝试使用异步任务,方法如下:
private class RunRequestsTasks extends AsyncTask<Void,Void,Void>{
private Context tContext;
@Override
protected void onPreExecute(){
//Build dialog here
}
@Override
protected void doInBackground(Void... voids) {
request1.run();
request2.run()
return null;
}
@Override
protected void onPostExecute(Void voidRet){
//Dismiss dialog here
}
}
私有类RunRequestsTasks扩展异步任务{
私人语境;
@凌驾
受保护的void onPreExecute(){
//在此处生成对话框
}
@凌驾
受保护的空位背景(空位…空位){
request1.run();
request2.run()
返回null;
}
@凌驾
受保护的void onPostExecute(void voidRet){
//在这里取消对话框
}
}
但是,如果没有更多的信息,很难给出更具针对性的答案。AsyncTask具有以下功能: doInBackground:执行长时间运行操作的代码在此方法中。当单击按钮执行onClick方法时,它调用接受参数的execute方法,并使用传递的参数自动调用doInBackground方法 onPostExecute:在doInBackground方法完成处理后调用此方法。doInBackground的结果将传递给此方法 onPreExecute:在调用doInBackground方法之前调用此方法 类登录扩展异步任务{
private final static String TAG = "LoginActivity.Login";
@Override
protected void onPreExecute()
{
super.onPreExecute();
Log.d(TAG, "Executando onPreExecute Login");
//inicia diálogo de progresso, mostranto processamento com servidor.
progressDialog = ProgressDialog.show(LoginActivity.this, "Autenticando", "Contactando o servidor, por favor, aguarde alguns instantes.", true, false);
}
@SuppressWarnings("unchecked")
@Override
protected String doInBackground(Object... parametros) {
Log.d(TAG, "Executando doInBackground Login");
Object[] params = parametros;
HttpClient httpClient = (HttpClient) params[0];
List<NameValuePair> listaParametros = (List<NameValuePair>) params[1];
String result = null;
try{
result = HttpProxy.httpPost(AnototudoMetadata.URL_AUTENTICACAO, httpClient, listaParametros);
}catch (IOException e) {
Log.e(TAG, "IOException, Sem conectividade com o servidor do Anototudo! " + e.getMessage());
e.printStackTrace();
return result;
}
return result;
}
@Override
protected void onPostExecute(String result)
{
super.onPostExecute(result);
if (result == null || result.equals("")) {
progressDialog.dismiss();
Alerta
.popupAlertaComBotaoOK(
"Dados incorretos",
"Os dados informados não foram encontrados no Sistema! Informe novamente ou cadastre-se antes pela internet.",
LoginActivity.this);
return;
}
Log.d(TAG, "Login passou persistindo info de login local no device");
ContentValues contentValues = new ContentValues();
contentValues.put(AnototudoMetadata.LOGIN_EMAIL, sLogin);
contentValues.put(AnototudoMetadata.LOGIN_SENHA, sSenha);
contentValues.put(AnototudoMetadata.LOGIN_SENHA_GERADA, result);
LoginDB loginDB = new LoginDB();
loginDB.addLogin(LoginActivity.this, contentValues);
Log.d(TAG, "Persistiu info de login no device, redirecionando para menu principal do Anototudo");
Log.d(TAG, "O retorno da chamada foi ==>> " + result);
// tudo ok chama menu principal
Log.d(TAG, "Device foi corretametne autenticado, chamando tela do menu principal do Anototudo.");
String actionName = "br.com.anototudo.intent.action.MainMenuView";
Intent intent = new Intent(actionName);
LoginActivity.this.startActivity(intent);
progressDialog.dismiss();
}
}
private final static String TAG=“LoginActivity.Login”;
@凌驾
受保护的void onPreExecute()
{
super.onPreExecute();
Log.d(标记“Executado onPreExecute登录”);
//inicia diálogo de progresso,mostranto processamento com Servisor。
progressDialog=progressDialog.show(LoginActivity.this,“Autenticando”,“Contactando servidor,por favor,aguarde alguns instants.”,true,false);
}
@抑制警告(“未选中”)
@凌驾
受保护字符串doInBackground(对象…参数){
Log.d(标签,“Executado doInBackground登录”);
对象[]参数=参数;
HttpClient HttpClient=(HttpClient)参数[0];
List listaParametros=(List)参数[1];
字符串结果=null;
试一试{
结果=HttpProxy.httpPost(AnototudoMetadata.URL_AUTENTICACAO,httpClient,listaParametros);
}捕获(IOE异常){
Log.e(标记“IOException,Sem conctividade com o servisor do anotototudo!”+e.getMessage());
e、 printStackTrace();
返回结果;
}
返回结果;
}
@凌驾
受保护的void onPostExecute(字符串结果)
{
super.onPostExecute(结果);
if(result==null | | result.equals(“”){
progressDialog.disclose();
艾尔塔
.popupAlertaComBotaoOK(
“护墙板不正确”,
“没有系统!信息更新和地籍更新与互联网有关。”,
后勤活动;
返回;
}
Log.d(标签,“登录密码持久化信息注销本地无设备”);
ContentValues ContentValues=新ContentValues();
contentValues.put(AnototudoMetadata.LOGIN_EMAIL,sLogin);
contentValues.put(AnototudoMetadata.LOGIN_SENHA,sSenha);
contentValues.put(AnototudoMetadata.LOGIN\u SENHA\u GERADA,result);
LoginDB LoginDB=新LoginDB();
loginDB.addLogin(LoginActivity.this,contentValues);
Log.d(标签,“Persistiu info de login no device,redReceionando para menu principal do Anototudo”);
Log.d(标记“O returno da chamada foi===>>”+结果);
//tudo ok chama菜单负责人
Log.d(标签为“设备的正确性,查曼多-特拉-多-多-多-多-多-多-多-多-多-多-多-多-多-多-多-多-多-多-多-多-多-多-多-多-多-多-多-多-多-多-多-多-多-;
String actionName=“br.com.anototudo.intent.action.MainMenuView”;
意向意向=新意向(actionName);
后勤活动。这。开始行动(意图);
progressDialog.disclose();
}
}