gpt4 book ai didi

java - 如何在 AsyncTask 任务中调用 facebook 的 executeAndWait()

转载 作者:行者123 更新时间:2023-11-29 08:52:27 25 4
gpt4 key购买 nike

我正在努力改进我的设计并将东西放在适当的位置。所以我不想把我的逻辑放在我的 Activity 中。为此,我创建了以下类:

public class FacebookLoginActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.facebook_login_activity);

new FacebookName().execute(this);
}

private class FacebookName extends AsyncTask<Activity, Void, String> {

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

CustomStatusCallback customStatusCallback = new CustomStatusCallback() {

@Override
public void call(Session session, SessionState state, Exception exception) {
if (session.isOpened()) {
CustomGraphUserCallback customerGraphUserCallback = new CustomGraphUserCallback() {

@Override
public void onCompleted(GraphUser user, Response response) {
if (user != null) {
setResult(user.getName());
}
}
};

Request.newMeRequest(session, customerGraphUserCallback).executeAndWait();
}
}
};

Session.openActiveSession(params[0], true, customStatusCallback);
return customStatusCallback.getResult();
}

@Override
protected void onPostExecute(String result) {
TextView facebookName = (TextView) findViewById(R.id.facebookName);
facebookName.setText("Hi " + result);
}

};

}

这是我创建的其他自定义类,用于保存值并将其传递回 protected void onPostExecute(String result):

public abstract class CustomGraphUserCallback implements Request.GraphUserCallback {

private String result;

public String getResult() {
return result;
}

protected void setResult(String result) {
this.result = result;
}

}

这是我的 CustomStatusCallback:

public abstract class CustomStatusCallback implements StatusCallback {

private String result;

public String getResult() {
return result;
}

public void setResult(String result) {
this.result = result;
}
}

我收到以下异常:

02-28 21:32:08.781: E/AndroidRuntime(15215): android.os.NetworkOnMainThreadException
02-28 21:32:08.781: E/AndroidRuntime(15215): at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1144)
02-28 21:32:08.781: E/AndroidRuntime(15215): at java.net.InetAddress.lookupHostByName(InetAddress.java:385)
02-28 21:32:08.781: E/AndroidRuntime(15215): at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
02-28 21:32:08.781: E/AndroidRuntime(15215): at java.net.InetAddress.getAllByName(InetAddress.java:214)
02-28 21:32:08.781: E/AndroidRuntime(15215): at libcore.net.http.HttpConnection.<init>(HttpConnection.java:70)
02-28 21:32:08.781: E/AndroidRuntime(15215): at libcore.net.http.HttpConnection.<init>(HttpConnection.java:50)
02-28 21:32:08.781: E/AndroidRuntime(15215): at libcore.net.http.HttpConnection$Address.connect(HttpConnection.java:340)
02-28 21:32:08.781: E/AndroidRuntime(15215): at libcore.net.http.HttpConnectionPool.get(HttpConnectionPool.java:87)
02-28 21:32:08.781: E/AndroidRuntime(15215): at libcore.net.http.HttpConnection.connect(HttpConnection.java:128)
02-28 21:32:08.781: E/AndroidRuntime(15215): at libcore.net.http.HttpEngine.openSocketConnection(HttpEngine.java:316)
02-28 21:32:08.781: E/AndroidRuntime(15215): at libcore.net.http.HttpsURLConnectionImpl$HttpsEngine.makeSslConnection(HttpsURLConnectionImpl.java:461)
02-28 21:32:08.781: E/AndroidRuntime(15215): at libcore.net.http.HttpsURLConnectionImpl$HttpsEngine.connect(HttpsURLConnectionImpl.java:433)
02-28 21:32:08.781: E/AndroidRuntime(15215): at libcore.net.http.HttpEngine.sendSocketRequest(HttpEngine.java:290)
02-28 21:32:08.781: E/AndroidRuntime(15215): at libcore.net.http.HttpEngine.sendRequest(HttpEngine.java:240)
02-28 21:32:08.781: E/AndroidRuntime(15215): at libcore.net.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:282)
02-28 21:32:08.781: E/AndroidRuntime(15215): at libcore.net.http.HttpURLConnectionImpl.getResponseCode(HttpURLConnectionImpl.java:497)
02-28 21:32:08.781: E/AndroidRuntime(15215): at libcore.net.http.HttpsURLConnectionImpl.getResponseCode(HttpsURLConnectionImpl.java:134)
02-28 21:32:08.781: E/AndroidRuntime(15215): at com.facebook.Response.fromHttpConnection(Response.java:301)
02-28 21:32:08.781: E/AndroidRuntime(15215): at com.facebook.Request.executeConnectionAndWait(Request.java:1564)
02-28 21:32:08.781: E/AndroidRuntime(15215): at com.facebook.Request.executeBatchAndWait(Request.java:1463)
02-28 21:32:08.781: E/AndroidRuntime(15215): at com.facebook.Request.executeBatchAndWait(Request.java:1432)
02-28 21:32:08.781: E/AndroidRuntime(15215): at com.facebook.Request.executeBatchAndWait(Request.java:1414)
02-28 21:32:08.781: E/AndroidRuntime(15215): at com.facebook.Request.executeAndWait(Request.java:1386)
02-28 21:32:08.781: E/AndroidRuntime(15215): at com.facebook.Request.executeAndWait(Request.java:1268)
02-28 21:32:08.781: E/AndroidRuntime(15215): at com.blahblah.FacebookLoginActivity$FacebookName$1.call(FacebookLoginActivity.java:47)

你能帮帮我吗?同样,我知道如何通过使用 executeAsync() 并将所有内容放在主要操作中来使其工作。

在此先感谢小伙伴们:)

最佳答案

这是你如何做到的:

private class FBUserInfoTask extends AsyncTask<Session, Void, Map<String, Object>>
{
@Override
protected Map<String, Object> doInBackground(Session[] session) // session state should be OPENED
{
Request request = new Request(session[0], "me"); //use any graph api here instead of "me"
Response response = request.executeAndWait();
GraphObject obj = response.getGraphObject();
Map<String, Object> map = obj.asMap();
return map;
}

@Override
protected void onPostExecute(Map<String, Object> result)
{
super.onPostExecute(result);
Log.d("User Name:", (String) result.get("userId"));
}
}

关于java - 如何在 AsyncTask 任务中调用 facebook 的 executeAndWait(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22096789/

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