Preface
Smack is a class library for developing OpenFire clients, supported by Android from smack4.1, the previous version is unsupported Android,
Before that, the Asmack class library was used, but now Asmack is obsolete.
As for the construction of OpenFire here is not introduced, you can look at the following this article
http://blog.csdn.net/hu285279904/article/details/45103163
It's a messy story, so let's see it.
The Android development environment that I use is the one that doesn't really matter.
You're using Eclipse ADT, too.
Body
First download the Smack Library from OpenFire website I am using smack 4.1.3 version
: http://www.igniterealtime.org/downloads/index.jsp
There are a lot of class libraries in the package, but we just need some of them, not all of them, say a few words about the API document in the package.
We only need the following class libraries here:
1. Jxmpp-core-0.4.1.jar2.jxmpp-util-cache-0.4.1.jar3.smack-android-4.1.3.jar4.smack-android-extensions-4.1.3.jar5.smack-core -4.1.3.jar6.smack-extensions-4.1.3.jar7.smack-im-4.1.3.jar8.smack-sasl-provided-4.1.3.jar9.smack-tcp-4.1.3.jar
class libraries are listed, just these class libraries can be
Next, look at the core code.
Package com.example.administrator.im;/** * Created by administrator on 2015/8/7. */import Org.jivesoftware.smack.tcp.xmpptcpconnection;import Org.jivesoftware.smack.tcp.xmpptcpconnectionconfiguration;public class Loginxmpp {//XMPP server address private static Strin G host = "192.168.0.71"; XMPP server port private static int prot = 5222; The XMPP server name may be the computer name private static String ServiceName = "WIN-1PJAI6NE4BF"; public static Boolean login (string username, string password) {Xmpptcpconnectionconfiguration.builder Configbuilde R = Xmpptcpconnectionconfiguration.builder (); Server IP address configbuilder.sethost (host); Server Port Configbuilder.setport (prot); Server name Configbuilder.setservicename (serviceName); Whether to turn on Safe Mode Configbuilder.setsecuritymode (XMPPTCPConnectionConfiguration.SecurityMode.disabled); Whether to turn on compression//configbuilder.setcompressionenabled (false); Turn on Debug mode configbuilder.setdebuggerenabled (trUE); User name Password Configbuilder.setusernameandpassword (username, password); Xmpptcpconnection connection = new Xmpptcpconnection (Configbuilder.build ()); try {connection.connect (); Connection.login (); return true; } catch (Exception e) {e.printstacktrace (); return false; } }}
Call the core code to sign in
Package Com.example.administrator.im;import Android.support.v7.app.appcompatactivity;import Android.os.Bundle; Import Android.view.menu;import android.view.menuitem;import android.widget.edittext;import Android.widget.Button; Import Android.view.view;import Android.view.view.onclicklistener;import Android.widget.toast;import Android.os.asynctask;public class Mainactivity extends Appcompatactivity {@Override protected void onCreate (Bundle Savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_main); Init (); } @Override Public boolean Oncreateoptionsmenu (Menu menu) {//Inflate the menu, this adds items to the Actio n Bar if it is present. Getmenuinflater (). Inflate (R.menu.menu_main, menu); return true; } @Override public boolean onoptionsitemselected (MenuItem Item) {//Handle Action Bar item clicks here. The action bar would//automatically handle clicks on the Home/up Button, so long/As you specify a parent activity in Androidmanifest.xml. int id = item.getitemid (); Noinspection simplifiableifstatement if (id = = r.id.action_settings) {return true; } return super.onoptionsitemselected (item); }/* Initialize UI */private void init () {final EditText Usernameedt = (EditText) Findviewbyid (r.id.user name); Final EditText Passwordedt = (EditText) Findviewbyid (R.id.password); Final Button loginbtn = (Button) Findviewbyid (R.ID.LOGINBTN); Loginbtn.setonclicklistener (New Onclicklistener () {@Override public void OnClick (View v) { String username = Usernameedt.gettext (). toString (); String password = Passwordedt.gettext (). toString (); if (Username.equals ("") | | Password.equals (")) {Toast.maketext (" ") {mainactivity.this," user name or password cannot be empty ", Toast.length_short). Show (); Return } new Loginasynctask (). Execute (username, password); } }); }/* Login Async class */class Loginasynctask extends Asynctask<string, Integer, boolean> {@Override Protected Boolean doinbackground (String ... parm) {Boolean result = false; result = Loginxmpp.login (parm[0],parm[1]); return result; } @Override protected void OnPostExecute (Boolean result) {if (!result) {toast.ma Ketext (mainactivity.this, "User name or password error", Toast.length_short). Show (); } else {Toast.maketext (mainactivity.this, "Login succeeded", Toast.length_short). Show (); } } }}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Smack for Android Login OpenFire