繁体   English   中英

分配AsyncTask返回的值

[英]Assign value returned by AsyncTask

public class Login extends ActionBarActivity{
.
.
.
// I have this method to run If the login button is pressed.
public void login(View v){

// I am performing all verification and validation
// Just not showing it here
// To store the keys
String api_key_from_database;
String api_key_from_network;

//Gets the key from the database
api_key_from_database=db.getApiKey(database,email);
api_key_from_network=httpRequest.getHttpResponse(email,password);;// I am having problems with this assignment

//Compare the two keys to authenticate
if(api_key_from_database.equals(api_key_from_network)){
       //Start the main activity
       //This is just placeholder to start the actual activity.
       startActivity(new Intent(this,MainActivity.class));
       //End this login activity
       finish();
   }
else{
// Throw error
}
}
}

由于getHttpResponse()执行网络活动,因此无法在Main Thread上运行它。

在我给你行api_key_from_network被返回的值getHttpResponse方法,我似乎无法同时使用分配ThreadAsyncTask

在使用Thread ,由于未将变量api_key_from_network声明为final ,所以我无法访问该变量。

new Thread(){
  public void run(){
  }
}.start();

如果将其声明为final ,则无法修改该值。

现在使用AsyncTask ,我不确定如何返回值。 doPostExecution() ,我似乎也无法分配该值。

您能帮我将方法返回的值分配给api_key_from_network

您的变量api_key_from_network对于您的班级是全局的。 因此,请使用AsyncTask进行网络操作并获取数据,然后在Post Execution后将其分配给变量。

public class get_key extends AsyncTask<String, String, String> {

ProgressDialog dialog;

    @Override
    protected void onPreExecute (){
    //Show Dialog Here
        super.onPreExecute ();
    }


    @Override
    protected String doInBackground (String... params){

    // Do Network Calling
    return string;
    }

    @Override
    protected void onPostExecute (String result){

     if(dialog.isShowing()){
     dialog.dismiss();
     }
    // put condition to check data
    api_key_from_network=reuslt;
    }
 }

只需运行new get_key().execute(); 您想从网络获取密钥的位置。

对此进行检查,它正在使用接口。 能否从这个示例中找出一些想法

public interface AsyncTaskListener<String> {
    public void onReceivingApiKey(String result);
}

public class AsyncTaskGetApiKey extends AsyncTask<Void, Void, String>{

    ArrayList<AsyncTaskListener<String>> listeners;

    public AsyncTaskLoadingInSplash() {
        listeners = new ArrayList<AsyncTaskListener<String>>();
    }

    public void addListener(AsyncTaskListener<String> listener){
        listeners.add(listener);
    }

    public void removeListener(AsyncTaskListener<String> listener){
        listeners.remove(listener);
    }


    @Override
    protected String doInBackground(Void... params) {
        return httpRequest.getHttpResponse(email,password);
    }

    @Override
    protected void onPostExecute(String result) {
        // TODO Auto-generated method stub
        super.onPostExecute(result);
        for (AsyncTaskListener<String> listener:listeners) {
            listener.onReceivingApiKey(result);
        }
    }
}

public class MyActivity extends Activity
implements AsyncTaskListener<String>{

AsyncTaskGetApiKey task;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        task = new AsyncTaskGetApiKey();
        task.addListener(this);
    }


    @Override
    public void onReceivingApiKey(String result) {
        //use the returned value
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM