Java 在异步任务之后运行代码
因此,我正在寻找一种方法,在异步任务完成后只运行一段代码。代码与Java 在异步任务之后运行代码,java,android,asynchronous,facebook-sdk-3.14.x,Java,Android,Asynchronous,Facebook Sdk 3.14.x,因此,我正在寻找一种方法,在异步任务完成后只运行一段代码。代码与请求相关。executeAsync()。我将用于解释的代码是: public class Home extends Activity { TextView welcomeTextView; private JSONObject userProfile; @Override protected void onCreate(Bundle savedInstanceState) { su
请求相关。executeAsync()。我将用于解释的代码是:
public class Home extends Activity {
TextView welcomeTextView;
private JSONObject userProfile;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
userProfileFetched = false;
welcomeTextView = (TextView)findViewById(R.id.welcome);
populateUserProfile();
Log.d("USER_PROFILE", userProfile.toString()); //NullPointerException occurs here
}
private void populateUserProfile() {
Request.newMeRequest(Session.getActiveSession(), new Request.GraphUserCallback() {
@Override
public void onCompleted(GraphUser user, Response response) {
userProfile = response.getGraphObject().getInnerJSONObject();
}
}).executeAsync();
}
}
上面带有注释的行指向我的应用程序意外关闭的行。当我第一次开始这个活动时,onCreate
方法会被一些内部事务调用。然后调用函数populateUserProfile()
,该函数异步执行me
请求。如果我将response.getGraphObject().getInnerJSONObject().toString()
记录在onCompleted()
函数中,我可以清楚地看到我正在寻找的完美结果,即登录用户的JSONObject,但正如上面的代码所示,如果我将该函数的返回值赋给userProfile
变量,然后在调用onCreate()
方法中的函数后记录它,那么我的应用程序会意外停止,并在log
行抛出NullPointerException
我知道这是因为当我记录userProfile
JSONObject时,me
请求还没有完成,这就是为什么它还没有分配给任何对象,所以我需要知道如何在populateUserProfile()中等待onCompleted
方法
功能,以便将userProfile
对象成功分配给登录用户的信息
换句话说,我如何等待me
请求的异步任务完成,然后将userProfile
记录到LogCat中
希望我的回答清楚,并期待得到一个解释性的答案。想到的解决方案是使用AsyncTask
,然后在onPostExecute
方法中调用您的函数
编辑:这是一个来自开发人员的示例,不要看它是做什么的,只要理解它的概念即可。在doInBackground
方法中,所有耗时的工作都完成了,当它完成时,它会调用onPostExecute
,因此您可以安全地假设在它里面所有的Internet工作都完成了,您可以继续
private class DownloadFilesTask extends AsyncTask<URL, Integer, Long> {
protected Long doInBackground(URL... urls) {
int count = urls.length;
long totalSize = 0;
for (int i = 0; i < count; i++) {
totalSize += Downloader.downloadFile(urls[i]);
publishProgress((int) ((i / (float) count) * 100));
// Escape early if cancel() is called
if (isCancelled()) break;
}
return totalSize;
}
protected void onProgressUpdate(Integer... progress) {
setProgressPercent(progress[0]);
}
protected void onPostExecute(Long result) {
showDialog("Downloaded " + result + " bytes");
// here you call the function, the task is ended.
}}
私有类下载文件任务扩展异步任务{
受保护的长doInBackground(URL…URL){
int count=url.length;
长totalSize=0;
for(int i=0;i
populateUserProfile(){
新建异步任务(){
@凌驾
受保护的JSONObject doInBackground(无效参数){
返回响应.getGraphObject().getInnerJSONObject();
}
@凌驾
受保护的void onPostExecute(JSONObject结果){
Log.d(“USER_PROFILE”,result.toString());
}
}.execute();
}
谢谢,但你能给我一点代码片段说明我如何使用它吗?@MohammadAreebSiddiqui Android文档有一个很好的示例:你是说Log.I(…)?也许你没有在logcat中选择你的应用程序,在filters.Log.d(“USER_PROFILE”,userProfile.toString()),问题在于userProfile,这意味着对象为null,这就是为什么不能对不存在的对象调用toString()。尝试类似于:Log.d(“USER_PROFILE”,“TEST”)。我在哪里使用它?你能将这段代码整合到我的代码中吗,我无法做到这一点:/@MohammadAreebSiddiqui只需在populateUserProfile()
中获取您的响应
对象,您就可以执行您的代码了
populateUserProfile(){
new AsyncTask<Void, Void, JSONObject>() {
@Override
protected JSONObject doInBackground(Void params) {
return response.getGraphObject().getInnerJSONObject();
}
@Override
protected void onPostExecute(JSONObject result) {
Log.d("USER_PROFILE", result.toString());
}
}.execute();
}