Android社交登录Quickblox聊天

时间:2014-05-29 11:50:31

标签: android api quickblox

这是我的代码。我已经阅读了quickblox文档并下载了样本。我不明白为什么这不起作用!!

我有访问令牌,我将它传递给quickblox以登录它(我可以在仪表板中看到它)但它没有签到我聊天而是我收到一个大错误,说聊天不是&#39 ; t签到。

请帮忙。网站上的代码示例很好,但我确定我只是把东西放在错误的地方或者遗漏了一些东西。我在我的系绳结束了!!

public class LoginActivity extends Activity {

private String accessToken;
private String accessTokenSecret;

private QBUser user;
private SmackAndroid smackAndroid;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    //setContentView(R.layout.activity_login);
    Toast.makeText(this,"this is the login part", 3000).show();
}
    smackAndroid = SmackAndroid.init(this);

    // get stored AccessToken in shared Preferences
    SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(this);

    accessToken = settings.getString( "twitter_access_token", null );
    accessTokenSecret = settings.getString( "twitter_access_token_secret", null );

    //AccessToken access_token = new AccessToken(accessToken,accessTokenSecret);

    //String TwitToken = String.valueOf(access_token);



    // have to start session and pass session ID
    String TwitToken = accessToken;
    String TwitTokenS = accessTokenSecret;
    QBUsers.signInUsingSocialProvider(QBProvider.TWITTER, TwitToken, TwitTokenS, new QBCallbackImpl(){

        @Override
        public void onComplete(Result result){
            if(result.isSuccess()){
                //QBUserResult qbUserResult = (QBUserResult) result;

                QBUser qbUser = ((QBUserResult)result).getUser();
                qbUser.setPassword(BaseService.getBaseService().getToken());
                QBChatService.getInstance().signIn(qbUser);
                //QBUsers.signIn(qbUser, null);

                //loginToChat(qbUser);
                Log.d("LogIn was successful","user: " + qbUser.toString());

                Intent intent = new Intent();
                setResult(RESULT_OK, intent);
                finish();
            }else{
                Log.e("QB didn't login with Twitter",result.getErrors().toString()); 
            }
        }
    });
}

 @Override
    public void onBackPressed() {
        super.onBackPressed();
        Intent intent = new Intent();
        setResult(RESULT_CANCELED, intent);
        finish();
    }
 @Override
    protected void onDestroy() {
        smackAndroid.onDestroy();
        super.onDestroy();
    }
 //  @Override
 //     public void onComplete(Result result, Object context) {
 //     } 

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.login, menu);
    return true;
}

}

2 个答案:

答案 0 :(得分:0)

您使用的是哪个版本的Android SDK? 请在此处下载最新版本http://quickblox.com/developers/Android#Framework_changelog

您是否查看了此Android聊天指南? http://quickblox.com/developers/Android_XMPP_Chat_Sample

要登录聊天,您必须使用此方法:

QBUser qbUser = ((QBUserResult)result).getUser();
qbUser.setPassword(BaseService.getBaseService().getToken());

Log.d(TAG, "user: " + qbUser);

QBChatService.getInstance().loginWithUser(qbUser, LoginActivity.this);

@Override
public void onLoginSuccess() {
    Log.d(TAG, "success when login");
}

@Override
public void onLoginError(String error) {
    Log.e(TAG, "error when login");
}

你在日志中看到了什么?

答案 1 :(得分:0)

好的,这是我的LoginActivity。

public class LoginActivity extends Activity implements QBCallback {

    protected static final String TAG = LoginActivity.class.getSimpleName();
    private String accessToken;
    private String accessTokenSecret;

    private QBUser user;
    private SmackAndroid smackAndroid;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //setContentView(R.layout.activity_login);
        Toast.makeText(this,"this is the login part", 3000).show();

        smackAndroid = SmackAndroid.init(this);

        // get stored AccessToken in shared Preferences
        SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(this);

        accessToken = settings.getString( "twitter_access_token", null );
        accessTokenSecret = settings.getString( "twitter_access_token_secret", null );

        //AccessToken access_token = new AccessToken(accessToken,accessTokenSecret);

        //String TwitToken = String.valueOf(access_token);



        // have to start session and pass session ID
        String TwitToken = accessToken;
        String TwitTokenS = accessTokenSecret;

            QBUsers.signInUsingSocialProvider(QBProvider.TWITTER,TwitToken,TwitTokenS,new QBCallbackImpl(),getApplicationContext());

        }   


    @Override
    public void onComplete(Result result){
        if(result.isSuccess()){

            QBSessionResult res = (QBSessionResult)result;
            QBUser qbUser = ((QBUserResult)result).getUser();
            try {
                qbUser.setPassword(BaseService.getBaseService().getToken());
            } catch (BaseServiceException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            //
            QBChatService.getInstance().loginWithUser(qbUser, new SessionCallback() {
                @Override
                public void onLoginSuccess(){
//                  Intent intent = new Intent();
//                  
                    Intent intentChat = new Intent(LoginActivity.this, HomeActivity.class);
                    setResult(RESULT_OK,intentChat);
                    startActivity(intentChat);
                    finish();
                    }
                @Override
                public void onLoginError(String error){
                    Log.i(TAG, "error when login"); 
                }
                });
            }
        }
     @Override
        public void onBackPressed() {
            super.onBackPressed();
            Intent intent = new Intent();
            setResult(RESULT_CANCELED, intent);
            finish();
        }
     @Override
        protected void onDestroy() {
            smackAndroid.onDestroy();
            super.onDestroy();
        }
     @Override
        public void onComplete(Result result, Object context) {
        } 

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.login, menu);
        return true;
    }



}

这是我的chatActivity

public class ChatActivity extends Activity {

    public static final String EXTRA_MODE = "mode";
    private static final String TAG = ChatActivity.class.getSimpleName();
    private EditText messageEditText;
    private Mode mode = Mode.SINGLE;
    private Chat chat;
    private ChatAdapter adapter;
    private ListView messagesContainer;

    public static void start(Context context, Bundle bundle) {
        Intent intent = new Intent(context, ChatActivity.class);
        intent.putExtras(bundle);
        context.startActivity(intent);
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_chat);

        initViews();
    }

    @Override
    public void onBackPressed() {
        try {
            chat.release();
        } catch (XMPPException e) {
            Log.e(TAG, "failed to release chat", e);
        }
        super.onBackPressed();
    }

    private void initViews() {
        messagesContainer = (ListView) findViewById(R.id.messagesContainer);
        messageEditText = (EditText) findViewById(R.id.messageEdit);
        Button sendButton = (Button) findViewById(R.id.chatSendButton);
        TextView meLabel = (TextView) findViewById(R.id.meLabel);
        TextView companionLabel = (TextView) findViewById(R.id.companionLabel);
        RelativeLayout container = (RelativeLayout) findViewById(R.id.container);

        adapter = new ChatAdapter(this, new ArrayList<ChatMessage>());
        messagesContainer.setAdapter(adapter);

        Intent intent = getIntent();
        mode = (Mode) intent.getSerializableExtra(EXTRA_MODE);
        switch (mode) {
            case GROUP:
                //chat = new RoomChat(this);
                container.removeView(meLabel);
                container.removeView(companionLabel);
                break;
            case SINGLE:
                chat = new SingleChat(this);
                int userId = intent.getIntExtra(SingleChat.EXTRA_USER_ID, 0);
                companionLabel.setText("user(id" + userId + ")");
                restoreMessagesFromHistory(userId);
                break;
        }

        sendButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String lastMsg = messageEditText.getText().toString();
                if (TextUtils.isEmpty(lastMsg)) {
                    return;
                }

                messageEditText.setText("");
                try {
                    chat.sendMessage(lastMsg);
                } catch (XMPPException e) {
                    Log.e(TAG, "failed to send a message", e);
                }

                if (mode == Mode.SINGLE) {
                    showMessage(new ChatMessage(lastMsg, Calendar.getInstance().getTime(), false));
                }
            }
        });
    }

    public void showMessage(ChatMessage message) {
        saveMessageToHistory(message);
        adapter.add(message);
        adapter.notifyDataSetChanged();
        scrollDown();
    }

    public void showMessage(List<ChatMessage> messages) {
        adapter.add(messages);
        adapter.notifyDataSetChanged();
        scrollDown();
    }

    private void saveMessageToHistory(ChatMessage message) {
        if (mode == Mode.SINGLE) {
            ((App)getApplication()).addMessage(getIntent().getIntExtra(SingleChat.EXTRA_USER_ID, 0), message);
        }
    }

    private void restoreMessagesFromHistory(int userId) {
        List<ChatMessage> messages = ((App)getApplication()).getMessages(userId);
        if (messages != null) {
            showMessage(messages);
        }
    }

    private void scrollDown() {
        messagesContainer.setSelection(messagesContainer.getCount() - 1);
    }

    public static enum Mode {SINGLE, GROUP}
}

我不再收到错误但登录活动后没有打开chatActivity。 我认为示例应用程序的流程很快 - &gt;主要 - &gt;登录/注册 - &gt;聊天??