[英]Android social signin Quickblox chat
Here is my code. 这是我的代码。 I have read the quickblox docs and downloaded the samples.
我已阅读quickblox文档并下载了示例。 I don't understand why this is not working!!
我不明白为什么这不起作用!
I have the access token and I pass it to quickblox to sign in which it does (i can see it in the dashboard) but it does not sign me into chat instead a I received a big error saying that chat wasn't signed into. 我有访问令牌,并将其传递给quickblox进行登录(我可以在仪表板中看到它),但是它没有使我登录聊天,而是收到一个大错误,说聊天未登录。
Please please help. 请帮忙。 The code examples on the site are fine but I'm sure I am just placing something in the wrong place or have left something out.
该站点上的代码示例很好,但是我敢肯定我只是将某些东西放在错误的地方或遗漏了一些东西。 I am at the end of my tether!!
我在束缚的尽头!!
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;
}
} }
Which version of Android SDK do you use? 您使用哪个版本的Android SDK? Please download latest version here http://quickblox.com/developers/Android#Framework_changelog :
请在这里http://quickblox.com/developers/Android#Framework_changelog下载最新版本:
Did you check this Android Chat guide? 您是否查看过此Android聊天指南? http://quickblox.com/developers/Android_XMPP_Chat_Sample
http://quickblox.com/developers/Android_XMPP_Chat_Sample
To login to chat you have to use this method: 要登录聊天,您必须使用以下方法:
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");
}
What do you see in logs? 您在日志中看到了什么?
Ok this is my LoginActivity. 好的,这是我的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;
}
}
And this is my chatActivity 这是我的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}
}
I'm no longer receiving an error however after login activity in does not open chatActivity. 我不再收到错误,但是登录活动后无法打开chatActivity。 I thought the flow of the sample app went splash --> main --> login/registration --> Chat??
我以为样例应用程序的流程会飞溅->主->登录/注册->聊天?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.