gpt4 book ai didi

java - AsyncTask 和回调 Android

转载 作者:行者123 更新时间:2023-11-30 04:14:37 25 4
gpt4 key购买 nike

我需要使用 facebok 登录,因此我下载了 sdk 并创建了我的方法。

在我获得用户已登录的信息后,我需要将其发送到我的网络服务并回调我的 Activity 。

这是我的代码。 onComplete 我试图解释我需要什么:好吧,基本上我需要回调到我当前的 Activity 。

public class DisplayLoginActivity extends FragmentActivity {



/** Called when the activity is first created. */
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.display_login_activity);

login();
}

public void login(){
facebook = new Facebook(kAppId);

facebook.authorize(this, new DialogListener() {

@Override
public void onComplete(Bundle values) {
System.out.println("onComplete");
JSONObject me = null;
me = new JSONObject(facebook.request("me"));

String id = me.getString("id");

//I want here to call other async request and make it call back to DisplayLoginActivity, for that
//reason i created callback with interface so i need to give this of my Activity
//If i give this so it's DialogListener this. I tried to do this DisplayLoginActivity.this but it crashes
}


@Override
public void onFacebookError(FacebookError error) {
error.getMessage();

}

@Override
public void onError(DialogError e) {

}

@Override
public void onCancel() {

}

});
}

}

也许你有一些想法?谢谢。

编辑:

04-20 08:34:50.270: E/AndroidRuntime(7642): FATAL EXCEPTION: Thread-10
04-20 08:34:50.270: E/AndroidRuntime(7642): java.lang.ExceptionInInitializerError
04-20 08:34:50.270: E/AndroidRuntime(7642): at com.my.main.DisplayLoginActivity$1$1.run(DisplayLoginActivity.java:161)
04-20 08:34:50.270: E/AndroidRuntime(7642): at java.lang.Thread.run(Thread.java:1019)
04-20 08:34:50.270: E/AndroidRuntime(7642): Caused by: java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
04-20 08:34:50.270: E/AndroidRuntime(7642): at android.os.Handler.<init>(Handler.java:121)
04-20 08:34:50.270: E/AndroidRuntime(7642): at android.os.AsyncTask$InternalHandler.<init>(AsyncTask.java:421)
04-20 08:34:50.270: E/AndroidRuntime(7642): at android.os.AsyncTask$InternalHandler.<init>(AsyncTask.java:421)
04-20 08:34:50.270: E/AndroidRuntime(7642): at android.os.AsyncTask.<clinit>(AsyncTask.java:152)
04-20 08:34:50.270: E/AndroidRuntime(7642): ... 2 more

最佳答案

我不熟悉 Facebook API,但您是否尝试过在单独线程(在本例中为 onComplete( ))?它应该是线程中的第一个函数调用。

关于java - AsyncTask 和回调 Android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10240536/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com