'Android/XMPP'에 해당되는 글 5건

  1. 2010.10.28 Smack patch 1
  2. 2010.10.28 Android Smack example 5
  3. 2010.10.06 SMACK links 2
  4. 2010.10.06 XMPP opensource library SMACK #2 - Processing Incoming Packets 1
  5. 2010.10.06 XMPP opensource library SMACK #1 1
Android/XMPP2010. 10. 28. 21:11


http://blog.jayway.com/2008/11/21/give-back-my-xmpp-in-android/

SASL 관련 XMPPConnection클래스의 버그를 수정한 패치가 올려져 있다.

아래와 같은 에러 발생시 이 패치가 유용할 것이다.

10-28 19:40:09.640: ERROR/AndroidRuntime(2342): java.lang.VerifyError: org.jivesoftware.smack.sasl.SASLMechanism
10-28 19:40:09.640: ERROR/AndroidRuntime(2342):     at java.lang.Class.getDeclaredConstructors(Native Method)
10-28 19:40:09.640: ERROR/AndroidRuntime(2342):     at java.lang.Class.getConstructor(Class.java:477)
10-28 19:40:09.640: ERROR/AndroidRuntime(2342):     at org.jivesoftware.smack.SASLAuthentication.authenticate(SASLAuthentication.java:303)

10-28 19:40:09.640: ERROR/AndroidRuntime(2342):     at
org.jivesoftware.smack.XMPPConnection.login(XMPPConnection.java:395)
10-28 19:40:09.640: ERROR/AndroidRuntime(2342):     at
org.jivesoftware.smack.XMPPConnection.login(XMPPConnection.java:349)
10-28 19:40:09.640: ERROR/AndroidRuntime(2342):     at com.yamaia.mobilebridge.delivery.PushService$LoginThread.run(PushService.java:404)
10-28 19:40:09.640: ERROR/AndroidRuntime(2342):     at java.lang.Thread.run(Thread.java:1102)



Posted by 삼스
Android/XMPP2010. 10. 28. 19:47


http://davanum.wordpress.com/2007/12/31/android-just-use-smack-api-for-xmpp/


Android – Just Use Smack API For XMPP

Filed under: Uncategorized — Davanum Srinivas @ 9:36 am

OUTDATED SAMPLE – Updated code is here:


http://davanum.wordpress.com/2008/12/29/updated-xmpp-client-for-android/

Using Smack XMPP API From Android

Once you get tired of the limitations of android’s built-in IMProvider and the corresponding API – IXmppSession and IXmppService, try the sample below. Inside the source/binary zip (bottom of this article) you will find a smack.jar that works with android. To build the jar yourself, You can download the Smack 3.0.4 sources from here and apply the patch here.

Here Is A Screen Shot Of The XMPP Settings Dialog.

1

Notes



  • For GTalk, use “gtalk.google.com” as host with port 5222. The service name is “gmail.com”

  • Don’t add “@gmail.com” in the user name, just the id will do

Here’s The Code For The Settings Dialog

01 package org.apache.android.xmpp;
02  
03 import android.app.Dialog;
04 import android.util.Log;
05 import android.view.View;
06 import android.widget.Button;
07 import android.widget.EditText;
08 import org.jivesoftware.smack.ConnectionConfiguration;
09 import org.jivesoftware.smack.XMPPConnection;
10 import org.jivesoftware.smack.XMPPException;
11 import org.jivesoftware.smack.packet.Presence;
12  
13 /**
14  * Gather the xmpp settings and create an XMPPConnection
15  */
16 public class SettingsDialog extends Dialog implements android.view.View.OnClickListener {
17     private XMPPClient xmppClient;
18  
19     public SettingsDialog(XMPPClient xmppClient) {
20         super(xmppClient);
21         this.xmppClient = xmppClient;
22     }
23  
24     protected void onStart() {
25         super.onStart();
26         setContentView(R.layout.settings);
27         getWindow().setFlags(44);
28         setTitle("XMPP Settings");
29         Button ok = (Button) findViewById(R.id.ok);
30         ok.setOnClickListener(this);
31     }
32  
33     public void onClick(View v) {
34         String host = getText(R.id.host);
35         String port = getText(R.id.port);
36         String service = getText(R.id.service);
37         String username = getText(R.id.userid);
38         String password = getText(R.id.password);
39  
40         // Create a connection
41         ConnectionConfiguration connConfig =
42                 new ConnectionConfiguration(host, Integer.parseInt(port), service);
43         XMPPConnection connection = new XMPPConnection(connConfig);
44  
45         try {
46             connection.connect();
47             Log.i("XMPPClient""[SettingsDialog] Connected to " + connection.getHost());
48         catch (XMPPException ex) {
49             Log.e("XMPPClient""[SettingsDialog] Failed to connect to " + connection.getHost());
50             xmppClient.setConnection(null);
51         }
52         try {
53             connection.login(username, password);
54             Log.i("XMPPClient""Logged in as " + connection.getUser());
55  
56             // Set the status to available
57             Presence presence = new Presence(Presence.Type.available);
58             connection.sendPacket(presence);
59             xmppClient.setConnection(connection);
60         catch (XMPPException ex) {
61             Log.e("XMPPClient""[SettingsDialog] Failed to log in as " + username);
62             xmppClient.setConnection(null);
63         }
64         dismiss();
65     }
66  
67     private String getText(int id) {
68         EditText widget = (EditText) this.findViewById(id);
69         return widget.getText().toString();
70     }
71 }

Here Is A Screen Shot Of The Main Window.

1

Notes



  • In the Recipient field, make sure you add the “@gmail.com”, not just the user id

Here’s The Code For The Main Activity

001 package org.apache.android.xmpp;
002  
003 import android.app.Activity;
004 import android.os.Bundle;
005 import android.os.Handler;
006 import android.util.Log;
007 import android.view.View;
008 import android.widget.ArrayAdapter;
009 import android.widget.Button;
010 import android.widget.EditText;
011 import android.widget.ListView;
012 import org.jivesoftware.smack.PacketListener;
013 import org.jivesoftware.smack.XMPPConnection;
014 import org.jivesoftware.smack.filter.MessageTypeFilter;
015 import org.jivesoftware.smack.filter.PacketFilter;
016 import org.jivesoftware.smack.packet.Message;
017 import org.jivesoftware.smack.packet.Packet;
018 import org.jivesoftware.smack.util.StringUtils;
019  
020 import java.util.ArrayList;
021  
022 public class XMPPClient extends Activity {
023  
024     private ArrayList<String> messages = new ArrayList();
025     private Handler mHandler = new Handler();
026     private SettingsDialog mDialog;
027     private EditText mRecipient;
028     private EditText mSendText;
029     private ListView mList;
030     private XMPPConnection connection;
031  
032     /**
033      * Called with the activity is first created.
034      */
035     @Override
036     public void onCreate(Bundle icicle) {
037         super.onCreate(icicle);
038         setContentView(R.layout.main);
039  
040         mRecipient = (EditText) this.findViewById(R.id.recipient);
041         mSendText = (EditText) this.findViewById(R.id.sendText);
042         mList = (ListView) this.findViewById(R.id.listMessages);
043         setListAdapter();
044  
045         // Dialog for getting the xmpp settings
046         mDialog = new SettingsDialog(this);
047  
048         // Set a listener to show the settings dialog
049         Button setup = (Button) this.findViewById(R.id.setup);
050         setup.setOnClickListener(new View.OnClickListener() {
051             public void onClick(View view) {
052                 mHandler.post(new Runnable() {
053                     public void run() {
054                         mDialog.show();
055                     }
056                 });
057             }
058         });
059  
060         // Set a listener to send a chat text message
061         Button send = (Button) this.findViewById(R.id.send);
062         send.setOnClickListener(new View.OnClickListener() {
063             public void onClick(View view) {
064                 String to = mRecipient.getText().toString();
065                 String text = mSendText.getText().toString();
066  
067                 Log.i("XMPPClient""Sending text [" + text + "] to [" + to + "]");
068                 Message msg = new Message(to, Message.Type.chat);
069                 msg.setBody(text);
070                 connection.sendPacket(msg);
071                 messages.add(connection.getUser() + ":");
072                 messages.add(text);
073                 setListAdapter();
074             }
075         });
076     }
077  
078     /**
079      * Called by Settings dialog when a connection is establised with the XMPP server
080      *
081      * @param connection
082      */
083     public void setConnection
084             (XMPPConnection
085                     connection) {
086         this.connection = connection;
087         if (connection != null) {
088             // Add a packet listener to get messages sent to us
089             PacketFilter filter = new MessageTypeFilter(Message.Type.chat);
090             connection.addPacketListener(new PacketListener() {
091                 public void processPacket(Packet packet) {
092                     Message message = (Message) packet;
093                     if (message.getBody() != null) {
094                         String fromName = StringUtils.parseBareAddress(message.getFrom());
095                         Log.i("XMPPClient""Got text [" + message.getBody() + "] from [" + fromName + "]");
096                         messages.add(fromName + ":");
097                         messages.add(message.getBody());
098                         // Add the incoming message to the list view
099                         mHandler.post(new Runnable() {
100                             public void run() {
101                                 setListAdapter();
102                             }
103                         });
104                     }
105                 }
106             }, filter);
107         }
108     }
109  
110     private void setListAdapter
111             () {
112         ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
113                 R.layout.multi_line_list_item,
114                 messages);
115         mList.setAdapter(adapter);
116     }
117 }

Download Source And APK From Here – XMPPClient.Zip


Posted by 삼스
Android/XMPP2010. 10. 6. 17:17
smack project site -> http://www.igniterealtime.org/projects/smack/index.jsp
javadoc -> http://www.igniterealtime.org/builds/smack/docs/latest/javadoc/
android project talkmyphone -> http://code.google.com/p/talkmyphone/
Posted by 삼스
Android/XMPP2010. 10. 6. 16:55
http://www.igniterealtime.org/builds/smack/docs/latest/documentation/processing.html
Processing Incoming Packets 

수신되는 패킷을 처리하는데 두가지 방식을 지원한다.

  • org.jivesoftware.smack.PacketCollector -- 새로운 패킷이 올때까지 동기적으로 대기하는 클래스
  • org.jivesoftware.smack.PacketListener -- 수신되는 패킷을 비동기적으로 받는 인터페이스
패킷리스너는 이벤트스타일의 프로그래밍이다. 반면에 패킷콜렉터는 당신이 폴링으로 수집한 데이터를 큐잉하여 받게되며 그 동작은 블럭된다. 패킷리스너는 수신중에 언제든지 다른 작업이 가능하며 콜렉터는 원하는 패킷이 올때가지 대기할때 유용하다. 이 두가지는 모두 XMPPConnection의 인스턴스로부터 얻을수 있다.

org.jivesoftware.smack.filter.PacketFilter인터페이스는 패킷리스너와 패킷콜렉터가 전달받을킷을러주는 역할을 한다. org.jivesoftware.smack.filter패키지에 미리정의된 많은 사용가능한 필터들이 정의되어 있다.

아래 코드는 패킷리스너와 패킷콜렉터를 모두 사용하는 데모이다.

 // Create a packet filter to listen for new messages from a particular
// user. We use an AndFilter to combine two other filters.
PacketFilter filter = new AndFilter(new PacketTypeFilter(Message.class), 
        new FromContainsFilter("mary@jivesoftware.com"));
// Assume we've created an XMPPConnection name "connection".

// First, register a packet collector using the filter we created.
PacketCollector myCollector = connection.createPacketCollector(filter);
// Normally, you'd do something with the collector, like wait for new packets.

// Next, create a packet listener. We use an anonymous inner class for brevity.
PacketListener myListener = new PacketListener() {
        public void processPacket(Packet packet) {
            // Do something with the incoming packet here.
        }
    };
// Register the listener.
connection.addPacketListener(myListener, filter);



Standard Packet Filters

아래는 Smack에서 제공하는 패킷필터들의 셋이다. PacketFilter인터페이스로 새로운 필터를 정의할수도 있다. 디폴트셋은 아래와 같다.
  • PacketTypeFilter -- 클래스타입을 지정할 수 있다. 
  • PacketIDFilter -- 패킷 ID별로 지정할 수 있다.
  • ThreadFilter -- 메세지패킷의 스레드 ID별로 지정할 수 있다.
  • ToContainsFilter -- 받는 주소로 지정할 수 있다.
  • FromContainsFilter -- 보내는 주소로 지정할 수 있다.
  • PacketExtensionFilter -- 패킷의 확장부분을 지정할 수 있다.
  • AndFilter -- 논리적 AND 연산을 두개이상의 필터로 지정할 수 있도록 해준다.
  • OrFilter -- 논리적 OR 연산을 두개이상의 필터로 지정할 수 있도록 해준다.
  • NotFilter -- 논리적 NOT 연산을 두개이상의 필터로 지정할 수 있도록 해준다.

Posted by 삼스
Android/XMPP2010. 10. 6. 16:31
Smack은 opensource로 제공되는  XMPP library이다. 안드로이드에서 사용이 가능하다.
실시간으로 XMPP server와 통신이 가능하고 인스탄트메세징과 채팅이 가능하다

아래와 같이 지극히 단순한 코드로 단문메세지를 전송할 수 있다.
XMPPConnection connection = new XMPPConnection("jabber.org");
connection.connect();
connection.login("mtucker", "password");
Chat chat = connection.getChatManager().createChat("jsmith@jivesoftware.com", new MessageListener() {

    public void processMessage(Chat chat, Message message) {
        System.out.println("Received message: " + message);
    }
});
chat.sendMessage("Howdy!"); 

프로그래머에게 패킷레벨의 접근이 필요없으며 Chat, Roater같은 상위레벨의 클래스를 사용하여 프로그래밍이 가능하다
XMPP XML format이나 XML에 대해서도 알필요가 없다.


Posted by 삼스