2012-12-22 73 views
1

我有一個完全適用於Java的Chat Client。現在我期待在我的android設備上實現相同的功能。我開始在項目中導入ASmack庫,並在每個階段檢查以避免錯誤。儘管如此,我還是陷入了第一步。我的活動無法在ASmack Jar中找到該課程,因此我認爲我無法繼續。無法將XMPP服務器與Android中的ASmack連接起來

我的簡單代碼:

package com.test.mypro; 

import org.jivesoftware.smack.ConnectionConfiguration; 
import org.jivesoftware.smack.XMPPConnection; 
import org.jivesoftware.smack.XMPPException; 

import android.app.Activity; 
import android.os.Bundle; 
import android.util.Log; 
import android.widget.TextView; 

    public class TestProjectActivity extends Activity { 
     private static final String LOG_TAG = "TESTER"; 
     /** Called when the activity is first created. */ 
     TextView tvHello; 
     XMPPConnection connection; 
     ConnectionConfiguration config; 

     @Override 
     public void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 
      setContentView(R.layout.main); 
      tvHello = (TextView) findViewById(R.id.tvHello); 
      Log.i(LOG_TAG, "I'm here"); 
      config = new ConnectionConfiguration("prc.p1.im", 5222, "prc.p1.im"); 
      connection = new XMPPConnection(config); 
      try { 
       connection.connect(); 
       // tvHello.setText("Connected to XMPP server"); 
       Log.i(LOG_TAG, "Successfully Connected"); 
      } catch (XMPPException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
       Log.e(LOG_TAG, "Not Connected"); 
      } 
     } 
    } 

logcat的日誌:

12-22 15:58:16.319: E/dalvikvm(828): Could not find class 'org.jivesoftware.smack.ConnectionConfiguration', referenced from method com.test.mypro.TestProjectActivity.onCreate 
12-22 15:58:16.339: W/dalvikvm(828): VFY: unable to resolve new-instance 26 (Lorg/jivesoftware/smack/ConnectionConfiguration;) in Lcom/test/mypro/TestProjectActivity; 
12-22 15:58:16.339: D/dalvikvm(828): VFY: replacing opcode 0x22 at 0x0019 
12-22 15:58:16.351: D/dalvikvm(828): VFY: dead code 0x001b-0047 in Lcom/test/mypro/TestProjectActivity;.onCreate (Landroid/os/Bundle;)V 
12-22 15:58:16.509: I/TESTER(828): I'm here 
12-22 15:58:16.519: D/AndroidRuntime(828): Shutting down VM 
12-22 15:58:16.519: W/dalvikvm(828): threadid=1: thread exiting with uncaught exception (group=0x40015560) 
12-22 15:58:16.561: E/AndroidRuntime(828): FATAL EXCEPTION: main 
12-22 15:58:16.561: E/AndroidRuntime(828): java.lang.NoClassDefFoundError: org.jivesoftware.smack.ConnectionConfiguration 
12-22 15:58:16.561: E/AndroidRuntime(828): at com.test.mypro.TestProjectActivity.onCreate(TestProjectActivity.java:24) 
12-22 15:58:16.561: E/AndroidRuntime(828): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047) 
12-22 15:58:16.561: E/AndroidRuntime(828): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611) 
12-22 15:58:16.561: E/AndroidRuntime(828): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663) 
12-22 15:58:16.561: E/AndroidRuntime(828): at android.app.ActivityThread.access$1500(ActivityThread.java:117) 
12-22 15:58:16.561: E/AndroidRuntime(828): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931) 
12-22 15:58:16.561: E/AndroidRuntime(828): at android.os.Handler.dispatchMessage(Handler.java:99) 
12-22 15:58:16.561: E/AndroidRuntime(828): at android.os.Looper.loop(Looper.java:123) 
12-22 15:58:16.561: E/AndroidRuntime(828): at android.app.ActivityThread.main(ActivityThread.java:3683) 
12-22 15:58:16.561: E/AndroidRuntime(828): at java.lang.reflect.Method.invokeNative(Native Method) 
12-22 15:58:16.561: E/AndroidRuntime(828): at java.lang.reflect.Method.invoke(Method.java:507) 
12-22 15:58:16.561: E/AndroidRuntime(828): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839) 
12-22 15:58:16.561: E/AndroidRuntime(828): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597) 
12-22 15:58:16.561: E/AndroidRuntime(828): at dalvik.system.NativeStart.main(Native Method) 

附加信息:我已經asmack-Android的7.jar庫中導入。我的AVD正在運行2.3.3 Android。

我認爲由於這個,我得到了致命的例外。 我在這裏錯過了什麼?您的幫助將不勝感激。謝謝。

回答

1
public class ChatScreen extends Activity implements android.view.View.OnClickListener { 

private final static String SERVER_HOST = "192.168.88.158"; 
private final static int SERVER_PORT = 5222; 
private final static String SERVICE_NAME = "192.168.88.158"; 
private final static String LOGIN = "manish"; 
private final static String PASSWORD = "android"; 
private List<String> m_discussionThread; 
private ArrayAdapter<String> m_discussionThreadAdapter; 
private XMPPConnection m_connection; 
private Handler m_handler; 
public static ArrayList<HashMap<String, String>> usersList; 
Presence presence; 
ListView list; 
int position = 0; 

private Button BackBtn; 
private Button EndChatBtn; 
TextView opretor; 
    String opretorName; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    requestWindowFeature(Window.FEATURE_NO_TITLE); 
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, 
      WindowManager.LayoutParams.FLAG_FULLSCREEN); 
    setContentView(R.layout.chat_screen); 
    usersList=new ArrayList<HashMap<String, String>>(); 
    Intent intent = getIntent(); 
    opretorName = intent.getStringExtra("opName"); 
    opretor = (TextView) findViewById(R.id.Opretor1); 
    opretor.setText(opretorName); 
      m_handler = new Handler(); 
    try { 
     initConnection(); 
    } catch (XMPPException e) { 
     e.printStackTrace(); 
    } 
    final EditText recipient = (EditText) this.findViewById(R.id.recipient); 
    recipient.setText(opretorName + "@manishsys-pc"); 
    final EditText message = (EditText) this.findViewById(R.id.message);   
    list = (ListView) this.findViewById(R.id.thread); 

    m_discussionThread = new ArrayList<String>(); 
    m_discussionThreadAdapter = new ArrayAdapter<String>(this, 
      R.layout.multi_line_list_item, m_discussionThread); 
    list.setAdapter(m_discussionThreadAdapter); 

    Button send = (Button) this.findViewById(R.id.send); 
    send.setOnClickListener(new View.OnClickListener() { 
     @SuppressLint("SimpleDateFormat") 
     public void onClick(View view) { 
      String to = recipient.getText().toString(); 
      String text = message.getText().toString(); 

      Message msg = new Message(to, Message.Type.chat); 
      Calendar c = Calendar.getInstance(); 
      SimpleDateFormat formatter = new SimpleDateFormat("yyyy MMMM d, hh:mm a"); 
      String strdt= formatter.format(c.getTime()); 
      msg.setBody(text); 
      m_connection.sendPacket(msg); 
      m_discussionThread.add("me :" + strdt); 
      m_discussionThread.add(text); 
      m_discussionThreadAdapter.notifyDataSetChanged(); 

     } 
    }); 

    BackBtn = (Button) findViewById(R.id.ChatBackBtn); 
    BackBtn.setOnClickListener(this); 
    EndChatBtn= (Button) findViewById(R.id.EndChatBtn); 
    EndChatBtn.setOnClickListener(this); 

} 

private void initConnection() throws XMPPException { 
    //Initialisation de la connexion 
    ConnectionConfiguration config = 
      new ConnectionConfiguration(SERVER_HOST, SERVER_PORT, SERVICE_NAME); 
    m_connection = new XMPPConnection(config); 
    m_connection.connect(); 
    m_connection.login(LOGIN, PASSWORD); 
    presence = new Presence(Presence.Type.available); 
    m_connection.sendPacket(presence); 

    //Get all userList 
    Roster roster = m_connection.getRoster(); 
    String[] urlArray = new String[] { "office" }; 
    roster.createEntry("ravi", "ravi", urlArray); 
    Collection<RosterEntry> entries = roster.getEntries(); 

    for(RosterEntry entry : entries) { 
     HashMap<String, String> map = new HashMap<String, String>(); 
     presence = roster.getPresence(entry.getUser()); 

     Presence.Type type = presence.getType();  

     map.put("USER", entry.getName().toString()); 
     map.put("STATUS", type.toString()); 
     Log.e("USER", entry.getName().toString()); 

     usersList.add(map); 
    } 

// Assume we've created a Connection name "connection". 
    ChatManager chatmanager = m_connection.getChatManager(); 
    Chat newChat = chatmanager.createChat("[email protected] ", new MessageListener() { 
     public void processMessage(Chat chat, Message message) { 
      System.out.println("Received message: " + message); 
     } 
    }); 

    try { 
     newChat.sendMessage("Howdy!"); 
    } 
    catch (XMPPException e) { 
     System.out.println("Error Delivering block"); 
    } 

    //enregistrement de l'écouteur de messages 
    PacketFilter filter = new MessageTypeFilter(Message.Type.chat); 
    m_connection.addPacketListener(new PacketListener() { 
      @SuppressLint("SimpleDateFormat") 
      public void processPacket(Packet packet) { 
       Message message = (Message) packet; 
       if (message.getBody() != null) { 
        String fromName = StringUtils.parseBareAddress(message 
          .getFrom()); 
        Calendar c = Calendar.getInstance(); 
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy MMMM d, hh:mm a"); 
        String strdt= formatter.format(c.getTime()); 
        m_discussionThread.add(fromName + ":" + strdt); 
        m_discussionThread.add(message.getBody()); 

        m_handler.post(new Runnable() { 
         public void run() { 
          list.smoothScrollToPosition(list.getCount()-1); 
          m_discussionThreadAdapter.notifyDataSetChanged(); 
         } 
        }); 
       } 
      } 
     }, filter); 
    } 
} 
@Override 
public void onClick(View v) { 
    // TODO Auto-generated method stub 
    switch (v.getId()) { 
    case R.id.ChatBackBtn: 

     Intent BackIntent = new Intent(ChatScreen.this, 
       OperatorScreen.class); 
     BackIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
     startActivity(BackIntent); 
     ChatScreen.this.finish(); 

     break; 
    case R.id.EndChatBtn: 

     Intent EndChtIntent = new Intent(ChatScreen.this, 
       RatingScreen.class); 
     EndChtIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
     startActivity(EndChtIntent); 
     ChatScreen.this.finish(); 

     break; 

    default: 
     break;` 
    } 
    } 
} 

這是我的代碼,它的工作對我罰款,我希望它有幫助你。 謝謝

0

試試這個

private final static String server_host = "talk.google.com"; 
private final static int SERVER_PORT = 5222; 
private final static String SERVICE_NAME = "gmail.com"; 

ConnectionConfiguration config = new ConnectionConfiguration(server_host, SERVER_PORT , SERVICE_NAME); 
     XMPPConnection m_connection = new XMPPConnection(config); 
       try { 
        SASLAuthentication.supportSASLMechanism("PLAIN"); 
        config.setSASLAuthenticationEnabled(true);  
        m_connection.connect(); 
        Roster.setDefaultSubscriptionMode(Roster.SubscriptionMode.manual); 
       } catch (XMPPException e) { 
        e.printStackTrace(); 
       }  
+0

我想你沒有看到正確的日誌。在日誌中應該注意的主要問題是:''無法找到從方法com.test.mypro.TestProjectActivity.onCreate引用的'org.jivesoftware.smack.ConnectionConfiguration'類。我無法調用COnnectionConfiguration類。儘管如此,我嘗試了你的代碼,得到了相同的結果 – harsh8888

+0

一旦把連接代碼放在單獨的方法中,然後嘗試。 – NagarjunaReddy

+0

請檢查日誌。這個問題存在於我假設的圖書館的入口(日誌的第一行)。它無法取出ConnectionConfiguration類,它將啓動進一步的連接過程,因此將它放在單獨的方法中不應該有任何區別。有什麼建議麼? – harsh8888

0

我找到了解決這個問題。這是由於我對API進行了更新。我從我的項目屬性中刪除了導入的ASmack庫。 Lib文件夾也丟失了。因此,我製作了一個文件夾(「libs」),他在項目中粘貼了ASmack jar。通過清理一次該項目,錯誤得到修復。 謝謝。 Reference

+1

請不要只提供解決方案的鏈接。相反,在這裏描述什麼幫助你解決問題。 – Flow

+0

我已將詳細信息添加到解決方案中。謝謝。 – harsh8888

相關問題