我正在尝试构建一个从Facebook个人资料中获取所有照片的应用程序,并且此代码位于UserProfileActivity中。但是启动此活动的意图位于 GraphRequest onCompleted 块中。正如我在调试时看到的那样,此块永远不会被执行。我尝试了很多来理解它,并看到了各种帖子,到处都是这样的代码。
public class MainActivity extends AppCompatActivity implements FacebookCallback<LoginResult>{
private LoginButton buttonLoginFacebook;
private TextView textViewMessage;
private CallbackManager callbackManager;
private String TAG = "MainActivity";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
FacebookSdk.sdkInitialize(getApplicationContext());
callbackManager = CallbackManager.Factory.create();
setContentView(R.layout.activity_main);
buttonLoginFacebook = (LoginButton)findViewById(R.id.buttonLoginFacebook);
buttonLoginFacebook.setReadPermissions(Arrays.asList("public_profile, user_photos, user_posts"));
textViewMessage = (TextView)findViewById(R.id.textViewMessage);
buttonLoginFacebook.registerCallback(callbackManager,this);
}
@Override
public void onSuccess(LoginResult loginResult) {
GraphRequest request = GraphRequest.newMeRequest(loginResult.getAccessToken(),
new GraphRequest.GraphJSONObjectCallback() {
@Override
public void onCompleted(JSONObject object, GraphResponse response) {
Log.e(TAG,object.toString());
Log.e(TAG,response.toString());
try{
String userId = object.getString("id");
URL profilePicture = new URL("https://graph.facebook.com/" + userId + "/picture?width=500&height=500");
String fullName="";
if(object.has("first_name"))
fullName += object.getString("first_name");
if(object.has("last_name"))
fullName += " " + object.getString("last_name");
Intent intent = new Intent(MainActivity.this,UserProfileActivity.class);
intent.putExtra("name",fullName);
intent.putExtra("imageUrl",profilePicture.toString());
startActivity(intent);
finish();
} catch (JSONException e) {
e.printStackTrace();
} catch (MalformedURLException e) {
e.printStackTrace();
}
}
});
Bundle parameters = new Bundle();
parameters.putString("fields", "id, first_name, last_name");
request.setParameters(parameters);
request.executeAsync();
}
@Override
public void onCancel() {
textViewMessage.setText("Login attempt cancelled");
}
@Override
public void onError(FacebookException error) {
textViewMessage.setText("Login attempt failed");
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
callbackManager.onActivityResult(requestCode, resultCode, data);
}
}
代码永远不会进入 onCompleted 块。因此,UserProfileActivity 的意图永远不会被执行。我对Facebook Sdk有点陌生,所以任何帮助将不胜感激。
它对我有用
LoginManager.getInstance().registerCallback(mCallbackManager,
new FacebookCallback<LoginResult>() {
@Override
public void onSuccess(LoginResult success) {
// TODO Auto-generated method stub
Log.d("facebookLogin", "successful"
+ success.getRecentlyGrantedPermissions()
.toString() + " " + success.toString());
AccessToken token = success.getAccessToken();
if (token == null) {
Log.e("FBaccessToken", "null");
} else {
Log.d("FBAccesstoken", token.toString());
getFBMeData(token);
}
}
@Override
public void onError(FacebookException e) {
if (e instanceof FacebookException) {
if (AccessToken.getCurrentAccessToken() != null) {
LoginManager.getInstance().logOut();
}
}
}
@Override
public void onCancel() {
// TODO Auto-generated method stub
Log.d("facebookLogin", "canceled by user");
}
});
}
public void getFBMeData(AccessToken atoken) {
Profile.fetchProfileForCurrentAccessToken();
GraphRequest request = GraphRequest.newMeRequest(
AccessToken.getCurrentAccessToken(),
new GraphRequest.GraphJSONObjectCallback() {
@Override
public void onCompleted(JSONObject object,
GraphResponse response) {
// TODO Auto-generated method stub
if (object != null) {
Log.d("FBGraphJSONObject", object.toString());
if(Profile.getCurrentProfile()!=null) {
FBdata.put("fbid", Profile.getCurrentProfile()
.getId());
FBdata.put("fname", Profile.getCurrentProfile()
.getFirstName());
FBdata.put("lname", Profile.getCurrentProfile()
.getLastName());
String gender = object.optString("gender");
String dob = object.optString("birthday");
String locationName = "";
JSONObject location = object
.optJSONObject("location");
if (location != null) {
locationName = location.optString("name");
}
String pictureUrl = "", email = "";
JSONObject picture = object
.optJSONObject("picture");
JSONObject data = picture.optJSONObject("data");
try {
email = URLDecoder.decode(
object.optString("email"), "UTF-8");
if (picture != null) {
pictureUrl = URLDecoder.decode(
data.optString("url"), "UTF-8");
}
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
FBdata.put("photo", pictureUrl);
FBdata.put("address", locationName);
FBdata.put("dob", dob);
FBdata.put("gender", gender);
FBdata.put("email", email);
}
}
});
Bundle params = new Bundle();
params.putString("fields", "gender,email,birthday,location,picture");
request.setParameters(params);
request.executeAsync();
}
我遇到了同样的问题,问题是android.os.NetworkOnMainThreadException,所以请确保在单独的线程中调用网络请求。