2014-03-07 70 views
0

我試圖通過XMPPClient從我的Android設備連接到明火服務器。我在Async-Task上單獨運行Network thread致命異常錯誤的Android

問題是

我的代碼是不是讓我連接到Openfire的服務器,但它與Gmail服務器可以輕鬆連接。我錯過了我的代碼中的一些東西?

2-當我的系統中獲取任何服務器連接的那一刻,說的Gmail,它崩潰,我得到我的LogCat

錯誤以下錯誤

03-07 12:29:57.085: W/dalvikvm(26662): threadid=11: thread exiting with uncaught exception (group=0x40a95210) 
03-07 12:29:57.115: E/AndroidRuntime(26662): FATAL EXCEPTION: AsyncTask #1 
03-07 12:29:57.115: E/AndroidRuntime(26662): java.lang.RuntimeException: An error occured while executing doInBackground() 
03-07 12:29:57.115: E/AndroidRuntime(26662): at android.os.AsyncTask$3.done(AsyncTask.java:278) 
03-07 12:29:57.115: E/AndroidRuntime(26662): at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273) 
03-07 12:29:57.115: E/AndroidRuntime(26662): at java.util.concurrent.FutureTask.setException(FutureTask.java:124) 
03-07 12:29:57.115: E/AndroidRuntime(26662): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307) 
03-07 12:29:57.115: E/AndroidRuntime(26662): at java.util.concurrent.FutureTask.run(FutureTask.java:137) 
03-07 12:29:57.115: E/AndroidRuntime(26662): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:208) 
03-07 12:29:57.115: E/AndroidRuntime(26662): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076) 
03-07 12:29:57.115: E/AndroidRuntime(26662): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569) 
03-07 12:29:57.115: E/AndroidRuntime(26662): at java.lang.Thread.run(Thread.java:856) 
03-07 12:29:57.115: E/AndroidRuntime(26662): Caused by: java.lang.NullPointerException 
03-07 12:29:57.115: E/AndroidRuntime(26662): at com.example.test.NetworkOperation.doInBackground(NetworkOperation.java:42) 
03-07 12:29:57.115: E/AndroidRuntime(26662): at com.example.test.NetworkOperation.doInBackground(NetworkOperation.java:1) 
03-07 12:29:57.115: E/AndroidRuntime(26662): at android.os.AsyncTask$2.call(AsyncTask.java:264) 
03-07 12:29:57.115: E/AndroidRuntime(26662): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305) 

這裏是在Async中運行的代碼任務

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

import android.os.AsyncTask; 
import android.util.Log; 

public class NetworkOperation extends AsyncTask<String, Void, String> { 

    private XMPPClient xmppClient; 
    @Override 
    protected String doInBackground(String... urls) { 

     String host = "talk.google.com"; //getText(R.id.host); 
     String port = "5222"; //getText(R.id.port); 
     String service = "gmail.com"; //getText(R.id.service); 
     String username = "[email protected]"; //getText(R.id.userid); 
     String password = "**********"; //getText(R.id.password); 

     // Create a connection 
     ConnectionConfiguration connConfig = 
       new ConnectionConfiguration(host, Integer.parseInt(port),service); 
     XMPPConnection connection = new XMPPConnection(connConfig); 

     try { 
      connection.connect(); 
      Log.i("XMPPClient", "[SettingsDialog] Connected to " + connection.getHost()); 
     } catch (XMPPException ex) { 
      Log.e("XMPPClient", "[SettingsDialog] Failed to connect to " + connection.getHost()); 
      xmppClient.setConnection(null); 
     } 
     try { 
      connection.login(username, password); 
      Log.i("XMPPClient", "Logged in as " + connection.getUser()); 

      // Set the status to available 
      Presence presence = new Presence(Presence.Type.available); 
      connection.sendPacket(presence); 
      xmppClient.setConnection(connection); 
     } catch (XMPPException ex) { 
      Log.e("XMPPClient", "[SettingsDialog] Failed to log in as " + username); 
      xmppClient.setConnection(null); 
     } 

     return null; 
    } 

    @Override 
    protected void onPostExecute(String result) { 

    } 
    } 

我錯過了什麼嗎? N +

+0

我現在唯一不認爲在你的代碼中是雙'try'塊。如果第一個失敗,第二個仍然執行。 – Merlevede

+0

在第二個try塊中,我在執行這行之後得到了uncaught異常xmppClient.setConnection(connection); –

回答

0

看起來你從來沒有分配到任何你xmppClient變量,因此它是空。

你不打它第一次嘗試作爲一個正在取得成功。

0

請嘗試使用以下代碼。

@Override 
protected String doInBackground(String... urls) { 

    String host = "talk.google.com"; //getText(R.id.host); 
    String port = "5222"; //getText(R.id.port); 
    String service = "gmail.com"; //getText(R.id.service); 
    String username = "[email protected]"; //getText(R.id.userid); 
    String password = "**********"; //getText(R.id.password); 

    // Create a connection 
    ConnectionConfiguration connConfig = 
      new ConnectionConfiguration(host, Integer.parseInt(port),service); 
    XMPPConnection connection = new XMPPConnection(connConfig); 
    if(xmppClient != null){ 
    try { 
     connection.connect(); 
     Log.i("XMPPClient", "[SettingsDialog] Connected to " + connection.getHost()); 
     connection.login(username, password); 
     Log.i("XMPPClient", "Logged in as " + connection.getUser()); 

     // Set the status to available 
     Presence presence = new Presence(Presence.Type.available); 
     connection.sendPacket(presence); 
     xmppClient.setConnection(connection); 
    } catch (XMPPException ex) { 
     Log.e("XMPPClient", "[SettingsDialog] Failed to connect to " + connection.getHost()); 
     xmppClient.setConnection(null); 
    } 
    }else{ 
     Log.d("Connection", "check your xmppClient"); 
    } 

    return null; 
}