尝试打开具有待处理请求的会话

时间:2016-05-16 07:58:01

标签: android facebook-graph-api

我在我的应用程序中使用Facebook功能进行登录和注册,问题是一旦用户点击使用Facebook按钮登录并且对话框出现用于登录,如果用户取消该对话框,则将其重定向到登录,如果它再次点击登录与Facebook白屏幕出现..日志猫显示错误为“尝试打开一个有待处理请求的会话”下面是我的代码..我很困惑这个错误任何帮助是appriciated

FaceBookIntegration.java / **  *  * /

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.util.Base64;
import android.util.Log;


import com.facebook.HttpMethod;
import com.facebook.Request;
import com.facebook.Response;
import com.facebook.Session;
import com.facebook.SessionState;
import com.facebook.model.GraphUser;

import java.io.ByteArrayOutputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

;

/**
 * Facebook integration class ccontains login with fb and share activity
 *
 * @author Admin
 */
public class FacebookIntegration extends GlobalActivity implements ResponseListener {

    private ProgressDialog progressDialog;
    private Session session;
    private boolean share;
    private UserHistory userHistory = null;
    Preferences preferences;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        preferences = HWUtil.getPreferences(this);

        share = getIntent().getBooleanExtra(HWUtil.SHARE, false);
        if (share) {
            userHistory = preferences.getShareUserHistory();
            if (Validator.isNull(userHistory)) {
                finish();
            }
        }
        Session.StatusCallback statusCallback = new SessionStatusCallback();
        session = Session.getActiveSession();
        if (session == null) {
            session = new Session(this);
            Session.setActiveSession(session);
        }
        if (!session.isOpened() && !session.isClosed()) {
            session.openForPublish(new Session.OpenRequest(this).setPermissions(Arrays.asList("public_profile", "email", "publish_actions", "user_actions.fitness")).setCallback(statusCallback));
        } else {
            if (share) {
                shareOnFacebook();
            } else {
                getProfileDetail(session);
            }
        }
    }

    /**
     * Share activity records on facebook
     */
    private void shareOnFacebook() {
        if (Validator.isNotNull(userHistory)) {
            String apiUrl;
            if ("Running".equals(userHistory.getSportName())) {
                apiUrl = "fitness.runs";
            } else if ("Walking".equals(userHistory.getSportName())) {
                apiUrl = "fitness.walks";
            } else if ("Cycling".equals(userHistory.getSportName())) {
                apiUrl = "fitness.bikes";
            } else {
                apiUrl = "*:sport";
            }

            Bundle params = new Bundle();
            params.putString("course", com.aimdek.healthwel.network.Request.HOST_URL + "/user-history/" + userHistory.getUserId() + "/" + userHistory.getId());
            params.putBoolean("fb:explicitly_shared", true);
            /* make the API call */
            new Request(Session.getActiveSession(), "/me/" + apiUrl, params, HttpMethod.POST, new Request.Callback() {
                public void onCompleted(Response response) {
                    if (response.getError() == null) {
                        com.aimdek.healthwel.network.Request.getRequest().sendRequest(com.aimdek.healthwel.network.Request.SHARE_USER_HISTORY, FacebookIntegration.this, FacebookIntegration.this, RequestParameterBuilder.buildMapForShareUserHistory(userHistory.getId(), FacebookIntegration.this));
                    } else {
                        finish();
                        Log.d("Facebook error", response.getError().toString());
                    }
                }
            }).executeAsync();
            if (Validator.isNotNull(userHistory.getHistoryPictures()) && userHistory.getHistoryPictures().length > 0) {
                for (int i = 0; i < userHistory.getHistoryPictures().length; i++) {
                    HistoryPictures pictures = userHistory.getHistoryPictures()[i];
                    String message = getString(R.string.facebook_status, HWUtil.getFullName(preferences.getUserInfo().getFirstName(), preferences.getUserInfo().getLastName()), userHistory.getSportName());
                    params.putString("message", message);
                    if (Validator.isNotNull(pictures.getPictureUrl())) {
                        params.putString("url", pictures.getPictureUrl());
                    } else {
                        params.putByteArray("picture", Base64.decode(pictures.getData(), Base64.DEFAULT));
                        // params.putString("method", "photos.upload");
                    }
                    new Request(Session.getActiveSession(), "/me/photos", params, HttpMethod.POST, new Request.Callback() {
                        public void onCompleted(Response response) {
                            if (response.getError() == null) {
                            } else {
                            }
                        }
                    }).executeAsync();
                }
            }
        }
    }

    @Override
    public void onResponse(int type, Object... result) {
        if (type == Request.SHARE_USER_HISTORY) {
            preferences.setShareOnFacebook(false);
            if (preferences.isShareOnGoogle()) {
                Intent intent = new Intent(FacebookIntegration.this, GooglePlusIntegration.class);
                intent.putExtra(HWUtil.SHARE, true);
                startActivity(intent);
            } else {
                preferences.setShareUserHistory(null);

            }
        } else {
            if (Validator.isNotNull(result) && result.length > 0 && Validator.isNotNull(result[0])) {
                String response = (String) result[0];
                HWUtil.loadMainActivity(response, FacebookIntegration.this);
            }
        }
        finish();
    }


    private class SessionStatusCallback implements Session.StatusCallback {

        @Override
        public void call(Session session, SessionState state, Exception exception) {
            if (session.isOpened()) {
                if (share) {
                    shareOnFacebook();
                } else {
                    getProfileDetail(session);
                }

            }
        }

    }

    /**
     * get facebook profile detail
     *
     * @param session - pass facebook session
     */
    private void getProfileDetail(Session session) {
        showProgressDialog();
        Request.newMeRequest(session, new Request.GraphUserCallback() {
            @Override
            public void onCompleted(GraphUser user, Response response) {
                if (Validator.isNotNull(user)) {
                    UserInfo userInfoDto = new UserInfo();
                    String id = user.getId();
                    userInfoDto.setFirstName(user.getFirstName());
                    userInfoDto.setLastName(user.getLastName());
                    String email = Validator.isNotNull(user.asMap().get("email")) ? user.asMap().get("email").toString() : HWUtil.BLANK;
                    if (Validator.isNotNull(email)) {
                        userInfoDto.setEmail(email);
                        String gender = Validator.isNotNull(user.asMap().get("gender")) ? user.asMap().get("gender").toString() : HWUtil.BLANK;
                        if ("male".equals(gender)) {
                            userInfoDto.setGender(1);
                        } else {
                            userInfoDto.setGender(2);
                        }
                        userInfoDto.setUniqueId(HWUtil.getDeviceId(FacebookIntegration.this));
                        userInfoDto.setMeasureUnit(HWUtil.getMeasureUnit(FacebookIntegration.this));
                        userInfoDto.setPassword(HWUtil.BLANK);
                        String imageURL = "https://graph.facebook.com/" + id + "/picture?type=large";
                        userInfoDto.setProfilePictureUrl(imageURL);
                        Request.getRequest().sendJsonRequest(Request.UPDATE_USER, FacebookIntegration.this, FacebookIntegration.this, RequestParameterBuilder.buildJsonObjectFromUserInfo(userInfoDto));
                        dismissProgressDialog();
                    } else {
                        HWUtil.showToast(FacebookIntegration.this, getString(R.string.allow_read_email));
                        dismissProgressDialog();
                        finish();
                        return;
                    }
                }
            }

        }).executeAsync();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode == Activity.RESULT_OK) {
            if (session != null && !session.isOpened()) {
                Session.getActiveSession().onActivityResult(this, requestCode, resultCode, data);
            }
        } else {
            finish();
        }
        super.onActivityResult(requestCode, resultCode, data);
    }

    private void showProgressDialog() {
        if(!isFinishing() && progressDialog==null) {
            progressDialog = new ProgressDialog(this);
            progressDialog.setCancelable(false);
            progressDialog.show();

        }

    }

    /**
     * dismiss Progress Dialog.
     */
    private void dismissProgressDialog() {
        if (!isFinishing() &&progressDialog!=null && progressDialog.isShowing()) {
            progressDialog.dismiss();
        }
    }

}

0 个答案:

没有答案