我正在尝试将facebook sdk与我的Android应用程序集成,并希望检索登录用户的用户名,电子邮件和个人资料照片,但我收到系统错误。我的代码如下 -
package com.example.facebooklogin;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.MalformedURLException;
import org.json.JSONException;
import org.json.JSONObject;
import com.facebook.android.AsyncFacebookRunner;
import com.facebook.android.AsyncFacebookRunner.RequestListener;
import com.facebook.android.DialogError;
import com.facebook.android.Facebook;
import com.facebook.android.Facebook.DialogListener;
import com.facebook.android.FacebookError;
import android.net.ParseException;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends Activity implements OnClickListener
{
// Your Facebook APP ID
private static String APP_ID = "801614236517742"; // Replace your App ID here
//private static String APP_ID = "308180782571605"; // Replace your App ID here
// Instance of Facebook Class
private Facebook facebook;
@SuppressWarnings("deprecation")
private AsyncFacebookRunner mAsyncRunner;
String FILENAME = "AndroidSSO_data";
private SharedPreferences mPrefs;
Button btn;
String name;
String email;
@SuppressWarnings("deprecation")
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
facebook = new Facebook(APP_ID);
mAsyncRunner = new AsyncFacebookRunner(facebook);
btn=(Button)findViewById(R.id.btnFbLogin);
btn.setOnClickListener(this);
}
@Override
public void onClick(View v)
{
loginToFacebook();
//getProfileInformation();
}
@SuppressWarnings("deprecation")
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
}
});
}
else
getProfileInformation();
}
@SuppressWarnings("deprecation")
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
name = profile.getString("name");
// getting email of the user
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 onFacebookError(FacebookError e, Object state) {
}
@Override
public void onIOException(IOException e, Object state) {
// TODO Auto-generated method stub
}
@Override
public void onFileNotFoundException(FileNotFoundException e,
Object state) {
// TODO Auto-generated method stub
}
@Override
public void onMalformedURLException(MalformedURLException e,
Object state) {
// TODO Auto-generated method stub
}
});
}
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
facebook.authorizeCallback(requestCode, resultCode, data);
}
}
我的应用程序的Logcat -
05-22 06:55:53.774: W/dalvikvm(3713): VFY: unable to find class referenced in signature (Landroid/support/v4/app/Fragment;)
05-22 06:55:53.774: W/dalvikvm(3713): VFY: unable to find class referenced in signature (Landroid/support/v4/app/Fragment;)
05-22 06:55:53.784: I/dalvikvm(3713): Could not find method android.support.v4.content.LocalBroadcastManager.getInstance, referenced from method com.facebook.Session.postActiveSessionAction
05-22 06:55:53.784: W/dalvikvm(3713): VFY: unable to resolve static method 216: Landroid/support/v4/content/LocalBroadcastManager;.getInstance (Landroid/content/Context;)Landroid/support/v4/content/LocalBroadcastManager;
05-22 06:55:53.784: D/dalvikvm(3713): VFY: replacing opcode 0x71 at 0x0009
05-22 06:55:54.514: D/dalvikvm(3713): GC_FOR_ALLOC freed 224K, 9% free 3058K/3360K, paused 54ms, total 57ms
05-22 06:55:54.604: D/Facebook-Util(3713): GET URL: https://graph.facebook.com/me?access_token=CAAEhug8jDagBAP7mXTSAg9pqFittCCALpgdciZCzhWya9pqRH0sB5puv36fD5bta2dNGTKHctWvaoaIBbCIDKLZACX3fNVFAv75u0YOULJZC7MLEZBiEpSOmUADEmB51gllXRTEAi5ZCLAfJGXuKOJIl9ZBJLqiWZAK51nHs5prITm5HrFOUr64D6f2M26ZAxZAk0Y2l5AZATZCznmzCGSDksXQ&format=json
05-22 06:55:57.034: D/Profile(3713): {"error":{"message":"Error validating application. Application has been deleted.","type":"OAuthException","code":190}}
05-22 06:55:57.044: W/System.err(3713): org.json.JSONException: No value for name
05-22 06:55:57.044: W/System.err(3713): at org.json.JSONObject.get(JSONObject.java:355)
05-22 06:55:57.044: W/System.err(3713): at org.json.JSONObject.getString(JSONObject.java:515)
05-22 06:55:57.044: W/System.err(3713): at com.example.facebooklogin.MainActivity$2.onComplete(MainActivity.java:145)
05-22 06:55:57.084: W/System.err(3713): at com.facebook.android.AsyncFacebookRunner$2.run(AsyncFacebookRunner.java:276)