您的位置:首页 > 移动开发 > Android开发

android基于openfire+spark+amack 即时聊天--------<2>登录遇到的问题和重要类解析

2015-03-21 16:33 483 查看

android基于openfire+spark+amack 即时聊天--------<2>登录遇到的问题和重要类解析

1.登录

1.1 public class ConnectionConfiguration

       extends java.lang.Object       implements java.lang.Cloneable

描述:
Configuration to use while establishing the connection to the server. It is possible to configure the path to the trustore file that keeps the trusted CA root certificates and enable or disable all or some of the checkings done while verifying server certificates.

It is also possible to configure if TLS, SASL, and compression are used or not.

意思大体是这样: 当与服务器建立连接的时候配置使用。它也能配置代理文件(能使受信任的CA根证书和启用或禁用的部分或全部的服务器检查证书的完成)

还可以配置如果TLS,SASL,和使用压缩或者不使用压缩。

1.2 使用的方法

setCompressionEnabled(boolean compressionEnabled)


是否使用流压缩,流压缩要求TLS建立后(是否启用了TLS),并且服务器提供流压缩,使用流压缩可以减少网络流量高达90%。默认情况下禁用压缩。


[code]public boolean isSASLAuthenticationEnabled();
返回true时如果客户机将使用SASL验证登录到服务器。如果SASL授权失败,那么客户端将尝试使用non-sasl身份验证。默认情况下启用SASL。
[/code]

[code]public void setSASLAuthenticationEnabled(boolean saslAuthenticationEnabled)设置是否使用SASL验证,如果验证失败客户端将会使用无验证授权,默认是可以使用

[/code]

[code]public void setReconnectionAllowed(boolean isAllowed)
设置是否允许重新连接机制,默认是允许[/code]

[code]setSecurityMode(ConnectionConfiguration.SecurityMode securityMode)
设置TLS安全模式时使用的连接。默认情况下,模式是ConnectionConfiguration.SecurityMode.enabled。
[/code]

1.3

[code]public class XMPPConnectionextends Connection

创建一个套接字连接到XMPP服务器。这是默认连接Jabber服务器并在XMPP中指定核心2.登录关键代码:
package com.example.chatdemo;

import org.jivesoftware.smack.ConnectionConfiguration;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.packet.Presence;
import org.jivesoftware.smackx.pubsub.PresenceState;

import android.content.Context;

public class ClientConServer {
	private Context context;
	private static int PORT = 5222;
	private String host = "10.2.9.1";

	public ClientConServer(Context context) {
		this.context = context;
	}

	public String login(String username, String pass) {
		ConnectionConfiguration configuration = new ConnectionConfiguration(
				host, PORT);
		// 是否允许重新连接
		configuration.setReconnectionAllowed(true);
		// 是否允许使用压缩
		configuration.setCompressionEnabled(false);
		// 设置TLS安全模式时使用的连接
		configuration
				.setSecurityMode(ConnectionConfiguration.SecurityMode.disabled);
		// 是否使用SASL
		configuration.setSASLAuthenticationEnabled(false);
		XMPPConnection xmppConnection = new XMPPConnection(configuration);
		try {
			XMPPConnection.DEBUG_ENABLED = true;
			xmppConnection.connect();
			xmppConnection.login(username, pass);
			//代表XMPP存在数据包  常量值在api中查看吧
			Presence presence = new Presence(Presence.Type.available);
			presence.setStatus("Q我吧");
			xmppConnection.sendPacket(presence);
			return "success";
		} catch (XMPPException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return e.getMessage();
		}

	}

}


3.遇到的问题

注意端口号的正确(502),是否设置授权(401) 我在写的时候就遇到了


                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐