gpt4 book ai didi

android - 无法在 android 中导入 Session、Request、Response、GraphUser 包

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:22:39 26 4
gpt4 key购买 nike

我在我的 ecllipe 中导入了最新的 facebook sdk。当我试图在我的应用程序中获取基本信息时,它不会导入请求、响应和 session 的相应包。我要做什么?导入相应的包。我尝试了以下代码,但在导入相应包时遇到问题。

登录 Activity .java

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

if (Session.getActiveSession() == null
|| Session.getActiveSession().isClosed()) {
Session.openActiveSession(this, true, new StatusCallback() {

@Override
public void call(Session session, SessionState state,
Exception exception) {
System.out.println("State= " + state);

if (session.isOpened()) {
System.out.println("Token=" + session.getAccessToken());
Request.executeMeRequestAsync(session,
new GraphUserCallback() {
@Override
public void onCompleted(GraphUser user,
Response response) {
if (user != null) {
System.out.println("User=" + user);

}
if (response != null) {
System.out.println("Response="
+ response);
Toast.makeText(MainActivity.this,
response.toString(),
Toast.LENGTH_LONG).show();
}
}
});
}
if (exception != null) {
System.out.println("Some thing bad happened!");
exception.printStackTrace();
}
}
});
}
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Session.getActiveSession().onActivityResult(this, requestCode,
resultCode, data);
}

最佳答案

它发生在你身上,因为 session 从新的 android SDK 4 中删除。所以你不能使用这段代码来获取用户信息。

尝试使用此代码。它对我来说很成功。

在改变布局之前,首先在 onCreate 中初始化 FacebookSdk。

FacebookSdk.sdkInitialize(this.getApplicationContext());

loginButton = (LoginButton) findViewById(R.id.login_button);

List < String > permissionNeeds = Arrays.asList("user_photos", "email",
"user_birthday", "public_profile", "AccessToken");
loginButton.registerCallback(callbackManager,
new FacebookCallback < LoginResult > () {@Override
public void onSuccess(LoginResult loginResult) {

System.out.println("onSuccess");

String accessToken = loginResult.getAccessToken()
.getToken();
Log.i("accessToken", accessToken);

GraphRequest request = GraphRequest.newMeRequest(
loginResult.getAccessToken(),
new GraphRequest.GraphJSONObjectCallback() {@Override
public void onCompleted(JSONObject object,
GraphResponse response) {
Log.i("LoginActivity", response.toString());
try {
id = object.getString("id");
try {
URL profile_pic = new URL(
"http://graph.facebook.com/" + id + "/picture?type=large");
Log.i("profile_pic",
profile_pic + "");

} catch (MalformedURLException e) {
e.printStackTrace();
}
String name = object.getString("name");
String email = object.getString("email");
String gender = object.getString("gender");
String birthday = object.getString("birthday");
} catch (JSONException e) {
e.printStackTrace();
}
}
});
Bundle parameters = new Bundle();
parameters.putString("fields",
"id,name,email,gender, birthday");
request.setParameters(parameters);
request.executeAsync();
}

@Override
public void onCancel() {
System.out.println("onCancel");
}

@Override
public void onError(FacebookException exception) {
System.out.println("onError");
Log.v("LoginActivity", exception.getCause().toString());
}
});

添加方法。

@Override
protected void onActivityResult(int requestCode, int responseCode,
Intent data) {
super.onActivityResult(requestCode, responseCode, data);
callbackManager.onActivityResult(requestCode, responseCode, data);
}

关于android - 无法在 android 中导入 Session、Request、Response、GraphUser 包,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31824694/

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