how to Parse facebook JSON response

前端 未结 2 715
-上瘾入骨i
-上瘾入骨i 2021-01-23 21:27

I think the return value from facebook is ambiguous.:

    loginButton.registerCallback(callbackManager, new FacebookCallback() {
        @Over         


        
2条回答
  •  梦毁少年i
    2021-01-23 21:35

    do it with this code. it's works for me.

    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();
    					}
    					name = object.getString("name");
    					email = object.getString("email");
    					gender = object.getString("gender");
    					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());
    	}
    });

提交回复
热议问题