我正在尝试连接Facebook并在Facebook上获取我的详细信息,但是当我想获取我的名字时,我收到一个错误的JSONException:“”必须使用活动访问令牌来查询有关当前用户的信息“< / p>
我标记了它发生的那条线
非常感谢
这是我的班级:
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) {
}
});
}
}
这里是logcat:
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)
答案 0 :(得分:2)
我建议使用脸谱新的Android 3.0 Facebook库。它有一个很好的教程和预构建的类来管理状态和查询。它比以前的库更简单,更清洁
https://developers.facebook.com/docs/getting-started/facebook-sdk-for-android/3.0/