gpt4 book ai didi

java - Facebook 连接异常

转载 作者:行者123 更新时间:2023-12-02 07:11:22 24 4
gpt4 key购买 nike

我正在尝试连接 facebook 并在 facebook 中获取我的详细信息,但是当我想获取我的名字时,我收到一个 JSONException 错误:“”必须使用 Activity 访问 token 来查询有关当前用户的信息“

我标记了它发生的行

非常感谢

这是我的课:

public class MainActivity extends Activity {

private Button logInButton;
private TextView registerScreen;
private TextView userText;
private TextView passwordText;
private SharedPreferences loginPreferences;
private SharedPreferences.Editor loginPrefsEditor;
private Boolean saveLogin;
private CheckBox saveLoginCheckBox;
private Button buttonLoginLogout;
private ShareActionProvider provider;
private TextView forgotPassword;
private String userName="";


private String friendsId="";
private String friendsName="";
private List<String> freindsIdList =new ArrayList<String>();

String FILENAME = "AndroidSSO_data";
private SharedPreferences mPrefs;
private Facebook facebook;
private AsyncFacebookRunner mAsyncRunner;
private static String APP_ID = "xxxxxx" ;


public void onCreate(Bundle savedInstanceState) {



super.onCreate(savedInstanceState);

this.requestWindowFeature(Window.FEATURE_NO_TITLE);

setContentView(R.layout.login);

facebook = new Facebook(APP_ID);
mAsyncRunner = new AsyncFacebookRunner(facebook);

final LayoutInflater inflater = (LayoutInflater)this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

buttonLoginLogout=(Button) findViewById(R.id.facebooklog);

buttonLoginLogout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
loginToFacebook();
getProfileInformation();
}
});

public void loginToFacebook() {
mPrefs = getPreferences(MODE_PRIVATE);
String access_token = mPrefs.getString("access_token", null);
long expires = mPrefs.getLong("access_expires", 0);

if (access_token != null) {
facebook.setAccessToken(access_token);
}

if (expires != 0) {
facebook.setAccessExpires(expires);
}

if (!facebook.isSessionValid()) {
facebook.authorize(this,
new String[] { "email", "publish_stream" },
new DialogListener() {

@Override
public void onCancel() {
// Function to handle cancel event
}

@Override
public void onComplete(Bundle values) {
// Function to handle complete event
// Edit Preferences and update facebook acess_token
SharedPreferences.Editor editor = mPrefs.edit();
editor.putString("access_token",
facebook.getAccessToken());
editor.putLong("access_expires",
facebook.getAccessExpires());
editor.commit();
}

@Override
public void onError(DialogError error) {
// Function to handle error

}

@Override
public void onFacebookError(FacebookError fberror) {
// Function to handle Facebook errors

}

});
}
}




public void getProfileInformation() {
mAsyncRunner.request("me", new RequestListener() {
@Override
public void onComplete(String response, Object state) {
Log.d("Profile", response);
String json = response;
try {
JSONObject profile = new JSONObject(json);
// getting name of the user
final String name = profile.getString("name"); //this line i get an JSONException with error:
//"An active access token must be used to query information about the current user "
// getting email of the user
final String email = profile.getString("email");

runOnUiThread(new Runnable() {

@Override
public void run() {
Toast.makeText(getApplicationContext(), "Name: " + name + "\nEmail: " + email, Toast.LENGTH_LONG).show();
}

});

} catch (JSONException e) {
e.printStackTrace();
}
}

@Override
public void onIOException(IOException e, Object state) {
}

@Override
public void onFileNotFoundException(FileNotFoundException e,
Object state) {
}

@Override
public void onMalformedURLException(MalformedURLException e,
Object state) {
}

@Override
public void onFacebookError(FacebookError e, Object state) {
}
});
}

}

这是日志猫:

03-18 12:24:32.081: W/System.err(858): org.json.JSONException: No value for name
03-18 12:24:32.131: W/System.err(858): at org.json.JSONObject.get(JSONObject.java:354)
03-18 12:24:32.131: W/System.err(858): at org.json.JSONObject.getString(JSONObject.java:510)
03-18 12:24:32.150: W/System.err(858): at com.example.sale.MainActivity$3.onComplete(MainActivity.java:291)
03-18 12:24:32.150: W/System.err(858): at com.facebook.android.AsyncFacebookRunner$2.run(AsyncFacebookRunner.java:276)

最佳答案

我建议使用 facebook 的新 Android 3.0 Facebook 库。它有一个很好的教程和预构建的类来管理状态和执行查询。它比以前的库更简单、更干净

https://developers.facebook.com/docs/getting-started/facebook-sdk-for-android/3.0/

看看Wait till get my id and my freinds id facebook了解更多信息

关于java - Facebook 连接异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15476682/

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