我试图在QBChatService上成功登录后调用一个方法来获取聊天对话框,但是当我这样做时,它会触发onError方法,如果我单独调用它,获取对话框的方法就可以了。这是我的代码:
public class ChatService {
public static final int AUTO_PRESENCE_INTERVAL_IN_SECONDS = 30;
private static ChatService instance;
private QBChatService qbChatService;
private ArrayList<QBUser> opponentsUsers = new ArrayList<QBUser>();
private Map<List<Integer>, QBDialog> chatDialogsHashMap = new HashMap<List<Integer>, QBDialog>();
private ArrayList<QBDialog> chatDialogsArrayList = new ArrayList<QBDialog>();
private ConnectionListener chatConnectionListener = new ConnectionListener() {
@Override
public void connected(XMPPConnection connection) {
Log.w("CHAT SERVICE", "connected");
}
@Override
public void authenticated(XMPPConnection connection) {
Log.w("CHAT SERVICE", "authenticated");
}
@Override
public void connectionClosed() {
Log.w("CHAT SERVICE", "connectionClosed");
}
@Override
public void connectionClosedOnError(final Exception e) {
Log.w("CHAT SERVICE", "connectionClosedOnError: " + e.getLocalizedMessage());
}
@Override
public void reconnectingIn(final int seconds) {
if(seconds % 5 == 0) {
Log.w("CHAT SERVICE", "reconnectingIn: " + seconds);
}
}
@Override
public void reconnectionSuccessful() {
Log.w("CHAT SERVICE", "reconnectionSuccessful");
}
@Override
public void reconnectionFailed(final Exception error) {
Log.w("CHAT SERVICE", "reconnectionFailed: " + error.getLocalizedMessage());
}
};
public ChatService(){
this.qbChatService = QBChatService.getInstance();
this.qbChatService.addConnectionListener(this.chatConnectionListener);
}
public static synchronized ChatService getInstance(){
if(instance == null){
instance = new ChatService();
}
return instance;
}
public static boolean initializedIfNeeded(Context context){
if(!QBChatService.isInitialized()){
QBChatService.init(context);
QBChatService.setDebugEnabled(true);
Log.w("INITIALIZED QBCHAT", "SUCCESS");
return true;
}
return false;
}
public void loginToChat(){
QBAuth.getSession(new QBEntityCallbackImpl<QBSession>() {
@Override
public void onSuccess(QBSession qbSession, Bundle bundle) {
Log.w("GET SESSION", "SUCCESS");
QBUser currentSessionUser = new QBUser();
currentSessionUser.setId(qbSession.getUserId());
currentSessionUser.setLogin(MenuActivity.sharedPreferences.getString("USERNAME", ""));
currentSessionUser.setPassword(MenuActivity.sharedPreferences.getString("PASSWORD", ""));
ChatService.this.qbChatService.login(currentSessionUser, new QBEntityCallbackImpl() {
@Override
public void onSuccess() {
Log.w("LOGIN TO CHAT", "SUCCESS");
ChatService.this.storeChatDialogs();
try {
ChatService.this.qbChatService.startAutoSendPresence(AUTO_PRESENCE_INTERVAL_IN_SECONDS);
} catch (SmackException.NotLoggedInException e) {
e.printStackTrace();
}
}
@Override
public void onError(List list) {
Log.w("LOGIN TO CHAT", "ERROR");
}
});
}
@Override
public void onError(List<String> list) {
Log.w("GET SESSION", "ERROR");
}
});
}
public void createPrivateDialog(final Activity activity, QBDialog dialogToCreate){
this.qbChatService.getGroupChatManager().createDialog(dialogToCreate, new QBEntityCallbackImpl<QBDialog>() {
@Override
public void onSuccess(QBDialog qbDialog, Bundle bundle) {
Log.w("CREATE DIALOG", "SUCCESS");
((MenuActivity) activity).changeDialog(qbDialog);
}
@Override
public void onError(List<String> list) {
Log.w("CREATE DIALOG", "EXITO");
}
});
}
public void storeChatDialogs(){
QBChatService.getChatDialogs(QBDialogType.PRIVATE, new QBRequestGetBuilder(), new QBEntityCallbackImpl<ArrayList<QBDialog>>() {
@Override
public void onSuccess(ArrayList<QBDialog> qbDialogs, Bundle bundle) {
Log.w("STORE CHAT DIALOGS", "SUCCESS");
ArrayList<QBDialog> dialogsFromCurrentUser = new ArrayList<QBDialog>();
for (QBDialog dialog : qbDialogs) {
ArrayList<Integer> occupantsIDs = dialog.getOccupants();
for (Integer ID : occupantsIDs) {
if (ChatService.getInstance().getCurrentChatUser().getId() == ID.intValue()) {
dialogsFromCurrentUser.add(dialog);
}
}
}
ChatService.this.storeOpponentsUsersOfPrivateDialogs(dialogsFromCurrentUser);
ChatService.this.setChatDialogsHashMap(dialogsFromCurrentUser);
ChatService.this.setChatDialogsArrayList(dialogsFromCurrentUser);
}
@Override
public void onError(List<String> list) {
Log.w("STORE CHAT DIALOGS", "ERROR");
}
});
}
public QBUser getCurrentChatUser(){
return this.qbChatService.getUser();
}
public Integer getOpponentIDForPrivateDialog(QBDialog dialog){
Integer opponentID = 0;
for(Integer userID : dialog.getOccupants()){
if(!userID.equals(getCurrentChatUser().getId())){
opponentID = userID;
}
}
return opponentID;
}
public void storeOpponentsUsersOfPrivateDialogs(ArrayList<QBDialog> privateDialogs){
this.opponentsUsers.clear();
QBUsers.getUsersByIDs(this.getOpponentsIDsOfPrivateDialogs(privateDialogs), new QBPagedRequestBuilder(), new QBEntityCallbackImpl<ArrayList<QBUser>>() {
@Override
public void onSuccess(ArrayList<QBUser> qbUsers, Bundle bundle) {
Log.w("GET USERS BY IDS", "SUCCESS");
ChatService.this.opponentsUsers = qbUsers;
}
@Override
public void onError(List<String> list) {
Log.w("GET USERS BY IDS", "ERROR");
}
});
}
public ArrayList<Integer> getOpponentsIDsOfPrivateDialogs(ArrayList<QBDialog> privateDialogs){
ArrayList<Integer> opponentsIDs = new ArrayList<Integer>();
for(QBDialog dialog : privateDialogs){
ArrayList<Integer> occupantsIDs = dialog.getOccupants();
for(Integer ID : occupantsIDs){
if(this.getCurrentChatUser().getId() != ID.intValue()){
opponentsIDs.add(ID);
}
}
}
return opponentsIDs;
}
public QBDialog getDialog(List<Integer> occupantsIDs){
return this.chatDialogsHashMap.get(occupantsIDs);
}
public ArrayList<QBDialog> getChatDialogsArrayList(){
return this.chatDialogsArrayList;
}
public void setChatDialogsArrayList(List<QBDialog> dialogs){
this.chatDialogsArrayList.clear();
for(QBDialog dialog : dialogs){
this.chatDialogsArrayList.add(dialog);
}
}
public Map<List<Integer>, QBDialog> getChatDialogsHashMap(){
return this.chatDialogsHashMap;
}
public void setChatDialogsHashMap(List<QBDialog> dialogs){
this.chatDialogsHashMap.clear();
for(QBDialog dialog : dialogs){
this.chatDialogsHashMap.put(dialog.getOccupants(), dialog);
}
}
public void setAndRefreshAdapter(Context context, ListView dialogsList){
ArrayList<String> opponentsNames = new ArrayList<String>();
for(QBUser opponentUser : this.opponentsUsers){
opponentsNames.add(opponentUser.getLogin());
Log.w("SET ADAPTER", opponentUser.getLogin());
}
ArrayAdapter<String> adapter = new ArrayAdapter<String>(context, R.layout.option_player, R.id.textView_PlayersFragment_textOption, opponentsNames);
dialogsList.setAdapter(adapter);
}
}
它没有显示任何错误,但我把logcat放在这里:
07-28 17:06:59.217 28003-28003/com.example.jozumaster.myapplication W/INITIALIZED QBCHAT﹕ SUCCESS
07-28 17:06:59.630 28003-28051/com.example.jozumaster.myapplication E/NativeCrypto﹕ ssl=0x6186c9d8 cert_verify_callback x509_store_ctx=0x625de940 arg=0x0
07-28 17:06:59.630 28003-28051/com.example.jozumaster.myapplication E/NativeCrypto﹕ ssl=0x6186c9d8 cert_verify_callback calling verifyCertificateChain authMethod=DHE_RSA
07-28 17:07:00.027 28003-28003/com.example.jozumaster.myapplication W/CREATE SESSION﹕ SUCCESS
07-28 17:07:08.832 28003-28522/com.example.jozumaster.myapplication E/NativeCrypto﹕ ssl=0x61d0bdb0 cert_verify_callback x509_store_ctx=0x64cf6940 arg=0x0
07-28 17:07:08.832 28003-28522/com.example.jozumaster.myapplication E/NativeCrypto﹕ ssl=0x61d0bdb0 cert_verify_callback calling verifyCertificateChain authMethod=DHE_RSA
07-28 17:07:08.970 28003-28003/com.example.jozumaster.myapplication W/dalvikvm﹕ VFY: unable to resolve virtual method 457: Landroid/content/res/TypedArray;.getChangingConfigurations ()I
07-28 17:07:08.971 28003-28003/com.example.jozumaster.myapplication W/dalvikvm﹕ VFY: unable to resolve virtual method 479: Landroid/content/res/TypedArray;.getType (I)I
07-28 17:07:09.415 28003-28529/com.example.jozumaster.myapplication E/NativeCrypto﹕ ssl=0x615d1a88 cert_verify_callback x509_store_ctx=0x64ef2940 arg=0x0
07-28 17:07:09.415 28003-28529/com.example.jozumaster.myapplication E/NativeCrypto﹕ ssl=0x615d1a88 cert_verify_callback calling verifyCertificateChain authMethod=DHE_RSA
07-28 17:07:09.506 28003-28003/com.example.jozumaster.myapplication W/SIGN IN﹕ SUCCESS
07-28 17:07:09.852 28003-28003/com.example.jozumaster.myapplication W/GET SESSION﹕ SUCCESS
07-28 17:07:10.314 28003-28538/com.example.jozumaster.myapplication W/CHAT SERVICE﹕ connected
07-28 17:07:11.008 28003-28538/com.example.jozumaster.myapplication W/CHAT SERVICE﹕ authenticated
07-28 17:07:11.011 28003-28538/com.example.jozumaster.myapplication W/LOGIN TO CHAT﹕ SUCCESS
07-28 17:07:11.017 28003-28538/com.example.jozumaster.myapplication W/LOGIN TO CHAT﹕ ERROR
答案 0 :(得分:2)
Below is the solution, may be this is to long and time taken to implement but it will provide a modular solution surely:
You can also use this method directly to get dialog list..
1.) Create an interface(seprate java class) for method to call
public interface IChat {
void getDialogForChatHistory(IChatCallback.IChatHistoryDialog callback);
}
2.) Create an interface(seprate java class) for callback :
public interface IChatCallback {
interface IChatHistoryDialog {
void onChatHistoryDialogReceived(ArrayList<UserChatDialog> userChatDialog);
}
3.) Create another Implementation java class and implement with IChat interface and override this method:
@Override
public void getDialogForChatHistory(final IChatCallback.IChatHistoryDialog callback) {
QBRequestGetBuilder requestBuilder = new QBRequestGetBuilder();
requestBuilder.sortDesc("last_message_date_sent");
requestBuilder.setPagesLimit(1000);
QBChatService.getChatDialogs(null, requestBuilder, new QBEntityCallbackImpl<ArrayList<QBDialog>>() {
ArrayList<UserChatDialog> userChatDialogs = new ArrayList<UserChatDialog>();
@Override
public void onSuccess(final ArrayList<QBDialog> dialogs, Bundle args) {
counterForChatUserNameFetch = 0;
if (0 == dialogs.size()) {
callback.onChatHistoryDialogReceived(userChatDialogs);
return;
}
for (QBDialog dialog : dialogs) {
if (dialog.getOccupants() == null) {
Log.i("getOccupants is:", "null" + dialogs.size());
}
int idToLookUp = dialog.getOccupants().get(0).intValue() == ChatUtils.CURRENT_USER_ID_BY_SESSION ? // chatService.getUser().getId() ?
dialog.getOccupants().get(1).intValue() : dialog.getOccupants().get(0).intValue();
final UserChatDialog dlg = new UserChatDialog();
dlg.setDialogId(dialog.getDialogId());
dlg.setLastMessage(dialog.getLastMessage());
dlg.setLastMessageUserId(dialog.getLastMessageUserId());
dlg.setLastMessageDateSent(dialog.getLastMessageDateSent() * 1000L);// Multiplied by 1000L because the time given by Quickblox server is epoch time
dlg.setUserId(idToLookUp);
dlg.setPhoto(dialog.getPhoto());
dlg.setRoomJid(dialog.getRoomJid());
dlg.setUnreadMessageCount(dialog.getUnreadMessageCount());
dlg.setType(dialog.getType().getCode());
dlg.setData(dialog.getData());
dlg.setOccupantsIds(dialog.getOccupants());
QBUsers.getUser(idToLookUp, new QBEntityCallback<QBUser>() {
@Override
public void onSuccess(QBUser qbUser, Bundle bundle) {
if (qbUser.getLogin() == null || qbUser.getLogin().equalsIgnoreCase("null")) {
Log.i("Login is:", "null" + dialogs.size());
}
dlg.setChatFriendNameByOccupantId(qbUser.getLogin());
userChatDialogs.add(dlg);
counterForChatUserNameFetch++;
if (counterForChatUserNameFetch == dialogs.size()) {
callback.onChatHistoryDialogReceived(userChatDialogs);
}
}
@Override
public void onSuccess() {
}
@Override
public void onError(List<String> list) {
counterForChatUserNameFetch++;
if (counterForChatUserNameFetch == dialogs.size()) {
callback.onChatHistoryDialogReceived(userChatDialogs);
}
}
});
}
}
@Override
public void onError(List<String> errors) {
callback.onChatHistoryDialogReceived(userChatDialogs);
}
});
}
4.) Create a class:
public abstract class AbstractChat {
public abstract IChat getChat(String choice);
}
5.) Create a factory class:
public class FactoryClass {
public static AbstractChat getFactory(String choice) {
return new ChatImpl();
}
}
6.) Create ChatImpl class:
public class ChatImpl extends AbstractChat {
@Override
public IChat getChat(String choice) {
if(choice.equalsIgnoreCase(ChatUtils.QUICKBLOX)){
return QuickbloxImpl.getInstance();
}
return null;
}
}
7.)CODE to get dialog list :
private IChat chat;
AbstractChat abstractChat = FactoryClass.getFactory("CHAT");
chat = abstractChat.getChat(ChatUtils.QUICKBLOX);
chat.getDialogForChatHistory(MyChatList.this);
'OR'
You can direct use this overridden method.. Hope it will help you..
答案 1 :(得分:2)
我找到了解决方案,所以我发布这里是为了帮助其他与我有同样问题的人。
来自Quickblox的回调适用于辅助线程,因此如果要调用创建另一个辅助线程的方法,则需要从 UI线程执行此操作,因此您需要创建一个Handler对象连接UI线程,Quickblox回调使这个自动化。问题是辅助线程默认情况下没有消息队列(处理程序队列)(UI线程确实),您必须使用Looper类创建它。 Looper | Android Deve
以下代码正在运行:
public void loginToChat(final QBUser currentSessionUser){
QBChatService.getInstance().login(currentSessionUser, new QBEntityCallbackImpl() {
@Override
public void onSuccess() {
Looper.prepare();
Log.w("LOGIN TO CHAT", "SUCCESS");
try {
QBChatService.getInstance().startAutoSendPresence(ChatService.AUTO_PRESENCE_INTERVAL_IN_SECONDS);
} catch (SmackException.NotLoggedInException e) {
e.printStackTrace();
}
ChatService.getInstance().storeChatDialogs();
Looper.loop();
}
@Override
public void onError(List errors) {
Log.w("LOGIN TO CHAT", "ERROR" + errors);
}
});
}
答案 2 :(得分:0)
如果您收到登录聊天:成功然后登录聊天:错误 那么这意味着在聊天登录 onSuccess 回调中发生了一些异常,所以你应该先检查一下