2013-05-14 68 views
1

我想在我使用SMACK和Openfire開發的Android聊天應用程序中實現文件傳輸功能。但是發送和接收文件時發生錯誤。因此任何人都可以幫助我解決此問題。感謝您提前。如何在android中使用SMACK創建發送和接收文件的偵聽器?

下面是接收文件的代碼。

ServiceDiscoveryManager sdm = ServiceDiscoveryManager.getInstanceFor(connection); 
     if (sdm == null) 
      sdm = new ServiceDiscoveryManager(connection); 
     sdm.addFeature("http://jabber.org/protocol/disco#info"); 
     sdm.addFeature("jabber:iq:privacy"); 
     XMPPConnection.DEBUG_ENABLED = true; 
     // Create the file transfer manager 
     final FileTransferManager managerListner = new FileTransferManager(connection); 
     FileTransferNegotiator.setServiceEnabled(connection, true); 
     Log.i("File transfere manager", "created"); 
     managerListner.addFileTransferListener(new FileTransferListener() 
     { 

      @Override 
      public void fileTransferRequest(FileTransferRequest request) { 
       // TODO Auto-generated method stub 
       Log.i("Recieve File","new file transfere request new file transfere request new file transfere request"); 
       Log.i("file request","from" + request.getRequestor()); 
       IncomingFileTransfer transfer = request.accept(); 
       Log.i("Recieve File alert dialog", "accepted"); 
       try { 
        transfer.recieveFile(new File("/mnt/sdcard/" + request.getFileName())); 
        while (!transfer.isDone() || (transfer.getProgress() < 1)) { 

         Thread.sleep(1000); 
         Log.i("Recieve File alert dialog", "still receiving : "+ (transfer.getProgress()) + " status "+ transfer.getStatus()); 
          if (transfer.getStatus().equals(org.jivesoftware.smackx.filetransfer.FileTransfer.Status.error)) { 
           // Log.i("Error file", 
           // transfer.getError().getMessage()); 
            Log.i("Recieve File alert dialog", 
             "cancelling still receiving : " 
               + (transfer.getProgress()) 
               + " status " 
               + transfer.getStatus()); 
            transfer.cancel(); 
            break; 
          } 
        } 
       }catch (XMPPException e) { 
        // TODO: handle exception 
         e.printStackTrace(); 
       } catch (InterruptedException e) { 
        // TODO Auto-generated catch block 
         e.printStackTrace(); 
       } 
      }  
     }); 

這裏以下是我出現的錯誤。

02:48:58 PM RCV (1079277904): <iq type="error" id="6gQg0-6" to="admin-pc/cc4dd310" from="[email protected]/Smack"><si xmlns="http://jabber.org/protocol/si" id="jsi_3030055238839806007" profile="http://jabber.org/protocol/si/profile/file-transfer"><file xmlns="http://jabber.org/protocol/si/profile/file-transfer" name="sdcard"><desc>test</desc></file><feature xmlns="http://jabber.org/protocol/feature-neg"><x xmlns="jabber:x:data" type="form"><field var="stream-method" type="list-single"><option><value>http://jabber.org/protocol/bytestreams</value></option><option><value>http://jabber.org/protocol/ibb</value></option></field></x></feature></si><error code="405" type="cancel"><not-allowed xmlns="urn:ietf:params:xml:ns:xmpp-stanzas"/></error></iq> 

回答

0

該錯誤主要發生是因爲您錯過了添加允許共享文件的提供程序管理器的配置。 請添加下面的靜態代碼中提供者管理類

// FileTransfer 
pm.addIQProvider("si", "http://jabber.org/protocol/si",new StreamInitiationProvider()); 
pm.addIQProvider("query","http://jabber.org/protocol/bytestreams",new BytestreamsProvider()); 
pm.addIQProvider("open","http://jabber.org/protocol/ibb", new 
     OpenIQProvider()); 
pm.addIQProvider("data","http://jabber.org/protocol/ibb", new 
DataPacketProvider()); 
pm.addIQProvider("close","http://jabber.org/protocol/ibb", new 
     CloseIQProvider()); 
pm.addExtensionProvider("data","http://jabber.org/protocol/ibb", new 
     DataPacketProvider()); 

以下屬性也添加到您的明火服務器啓用文件共享。

xmpp.proxy.enabled =true 
xmpp.proxy.externalip =your public ip 
xmpp.proxy.port =7777 

這將爲你工作。

+0

你能解釋一下xmpp.proxy.externalip是什麼?在我的情況下,我將http://192.168.0.102:9090設置爲externalip.Is可以嗎? – 2013-05-14 05:55:01

+0

如果你的服務器是本地的,那麼192.168.0.102 如果它是在互聯網上的Web服務器上託管,那麼服務器的IP。用於映射您的域名的 。 但如果你在本地使用它,那麼你只需要192.168.0.102 希望它對你有幫助。 – Bug 2013-05-14 06:20:32

+0

好的非常感謝你幫助我的錯誤。 – 2013-05-14 06:54:43

相關問題