smack 是开发openfire客户端的类库,从smack4.1 开始支持android开发,之前的版本是不支持的android的 ,
在这之前都是使用asmack 类库,但是现在asmack 已经过时了。
至于openfire的搭建这里就不介绍了,可以看看下面这篇内容
http://blog.csdn.net/hu285279904/article/details/45103163
写的很乱,大家就凑合着看吧
我使用的安卓开发环境是 Android Studio 1.3 其实这都无所谓,
你使用eclipse adt 也都一样
首先 从openfire 官网下载smack库 我这里是使用smack 4.1.3版本
下载地址:http://www.igniterealtime.org/downloads/index.jsp
压缩包里有很多的类库 ,但是我们只需其中的一些类库, 不是所有的,多说一句压缩包里有api文档
我们这里只需要如下的类库:
1.jxmpp-core-0.4.1.jar 2.jxmpp-util-cache-0.4.1.jar 3.smack-android-4.1.3.jar 4.smack-android-extensions-4.1.3.jar 5.smack-core-4.1.3.jar 6.smack-extensions-4.1.3.jar 7.smack-im-4.1.3.jar 8.smack-sasl-provided-4.1.3.jar 9.smack-tcp-4.1.3.jar
接下来看 核心代码了
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 服务器地址 private static String host = "192.168.0.71"; //xmpp 服务器端口 private static int prot = 5222; //xmpp 服务器名称 可能是计算机名字 private static String serviceName = "win-1pjai6ne4bf"; public static boolean login(String username, String password) { XMPPTCPConnectionConfiguration.Builder configBuilder = XMPPTCPConnectionConfiguration.builder(); //服务器IP地址 configBuilder.setHost(host); //服务器端口 configBuilder.setPort(prot); //服务器名称 configBuilder.setServiceName(serviceName); //是否开启安全模式 configBuilder.setSecurityMode(XMPPTCPConnectionConfiguration.SecurityMode.disabled); //是否开启压缩 //configBuilder.setCompressionEnabled(false); //开启调试模式 configBuilder.setDebuggerEnabled(true); //用户名密码 configBuilder.setUsernameAndPassword(username, password); XMPPTCPConnection connection = new XMPPTCPConnection(configBuilder.build()); try { connection.connect(); connection.login(); return true; } catch (Exception e) { e.printStackTrace(); return false; } } }
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 action 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 will // 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); } /* 初始化UI */ private void init() { final EditText usernameEdt = (EditText) findViewById(R.id.username); 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, "用户名或密码不能为空", Toast.LENGTH_SHORT).show(); return; } new LoginAsyncTask().execute(username, password); } }); } /* 登录的异步类 */ 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.makeText(MainActivity.this, "用户名或密码错误", Toast.LENGTH_SHORT).show(); } else { Toast.makeText(MainActivity.this, "登录成功", Toast.LENGTH_SHORT).show(); } } } }