2014-02-12 25 views
0

登錄代碼。登錄成功完成。Asmack文件發送錯誤503類型=用openfire取消

 String host=AppContext.HOST;//myHost 
     String [] params={"a1","aa"} 
     int port=AppContext.PORT;//My Port 2777 
     String service=AppContext.SERVICE; //service = smack 
     ConnectionConfiguration connConfig = new ConnectionConfiguration(
       host, port, service); 
     connConfig.setDebuggerEnabled(true); 
      Connection.DEBUG_ENABLED = true; 
    connConfig.setSASLAuthenticationEnabled(true); 
    XMPPConnection connection = new XMPPConnection(connConfig); 
     Log.e("XMPP LoginActivity Username ",params[0]+" pass "+params[1]); 
     try { 
      connection.connect(); 
      connectionFlg=true; 
      Log.i("XMPP LoginActivity", 
        "Connected to " + connection.getHost()); 
     } catch (XMPPException ex) { 
      Log.e("XMPP LoginActivity", "Failed to connect to " 
        + connection.getHost()); 
      Log.e("XMPP LoginActivity", ex.toString()); 
      appContext.setConnection(null); 
     } 
     try { 
      if(connectionFlg) 
      { 
      connection.login(params[0], params[1]); 
      Log.i("XMPP LoginActivity", 
        "Logged in as " + connection.getUser()); 

      Presence presence = new Presence(Presence.Type.available); 
      presence.setStatus("I’m available"); 
      connection.sendPacket(presence); 
      loginFlg=true; 
      appContext.setConnection(connection); 
      }else 
      { 
       Log.e("XMPP LoginActivity", "Failed to connect to " 
         + connection.getHost()); 
      } 

     } catch (XMPPException ex) { 
      loginFlg=false;      ; 
      Log.e("XMPP LoginActivity", "Failed to log in as " 
        + params[0]); 
      Log.e("XMPP LoginActivity", ex.toString()); 
      appContext.setConnection(null); 
     } 

我的文件發送代碼是波紋管

public void sendFile(String sentTo) { 
    ProviderManager.getInstance().addIQProvider("query","http://jabber.org/protocol/bytestreams", new BytestreamsProvider()); 
    ProviderManager.getInstance().addIQProvider("query","http://jabber.org/protocol/disco#items", new DiscoverItemsProvider()); 
    ProviderManager.getInstance().addIQProvider("query","http://jabber.org/protocol/disco#info", new DiscoverInfoProvider()); 

    FileTransferManager manager = new FileTransferManager(connection); 
    OutgoingFileTransfer transfer = manager.createOutgoingFileTransfer(sentTo); 
    File file = new File(Environment.getExternalStorageDirectory() 
      .getPath() + "/user.json"); 
    try { 
     transfer.sendFile(file, "test_file"); 
    } catch (XMPPException e) { 
     e.printStackTrace(); 
    } 
    while(!transfer.isDone()) { 
     if(transfer.getStatus().equals(Status.error)) { 
      System.out.println("ERROR!!! " + transfer.getError()); 
     } else if (transfer.getStatus().equals(Status.cancelled) 
         || transfer.getStatus().equals(Status.refused)) { 
      System.out.println("Cancelled!!! " + transfer.getError()); 
     } 
     try { 
      Thread.sleep(1000L); 
     } catch (InterruptedException e) { 
      e.printStackTrace(); 
     } 
    } 
    if(transfer.getStatus().equals(Status.refused) || transfer.getStatus().equals(Status.error) 
    || transfer.getStatus().equals(Status.cancelled)){ 
     System.out.println("refused cancelled error " + transfer.getError()); 
    } else { 
     System.out.println("Success"); 
    } 

} 

在發送我得到這個下面兩個XML響應。請看一下。

1)

12:55:16 PM SENT (1092826504): <iq id="SU8c1-17" to="[email protected]" from="[email protected]/Smack" type="set"><si xmlns="http://jabber.org/protocol/si" id="jsi_2427513438410796738" profile="http://jabber.org/protocol/si/profile/file-transfer"><file xmlns="http://jabber.org/protocol/si/profile/file-transfer" name="user.json" size="379" ><desc>test_file</desc></file><feature xmlns="http://jabber.org/protocol/feature-neg"><x xmlns="jabber:x:data" type="form"><field var="stream-method" type="list-multi"><option><value>http://jabber.org/protocol/bytestreams</value></option><option><value>http://jabber.org/protocol/ibb</value></option></field></x></feature></si></iq> 

2)

12:55:16 PM RCV (1092826504): <iq type="error" id="SU8c1-17" from="[email protected]" to="[email protected]/Smack"><si xmlns="http://jabber.org/protocol/si" id="jsi_2427513438410796738" profile="http://jabber.org/protocol/si/profile/file-transfer"><file xmlns="http://jabber.org/protocol/si/profile/file-transfer" name="user.json" size="379"><desc>test_file</desc></file><feature xmlns="http://jabber.org/protocol/feature-neg"><x xmlns="jabber:x:data" type="form"><field var="stream-method" type="list-multi"><option><value>http://jabber.org/protocol/bytestreams</value></option><option><value>http://jabber.org/protocol/ibb</value></option></field></x></feature></si><error code="503" type="cancel"><service-unavailable xmlns="urn:ietf:params:xml:ns:xmpp-stanzas"/></error></iq> 

請朋友有所幫助。我已經嘗試了很多代碼片段。因爲三天,我很努力,只有:(

+0

請澄清你的問題 - 應該發生什麼,發生了什麼? – Smutje

+0

發送文件時發生503錯誤。看到第二個XML,它們是503錯誤代碼和服務不可用。這是主要問題。 :( – addy

+0

由於他們的服務返回503,主要負責接收方,你不可能做任何事情來解決這個問題。 – Smutje

回答

0

我同樣的問題,我研究了節,並解決了這樣的看法。

許多人使用「/啪」「/資源」 。在JID資源的一部分,但這是錯誤的

資源路徑與每一個存在變化改變了用戶假設我們要發送圖片到該用戶: 「USER1 @ MYDOMAIN」

您必須添加「/資源」一部分,這個JID,它成爲本: USER1 @ MYDOMAIN /資源

/資源路徑與存在變化,所以你必須遵循每一個在場的變化更新資源路徑。 最好的辦法是讓用戶的存在是在名冊聽衆和presencheChanged()方法,你會得到最後的用戶資源部分是這樣的:

Roster roster=getRoster(); 
roster.addRosterListener(new RosterListener() { 
       @Override 
       public void entriesAdded(Collection<Jid> addresses) { 
        Log.d("entriesAdded", "ug"); 
        context.sendBroadcast(new Intent("ENTRIES_ADDED")); 
       } 

       @Override 
       public void entriesUpdated(Collection<Jid> addresses) { 
        Log.d("entriesUpdated", "ug"); 
       } 

       @Override 
       public void entriesDeleted(Collection<Jid> addresses) { 
        Log.d("entriesDeleted", "ug"); 
       } 

       @Override 
       public void presenceChanged(Presence presence) { 
        Log.d("presenceChanged", "ug"); 
        //Resource from presence 
        String resource = presence.getFrom().getResourceOrEmpty().toString(); 
        //Update resource part for user in DB or preferences 
        //... 
       } 
      }); 
} 

資源串會像「6u1613j3kv」某些生成的字符串和JID將變爲:

[email protected]/6u1613j3kv 

這意味着,你必須創建這樣引出傳送:

EntityFullJid jid = JidCreate.entityFullFrom("[email protected]/6u1613j3kv"); 
OutgoingFileTransfer transfer = manager.createOutgoingFileTransfer(jid) 
transfer.sendFile(new File("DirectoryPath"), "Description"); 

那就是如何我已經解決了我的問題與文件在smack和Openfire上傳輸。

在您的案例中sendFile(String sentTo)函數sentTo必須像我的jid那樣形成,資源路徑隨着每次rpesence更改而變化。

另外提一下,您必須在您的Openfire服務器添加以下屬性:

xmpp.proxy.enabled - true 
xmpp.proxy.externalip - MY_IP_ADDRESS 
xmpp.proxy.port - 7777 

就更不用說了,我使用的Openfire 4.0.2和4.2.2啪。