Java Code Examples for org.jivesoftware.smack.XMPPException#printStackTrace()

The following examples show how to use org.jivesoftware.smack.XMPPException#printStackTrace() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: XmppTool.java    From xmpp with Apache License 2.0 6 votes vote down vote up
/**
 * ��¼
 * 
 * @param user
 * @param password
 */
public static void login(String user, String password) {

	if (connection == null) {
		init();
	}
	try {
		/** �û���½���û��������� */
		connection.login(user, password);
	} catch (XMPPException e) {
		e.printStackTrace();
	}
	/** ��ȡ��ǰ��½�û� */
	fail("User:", connection.getUser());
	addGroup(connection.getRoster(), "�ҵĺ���");
	addGroup(connection.getRoster(), "������");
	System.out.println("OK");

}
 
Example 2
Source File: XmppManager.java    From weixin with Apache License 2.0 6 votes vote down vote up
/**
 * 加入会议室
 * 
 * @param user 昵称
 * @param password 会议室密码
 * @param roomsName 会议室名
 */
public MultiUserChat joinMultiUserChat(String user, String password, String roomsName) {
	try {
		// 使用XMPPConnection创建一个MultiUserChat窗口  
		MultiUserChat muc = new MultiUserChat(getConnection(), roomsName + "@conference." + getConnection().getServiceName());
		// 聊天室服务将会决定要接受的历史记录数量  
		DiscussionHistory history = new DiscussionHistory();
		history.setMaxStanzas(0);
		//history.setSince(new Date());  
		// 用户加入聊天室  
		muc.join(user, password, history, SmackConfiguration.getPacketReplyTimeout());
		L.i(LOGTAG, "会议室加入成功........");
		return muc;
	} catch (XMPPException e) {
		e.printStackTrace();
		L.i(LOGTAG, "会议室加入失败........");
		return null;
	}
}
 
Example 3
Source File: MultiUserChatTest.java    From Smack with Apache License 2.0 6 votes vote down vote up
public void testDiscoverJoinedRooms() {
    try {
        // Check that user1 has joined only to one room
        Iterator<String> joinedRooms = MultiUserChat.getJoinedRooms(getConnection(1), getFullJID(0));
        assertTrue("Joined rooms shouldn't be empty", joinedRooms.hasNext());
        assertEquals("Joined room is incorrect", joinedRooms.next(), room);
        assertFalse("User has joined more than one room", joinedRooms.hasNext());

        // Leave the new room
        muc.leave();

        // Check that user1 is not currently join any room
        joinedRooms = MultiUserChat.getJoinedRooms(getConnection(1), getFullJID(0));
        assertFalse("Joined rooms should be empty", joinedRooms.hasNext());

        muc.join("testbot");
    }
    catch (XMPPException e) {
        e.printStackTrace();
        fail(e.getMessage());
    }
}
 
Example 4
Source File: XmppConnection.java    From weixin with Apache License 2.0 6 votes vote down vote up
/**
 * 加入会议室
 * 
 * @param user 昵称
 * @param password 会议室密码
 * @param roomsName 会议室名
 */
public MultiUserChat joinMultiUserChat(String user, String roomsName, String password) {
	if (getConnection() == null)
		return null;
	try {
		// 使用XMPPConnection创建一个MultiUserChat窗口
		MultiUserChat muc = new MultiUserChat(getConnection(), roomsName + "@conference." + getConnection().getServiceName());
		// 聊天室服务将会决定要接受的历史记录数量
		DiscussionHistory history = new DiscussionHistory();
		history.setMaxChars(0);
		// history.setSince(new Date());
		// 用户加入聊天室
		muc.join(user, password, history, SmackConfiguration.getPacketReplyTimeout());
		Log.i("MultiUserChat", "会议室【" + roomsName + "】加入成功........");
		return muc;
	} catch (XMPPException e) {
		e.printStackTrace();
		Log.i("MultiUserChat", "会议室【" + roomsName + "】加入失败........");
		return null;
	}
}
 
Example 5
Source File: Room.java    From HippyJava with MIT License 6 votes vote down vote up
public static Room createRoom(String name, MultiUserChat chat, XMPPConnection con) {
    final Room r = new Room(name, chat);
    try {
        r.info = MultiUserChat.getRoomInfo(con, (name.indexOf("@") != -1 ? name : name + "@" + CONF_URL));
    } catch (XMPPException e) {
        e.printStackTrace();
    }
    r.subject = r.info.getSubject();
    chat.addSubjectUpdatedListener(new SubjectUpdatedListener() {
        public void subjectUpdated(String newsubject, String from) {
            r.subject = newsubject;
        }
    });
    for (String user : r.getConnectedUsers()) {
        r.users.add(user);
    }
    r.startThread();
    return r;
}
 
Example 6
Source File: MultiUserChatTest.java    From Smack with Apache License 2.0 6 votes vote down vote up
public void testDiscoverRoomInfo() {
    try {
        makeRoomModerated();

        RoomInfo info = MultiUserChat.getRoomInfo(getConnection(1), room);

        assertFalse("Room is members-only", info.isMembersOnly());
        assertTrue("Room is moderated", info.isModerated());
        assertFalse("Room is Nonanonymous", info.isNonanonymous());
        assertFalse("Room is PasswordProtected", info.isPasswordProtected());
        assertFalse("Room is Persistent", info.isPersistent());
        assertEquals("Room's description is incorrect", "fruta124", info.getDescription());
        assertEquals("Room's subject is incorrect", "", info.getSubject());
        assertEquals("Number of occupants is incorrect", 1, info.getOccupantsCount());
    }
    catch (XMPPException e) {
        e.printStackTrace();
        fail(e.getMessage());
    }
}
 
Example 7
Source File: MultiUserChatCreationTest.java    From Smack with Apache License 2.0 6 votes vote down vote up
/**
 * Tests creating a new "Instant Room".
 */
public void testCreateInstantRoom() {
    MultiUserChat muc = new MultiUserChat(getConnection(0), room);

    try {
        // Create the room
        muc.create("testbot");

        // Send an empty room configuration form which indicates that we want
        // an instant room
        muc.sendConfigurationForm(new Form(Form.TYPE_SUBMIT));

        // Destroy the new room
        muc.destroy("The room has almost no activity...", null);
    }
    catch (XMPPException e) {
        e.printStackTrace();
        fail(e.getMessage());
    }
}
 
Example 8
Source File: XSCHelper.java    From PracticeCode with Apache License 2.0 6 votes vote down vote up
/**
 * 设置基础信息
 *
 * @param password 可null。非空则更改登录密码为password
 * @param nickName 可null。非空则更改昵称为nickName
 */
public void setUserBasicData(String password, String nickName) {
    try {
        if(getConnection() == null || !getConnection().isAuthenticated())
            return;

        if (password != null) {
            getConnection().getAccountManager().changePassword(password);
        }

        VCard card = getVCard();
        if(nickName != null && card != null){
            card.setNickName(nickName);
            card.save(getConnection());
        }
    } catch (XMPPException e) {
        e.printStackTrace();
    }
}
 
Example 9
Source File: MultiUserChatTest.java    From Smack with Apache License 2.0 6 votes vote down vote up
public void testDiscoverMUCService() {
    try {
        Collection<String> services = MultiUserChat.getXMPPServiceDomains(getConnection(1));
        assertFalse("No MUC service was found", services.isEmpty());

        // Discover the hosted rooms by the chat service.
        Collection<HostedRoom> rooms = MultiUserChat.getHostedRooms(getConnection(1),
               services.toArray(new String[0])[0]);
        // Check that we have discovered the room used by this test
        assertFalse("No room was found", rooms.isEmpty());
        // Check that we have discovered the room used by this test
        boolean found = false;
        for (Object room1 : rooms) {
            HostedRoom hostedRoom = (HostedRoom) room1;
            if (room.equals(hostedRoom.getJid())) {
                found = true;
                break;
            }
        }
        assertTrue("JID of room was not found", found);
    }
    catch (XMPPException e) {
        e.printStackTrace();
        fail(e.getMessage());
    }
}
 
Example 10
Source File: LastActivityManagerTest.java    From Smack with Apache License 2.0 6 votes vote down vote up
/**
 * This is a test to check if a LastActivity request for server uptime
 * is answered and correct
 */
public void testServerUptime() {
	TCPConnection conn0 = getConnection(0);

	LastActivity lastActivity = null;
	try {
		lastActivity = LastActivityManager.getLastActivity(conn0, getHost());
	} catch (XMPPException e) {
		if (e.getStanzaError().getCode() == 403) {
			//The test can not be done since the host do not allow this kind of request
			return;
		}
		e.printStackTrace();
		fail("An error occurred requesting the Last Activity");
	}

	assertNotNull("No last activity packet", lastActivity);
       assertTrue("The last activity idle time should be greater than 0 : " +
               lastActivity.getIdleTime(), lastActivity.getIdleTime() > 0);
   }
 
Example 11
Source File: Room.java    From HippyJava with MIT License 5 votes vote down vote up
/**
 * Send a message to this room. This method may only be used when an active connection to the room is present.
 * @param message
 *              The message to send.
 * @param from
 *            The name of the user who sent this message.
 * @return
 *        Whether the action was successful or not.
 */
public boolean sendMessage(String message, String from) {
    if (chat == null)
        return false;
    try {
        chat.sendMessage(message);
        return true;
    } catch (XMPPException e) {
        e.printStackTrace();
    }
    return false;
}
 
Example 12
Source File: XmppConnection.java    From weixin with Apache License 2.0 5 votes vote down vote up
/**
 * 获取用户VCard信息
 * 
 * @param connection
 * @param user
 * @return
 * @throws XMPPException
 */
public VCard getUserVCard(String user) {
	if (getConnection() == null)
		return null;
	VCard vcard = new VCard();
	try {
		vcard.load(getConnection(), user);
	} catch (XMPPException e) {
		e.printStackTrace();
	}
	return vcard;
}
 
Example 13
Source File: Room.java    From HippyJava with MIT License 5 votes vote down vote up
/**
 * Set the subject for this room. This method may only be used when an active connection to the room is present.
 * @param subject
 * @return
 *        Returns whether the change was successful or not.
 */
public boolean setSubject(String subject) {
    if (chat == null)
        return false;
    try {
        chat.changeSubject(subject);
    } catch (XMPPException e) {
        e.printStackTrace();
        return false;
    }
    return true;
}
 
Example 14
Source File: XmppManager.java    From weixin with Apache License 2.0 5 votes vote down vote up
/**
 * 修改密码
 * 
 * @param password
 * @return
 */
public boolean changePassword(String password) {
	try {
		getConnection().getAccountManager().changePassword(password);
		return true;
	} catch (XMPPException e) {
		e.printStackTrace();
	}
	return false;
}
 
Example 15
Source File: XmppManager.java    From weixin with Apache License 2.0 5 votes vote down vote up
/**
 * 删除当前账户
 * 
 * @return
 */
public boolean deleteAccount() {
	try {
		getConnection().getAccountManager().deleteAccount();
		return true;
	} catch (XMPPException e) {
		e.printStackTrace();
	}
	return false;
}
 
Example 16
Source File: XmppManager.java    From weixin with Apache License 2.0 5 votes vote down vote up
/**
 * 添加好友--无分组
 * 
 * @param roster
 * @param userName
 * @param name
 * @return
 */
public boolean addUser(Roster roster, String userName, String name) {
	try {
		roster.createEntry(userName, name, null);
		return true;
	} catch (XMPPException e) {
		e.printStackTrace();
	}
	return false;
}
 
Example 17
Source File: XmppManager.java    From weixin with Apache License 2.0 5 votes vote down vote up
/**
 * 添加好友--有分组
 * 
 * @param roster
 * @param userName
 * @param name
 * @param groupName
 * @return
 */
public boolean addUser(Roster roster, String userName, String name, String groupName) {
	try {
		roster.createEntry(userName, name, new String[] { groupName });
		return true;
	} catch (XMPPException e) {
		e.printStackTrace();
	}
	return false;
}
 
Example 18
Source File: XmppTool.java    From xmpp with Apache License 2.0 5 votes vote down vote up
public static boolean create(User user) {
	if (connection == null) {
		init();
	}

	// String strs = user.getUser() + ";" + user.getNickname() + ";"
	// + user.getIcon() + ";" + user.getSex();
	String pswd = user.getPassword();
	user.setPassword(null);
	String strs = new Gson().toJson(user);
	System.out.println(strs);
	AccountManager accountManager = connection.getAccountManager();
	try {
		/**
		 * ����һ���û�boy������Ϊboy��������ڹ���Ա����̨ҳ��http://192.168.8.32:9090/user-
		 * summary.jsp�鿴�û�/��������Ϣ�����鿴�Ƿ�ɹ������û�
		 */
		Map<String, String> map = new HashMap<String, String>();
		map.put("name", strs);
		map.put("email", "[email protected]");
		accountManager.createAccount(user.getUser(), pswd, map);
		System.out.println(user.getUser() + "\t" + pswd + "��xmppע��ɹ�");
		return true;
		/** �޸����� */
		// accountManager.changePassword("abc");
	} catch (XMPPException e) {

		e.printStackTrace();
		return true;
	}
}
 
Example 19
Source File: XmppManager.java    From weixin with Apache License 2.0 4 votes vote down vote up
/**
 * 创建房间
 * 
 * @param roomName 房间名称
 */
public void createRoom(String roomName) {
	if (!isConnected()) {
		return;
	}
	try {
		// 创建一个MultiUserChat  
		MultiUserChat muc = new MultiUserChat(getConnection(), roomName + "@conference." + getConnection().getServiceName());
		// 创建聊天室  
		muc.create(roomName); // roomName房间的名字  
		// 获得聊天室的配置表单  
		Form form = muc.getConfigurationForm();
		// 根据原始表单创建一个要提交的新表单。  
		Form submitForm = form.createAnswerForm();
		// 向要提交的表单添加默认答复  
		for (Iterator<FormField> fields = form.getFields(); fields.hasNext();) {
			FormField field = (FormField) fields.next();
			if (!FormField.TYPE_HIDDEN.equals(field.getType()) && field.getVariable() != null) {
				// 设置默认值作为答复  
				submitForm.setDefaultAnswer(field.getVariable());
			}
		}
		// 设置聊天室的新拥有者  
		List<String> owners = new ArrayList<String>();
		owners.add(getConnection().getUser());// 用户JID  
		submitForm.setAnswer("muc#roomconfig_roomowners", owners);
		// 设置聊天室是持久聊天室,即将要被保存下来  
		submitForm.setAnswer("muc#roomconfig_persistentroom", false);
		// 房间仅对成员开放  
		submitForm.setAnswer("muc#roomconfig_membersonly", false);
		// 允许占有者邀请其他人  
		submitForm.setAnswer("muc#roomconfig_allowinvites", true);
		// 进入是否需要密码  
		//submitForm.setAnswer("muc#roomconfig_passwordprotectedroom", true);  
		// 设置进入密码  
		//submitForm.setAnswer("muc#roomconfig_roomsecret", "password");  
		// 能够发现占有者真实 JID 的角色  
		// submitForm.setAnswer("muc#roomconfig_whois", "anyone");  
		// 登录房间对话  
		submitForm.setAnswer("muc#roomconfig_enablelogging", true);
		// 仅允许注册的昵称登录  
		submitForm.setAnswer("x-muc#roomconfig_reservednick", true);
		// 允许使用者修改昵称  
		submitForm.setAnswer("x-muc#roomconfig_canchangenick", false);
		// 允许用户注册房间  
		submitForm.setAnswer("x-muc#roomconfig_registration", false);
		// 发送已完成的表单(有默认值)到服务器来配置聊天室  
		submitForm.setAnswer("muc#roomconfig_passwordprotectedroom", true);
		// 发送已完成的表单(有默认值)到服务器来配置聊天室  
		muc.sendConfigurationForm(submitForm);
	} catch (XMPPException e) {
		e.printStackTrace();
	}
}
 
Example 20
Source File: MessengerService.java    From KlyphMessenger with MIT License 4 votes vote down vote up
private void sendMessageTo(Bundle bundle)
{
	if (!connection.isConnected())
	{
		addPendingAction(ACTION_SEND_MSG, bundle);
	}
	else
	{
		String to = bundle.getString("to");
		String message = bundle.getString("message");

		to = "-" + to + "@chat.facebook.com";
		Log.d(TAG, "sendMessage to " + to + " " + message);
		Chat toChat = null;

		for (Chat chat : chats)
		{
			if (chat.getParticipant().equals(to))
			{
				toChat = chat;
			}
		}

		if (toChat == null)
		{
			toChat = createChat(to);
		}
		
		org.jivesoftware.smack.packet.Message msg = new org.jivesoftware.smack.packet.Message();
		msg.setBody(message);
		msg.setTo(to);
		msg.setType(org.jivesoftware.smack.packet.Message.Type.chat);
		msg.setThread(toChat.getThreadID());
		
		// Add to the message all the notifications requests (offline, delivered, displayed, composing)
	    MessageEventManager.addNotificationsRequests(msg, true, true, true, true);
	    DeliveryReceiptManager.addDeliveryReceiptRequest(msg);
	    
		try
		{
			toChat.sendMessage(msg);
		}
		catch (XMPPException e)
		{
			e.printStackTrace();
		}
	}
}