究竟如何在 android 中使用 quickblox?

Posted

技术标签:

【中文标题】究竟如何在 android 中使用 quickblox?【英文标题】:How exactly use quickblox in android? 【发布时间】:2017-03-08 07:35:22 【问题描述】:

我参考官网https://quickblox.com/developers/android#Download_Android_SDK

gradle 编译成功:

    repositories 
            maven 
                url "https://github.com/QuickBlox/quickblox-android-sdk-releases/raw/master/"
            
        
            dependencies 
                 compile "com.quickblox:quickblox-android-sdk-core:2.5.1@aar"   
                 compile("com.quickblox:quickblox-android-sdk-chat:2.5.1@aar") 
                 transitive=true
            
        

然后我先使用代码: 我有 APP_ID...等

    QBSettings.getInstance().init(getApplicationContext(), APP_ID, AUTH_KEY, AUTH_SECRET);

    QBSettings.getInstance().setAccountKey(ACCOUNT_KEY);

第二步: 我参考指南:Chat API 入门https://quickblox.com/developers/Android_XMPP_Chat_Sample#Guide:_Getting_Started_with_Chat_API

//Prepare chat service
QBChatService.setDebugEnabled(true); // enable chat logging

        QBChatService.setDefaultPacketReplyTimeout(10000);//set reply timeout in milliseconds for connection's packet.
        //Can be used for events like login, join to dialog to increase waiting response time from server if network is slow.


        //configure chat socket
        QBChatService.ConfigurationBuilder chatServiceConfigurationBuilder = new QBChatService.ConfigurationBuilder();
        chatServiceConfigurationBuilder.setSocketTimeout(60); //Sets chat socket's read timeout in seconds
        chatServiceConfigurationBuilder.setKeepAlive(true); //Sets connection socket's keepAlive option.
        chatServiceConfigurationBuilder.setUseTls(true); //Sets the TLS security mode used when making the connection. By default TLS is disabled.

QBChatService.setConfigurationBuilder(chatServiceConfigurationBuilder);

有一个问题,我无法导入QBChatService.ConfigurationBuilder 所以我尝试将gradle更改为compile("com.quickblox:quickblox-android-sdk-chat:2.6.1")

现在可以导入 QBChatService.ConfigurationBuilder

第三步: 我采取官方步骤使用代码:

// Initialise Chat service
        final QBChatService chatService = QBChatService.getInstance();

        final QBUser user = new QBUser("garrysantos", "garrysantospass");

        QBAuth.createSession(user, new QBEntityCallback<QBSession>() 
            @Override
            public void onSuccess(QBSession qbSession, Bundle bundle) 

                // success, login to chat

                user.setId(qbSession.getUserId());

                chatService.login(user, new QBEntityCallback() 
                    @Override
                    public void onSuccess(Object o, Bundle bundle) 

                    

                    @Override
                    public void onError(QBResponseException e) 

                    
                );


            

            @Override
            public void onError(QBResponseException e) 

            
        );


        //To handle different connection states use ConnectionListener:
        ConnectionListener connectionListener = new ConnectionListener() 
            @Override
            public void connected(XMPPConnection xmppConnection) 

            

            @Override
            public void authenticated(XMPPConnection xmppConnection, boolean b) 

            

            @Override
            public void connectionClosed() 

            

            @Override
            public void connectionClosedOnError(Exception e) 
                // connection closed on error. It will be established soon
            

            @Override
            public void reconnectionSuccessful() 

            

            @Override
            public void reconnectingIn(int i) 

            

            @Override
            public void reconnectionFailed(Exception e) 

            
        ;

        QBChatService.getInstance().addConnectionListener(connectionListener);


        //logOut
        boolean isLoggedIn = chatService.isLoggedIn();
        if (!isLoggedIn) 
            return;
        

        chatService.logout(new QBEntityCallback<Void>() 
            @Override
            public void onSuccess(Void aVoid, Bundle bundle) 
                //success
                chatService.destroy();
            

            @Override
            public void onError(QBResponseException e) 

            
        );

        //By default Android SDK reconnects automatically when connection to server is lost.
        //But there is a way to disable this and then manage this manually:
        QBChatService.getInstance().setReconnectionAllowed(false);

当我使用关于QBChatDialog的步骤时,它不能再次导入......

ArrayList<Integer> occupantIdsList = new ArrayList<Integer>();
        occupantIdsList.add(34);
        occupantIdsList.add(17);

        QBChatDialog dialog = new QBChatDialog();
        dialog.setName("Chat with Garry and John");
        dialog.setPhoto("1786");
        dialog.setType(QBDialogType.GROUP);
        dialog.setOccupantsIds(occupantIdsList);

//or just use DialogUtils
//for creating PRIVATE dialog
//QBChatDialog dialog = DialogUtils.buildPrivateDialog(recipientId);

//for creating GROUP dialog
        QBChatDialog dialog = DialogUtils.buildDialog("Chat with Garry and John", QBDialogType.GROUP, occupantIdsList);

        QBRestChatService.createChatDialog(dialog).performAsync(new QBEntityCallback<QBChatDialog>() 
            @Override
            public void onSuccess(QBChatDialog result, Bundle params) 

            

            @Override
            public void onError(QBResponseException responseException) 

            
        );

所以我尝试更改 gradle compile compile("com.quickblox:quickblox-android-sdk-chat:3.3.0")

现在可以导入QBChatDialog

但它还有另一个问题......

Can't not resolve symbol 'QBSettings' and 'QBSession' 

我现在很生气,你在开玩笑吗?

为什么官方一步一步骗我?

我累了……我该怎么办?

有人可以救我,任何帮助将不胜感激!

根据@Jagapathi 的回复,我更新了我的代码,下一个问题是我无法登录

当我点击登录按钮时,我的 toast 显示登录错误:

private void setupQuickBlox() 
        QBSettings.getInstance().init(getApplicationContext(), APP_ID, AUTH_KEY, AUTH_SECRET);
        QBSettings.getInstance().setAccountKey(ACCOUNT_KEY);
        QBSettings.getInstance().setAutoCreateSession(true);

        //login to quickblox
        String enterAccount = editAccount.getText().toString();
        String enterPassword = editPassword.getText().toString();
        Log.d(TAG,enterAccount);
        Log.d(TAG,enterPassword);
        final QBUser user = new QBUser(enterAccount, enterPassword);
        //login
        QBUsers.signIn(user).performAsync(new QBEntityCallback<QBUser>() 
            @Override
            public void onSuccess(QBUser qbUser, Bundle bundle) 
                SharedPreferences.Editor s = getSharedPreferences("QBid", 0).edit();
                s.putString("id", user.getId().toString());
                s.apply();
                Log.d(TAG,user.getId().toString());
                Toast.makeText(MainActivity.this, "Login success with quickblox", Toast.LENGTH_SHORT).show();
            

            @Override
            public void onError(QBResponseException e) 
                Toast.makeText(MainActivity.this, "Login error", Toast.LENGTH_SHORT).show();
            
        );
    

代码在我的onCreat下,所以当我打开应用程序时它显示登录错误,但是当我输入帐户和密码时,它仍然显示登录错误,为什么?我查看日志,我可以看到我输入的帐户和密码,但我在我的日志中看不到user.getId().toString(),是哪一步出错了? 我检查帐户是否正确:

这是我的钥匙:

static final String APP_ID = "50427";
    static final String AUTH_KEY = "naMGFKMshdLC3s4";
    static final String AUTH_SECRET = "GP8ey4GsQXt2TGu";
    static final String ACCOUNT_KEY = "dHYgix3we3bxxsvMqyuR";

这是我的测试帐户密钥:

我在Clcik上的按钮:

buttonLogin.setOnClickListener(new View.OnClickListener() 
            @Override
            public void onClick(View view) 
                setupQuickBlox();
            
        );

这是我的日志:

【问题讨论】:

【参考方案1】:

我可以指导您使用 Quickblox 我开始使用 quickblox 时处于相同的位置。

第 1 步:-

compile 'com.quickblox:quickblox-android-sdk-core:3.3.0@aar'
compile("com.quickblox:quickblox-android-sdk-chat:3.3.0@aar") 
    transitive = true

这是最新版本的 quickblox。所以不要使用旧版本。

第 2 步:-

这是我的设置 Quickblox 功能,您不要忘记使用 app_id Auth_key auth_secret 和 account_key

 private void SetupQuickBlox() 

    QBSettings.getInstance().init(getApplicationContext(), APP_ID, AUTH_KEY, AUTH_SECRET);
    QBSettings.getInstance().setAccountKey(ACCOUNT_KEY);
    QBSettings.getInstance().setAutoCreateSession(true);

    //login to quickblog for


    final QBUser user=new QBUser("USER_NAME OF USER","PASSWORD OF USER");
    // Login
    QBUsers.signIn(user).performAsync(new QBEntityCallback<QBUser>() 
        @Override
        public void onSuccess(QBUser user, Bundle args) 
            // success
            SharedPreferences.Editor s=getSharedPreferences("QBid",0).edit();
            s.putString("id",user.getId().toString());
            s.apply();
            Toast.makeText(HomeActivity.this, "Login succes with quickblox", Toast.LENGTH_SHORT).show();
        

        @Override
        public void onError(QBResponseException error) 
            // error
        
    );

步骤:- 3

您已完成使用 quickblox 登录成功,您现在可以请求 DIALOGS 或 CREATE DIALOg 会话会在最新版本中自动创建。

创建新对话框

private void NewMessage() 
    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() 
        @Override
        public void onClick(View view) 
            QBChatDialog dialog = DialogUtils.buildPrivateDialog("USER_ID of other user");
            dialog.setName("tester1");

            QBRestChatService.createChatDialog(dialog).performAsync(new QBEntityCallback<QBChatDialog>() 
                @Override
                public void onSuccess(QBChatDialog result, Bundle params) 

                

                @Override
                public void onError(QBResponseException responseException) 

                
            );
        
    );

请求登录用户对话框列表

我使用 ListView 和 Dialogs 结果将在包含 QBCHATDIALOG 列表的数组列表中

private void receiveChatList() 
    QBRequestGetBuilder requestBuilder = new QBRequestGetBuilder();
    requestBuilder.setLimit(100);

    QBRestChatService.getChatDialogs(null, requestBuilder).performAsync(
            new QBEntityCallback<ArrayList<QBChatDialog>>() 
                @Override
                public void onSuccess(final ArrayList<QBChatDialog> result, Bundle params) 
                    int totalEntries = params.getInt("total_entries");
                    Log.wtf("chat",""+result);
                    TrumeMsgAdapter adapter=new TrumeMsgAdapter(TrueMeMessagesActivity.this,result);
                    chatlistView.setAdapter(adapter);
                    chatlistView.setOnItemClickListener(new AdapterView.OnItemClickListener() 
                        @Override
                        public void onItemClick(AdapterView<?> parent, View view, int position, long id) 
                            startActivity(new Intent(TrueMeMessagesActivity.this,ChatingActivity.class).putExtra("dialog",result.get(position)));
                        
                    );

                
                @Override
                public void onError(QBResponseException responseException) 

                
            );

我的适配器代码

public class TrumeMsgAdapter extends BaseAdapter 

private ArrayList<QBChatDialog> chatlist;
private Context context;

public TrumeMsgAdapter(Context c,ArrayList<QBChatDialog> chatlist)
    this.chatlist=chatlist;
    this.context=c;

@Override
public int getCount() 
    return chatlist.size();


@Override
public Object getItem(int position) 
    return null;


@Override
public long getItemId(int position) 
    return 0;


@Override
public View getView(int position, View convertView, ViewGroup parent) 
    View List;
    LayoutInflater inflater = (LayoutInflater) context
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    if (convertView == null) 
        List = inflater.inflate(R.layout.trume_msg_adapter, null);
        TextView username=(TextView) List.findViewById(R.id.UserName);
        TextView lastmessage=(TextView)List.findViewById(R.id.lastmessage);
        username.setText(chatlist.get(position).getName());
        lastmessage.setText(chatlist.get(position).getLastMessage());
     else 
        List = convertView;
        TextView username=(TextView) List.findViewById(R.id.UserName);
        TextView lastmessage=(TextView)List.findViewById(R.id.lastmessage);
        username.setText(chatlist.get(position).getName());
        lastmessage.setText(chatlist.get(position).getLastMessage());
    

    return List;


【讨论】:

非常感谢!最近几天没有回复,我不再尊重你的帮助,尽管我会一步一步地尝试你的问题来解决我们的问题,如果我有任何问题,我会再次寻求你的帮助。谢谢@Jagapathi! 感谢您的支持,我添加了获取聊天对话框的代码对于获取聊天消息有任何问题,我可以帮助您度过美好的一天 嗨@Jagapathi,我应该按照你的指南忘记我的步骤吗?我无法登录,我更新我的问题。 你所有的钥匙都完美了吗?我的登录代码完美运行,因为我两天前正在使用 quickblox 聊天 您使用手动登录,然后使用两个不同的功能分开初始化和登录

以上是关于究竟如何在 android 中使用 quickblox?的主要内容,如果未能解决你的问题,请参考以下文章

浅谈Android音视频开发那点事儿,音视频究竟如何入门?

浅谈Android音视频开发那点事儿,音视频究竟如何入门?

如何使用 Android 片段?

如何从图像 Android 应用程序中提取文本

android:onClick XML 属性与 setOnClickListener 究竟有何不同?

Android开发Git版本号控制,究竟哪些文件不要提交