Login Openfire use smack 4.1.x on Android

Hi all,

I’m new on openfire and smack, my project has to use smack on android.I know the smack 4.1+ version is native support android.

so I followed the github https://github.com/igniterealtime/Smack/wiki/Smack-4.1-Readme-and-Upgrade-Guide

I got an error :org.jivesoftware.smack.sasl.SASLErrorException: SASLError using PLAIN: not-authorized , I don’t know how to login with SASL.

Is anybody successed login with SASL(Smack 4.1+) on Android ? give me some sample code please.

My develop environment : JDK 8 x64、Android Studio 1.1、Smack 4.1.0-rc3-SNAPSHOT、Openfire 3.9.3

I use my phone to debug, pc is server installed openfire, pc ip is 192.168.0.100.phone use wifi.

I add a property in openfire:

My Code

gradle :

buildscript {

repositories {

jcenter()

maven {

        url '[https://oss.sonatype.org/content/repositories/snapshots](https://oss.sonatype.org/content/repositories/snapshots)'

}

}

dependencies {

classpath ‘com.android.tools.build:gradle:1.1.0’

// NOTE: Do not place your application dependencies here; they belong

// in the individual module build.gradle files

}

}

allprojects {

repositories {

jcenter()

maven {

        url '[https://oss.sonatype.org/content/repositories/snapshots](https://oss.sonatype.org/content/repositories/snapshots)'

}

}

}

apply plugin: ‘com.android.application’

android {

compileSdkVersion 14

buildToolsVersion “21.1.2”

defaultConfig {

applicationId “cn.do7hr.ui”

minSdkVersion 14

targetSdkVersion 14

versionCode 1

versionName “1.0”

}

buildTypes {

release {

minifyEnabled false

proguardFiles getDefaultProguardFile(‘proguard-android.txt’), ‘proguard-rules.pro’

}

}

compileOptions {

sourceCompatibility JavaVersion.VERSION_1_7

targetCompatibility JavaVersion.VERSION_1_7

}

packagingOptions {

exclude ‘META-INF/LICENSE’

exclude ‘META-INF/NOTICE’

}

}

dependencies {

compile fileTree(include: [’*.jar’], dir: ‘libs’)

compile ‘com.google.zxing:core:3.1.0’

compile ‘com.google.zxing:android-core:3.1.0’

compile ‘com.google.zxing:android-integration:3.1.0’

compile ‘com.fasterxml.jackson.core:jackson-core:2.5.1’

compile ‘com.fasterxml.jackson.core:jackson-databind:2.5.1’

compile ‘com.fasterxml.jackson.core:jackson-annotations:2.5.1’

compile ‘org.apache.commons:commons-lang3:3.3.2’

compile ‘com.nostra13.universalimageloader:universal-image-loader:1.9.3’

compile ‘com.github.nicolasjafelle:paginglistview:1.2’

compile ‘com.github.nicolasjafelle:paginggridview:1.0’

compile “org.igniterealtime.smack:smack-android-extensions:4.1.0-rc3-SNAPSHOT”

compile “org.igniterealtime.smack:smack-tcp:4.1.0-rc3-SNAPSHOT”

compile ‘org.igniterealtime.smack:smack-im:4.1.0-rc3-SNAPSHOT’

}

Activity:

private String ChatServer = “192.168.0.100”;

@Override

public void onClick(View v) {

new Thread() {

public void run() {

try {

XMPPTCPConnectionConfiguration.Builder builder = XMPPTCPConnectionConfiguration.builder();

builder.setSecurityMode(ConnectionConfiguration.SecurityMode.disabled);

builder.setUsernameAndPassword(“user1@” + ChatServer, “000”);

builder.setServiceName(ChatServer);

builder.setHost(ChatServer);

builder.setDebuggerEnabled(true);

//this is also error

//SASLPlainMechanism saslPlainMechanism = new SASLPlainMechanism();

//SASLAuthentication.registerSASLMechanism(saslPlainMechanism);

AbstractXMPPConnection connection = new XMPPTCPConnection(builder.build());

//saslPlainMechanism.instanceForAuthentication(connection);

connection.connect();

connection.login();

ChatManager chatmanager = ChatManager.getInstanceFor(connection);

//Create chat

Chat newChat = chatmanager.createChat(“user2@” + ChatServer, new ChatMessageListener() {

@Override

public void processMessage(Chat chat, Message message) {

try {

chat.sendMessage(message);

} catch (SmackException.NotConnectedException e) {

Log.d(TAG, e.toString());

}

}

});

newChat.sendMessage(“I’m User1”);

chatmanager.addChatListener(new ChatManagerListener() {

@Override

public void chatCreated(Chat chat, boolean createdLocally) {

if (!createdLocally) {

chat.addMessageListener(new ChatMessageListener() {

@Override

public void processMessage(Chat chat, Message message) {

}

});

}

}

});

} catch (SmackException | IOException | XMPPException e) {

Log.d(TAG, e.toString());

}

}

}.start();

}

I have try neally all methed and it’s no answer, thanks.

builder.setUsernameAndPassword(“user1@” + ChatServer, “000”);
Try just “user1” as username, i.e. without the domainpart.

Thanks, but there is the same error.

is this user1 is created in your openfire server?

Yeah, I create two users : user1、user2 and tested with spark.

I have solved this problem, the Service Name should be the same as openfire settings:

builder.setServiceName(ChatServer);

In my openfire installing, I set ComputerName as Service Name, but I use IP address, so it’s the answer.Thanks for everyone help, for everyone with this problem.