使用smack Android发送文件时发生异常

file_upload.setOnClickListener { // Create the file transfer manager var fileTransferInstance:FileTransferManager=FileTransferManager.getInstanceFor(connection) Log.i("id is ",contactPersonJId) // Create the outgoing file transfer fileTransderReceiver=fileTransferInstance.createOutgoingFileTransfer(contactPersonJId+"/Smack") //location of the file locateFile() } fun locateFile(){ var intent:Intent= Intent(applicationContext,LocateImage::class.java ) startActivityForResult(intent,IMAGE_REQUEST) } override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) { super.onActivityResult(requestCode, resultCode, data) when(requestCode){ IMAGE_REQUEST -> { when(resultCode){ Activity.RESULT_OK -> { val selectedImagePath=data?.getSerializableExtra(LocateImage.Obj.MEDIA_DATA) as? String Log.v("file path is ",selectedImagePath) if(!selectedImagePath.isNullOrEmpty()){ fileTransderReceiver?.sendFile(File(selectedImagePath),"First file") GetFileResponse().execute(fileTransderReceiver) } } } } } private inner class GetFileResponse : AsyncTask<OutgoingFileTransfer?, String,fileTransfer>() { var progressDialog: ProgressDialog? = null override fun onPreExecute() { progressDialog = ProgressDialog.show(this@chatActivity, null, "Sending File..."); } override fun doInBackground(vararg params: OutgoingFileTransfer?): fileTransfer { while (!params[0]?.isDone!!){ if (params[0]?.status!!.equals(FileTransfer.Status.error)){ println("ERROR!!! " + params[0]?.error); }else{ println(params[0]?.status); println(params[0]?.progress); } } var fileTransferState=fileTransfer("abc") fileTransferState.status=params[0]?.status fileTransferState.exception=params[0]?.exception fileTransferState.error=params[0]?.error return fileTransferState } override fun onPostExecute(result: fileTransfer?) { if(progressDialog!!.isShowing) progressDialog!!.dismiss() Log.v("Status is ",result?.status.toString()) Log.v("Exception is ",result?.exception.toString()) Log.v("Error is ",result?.error.toString()) } } 

logcat的:

 05-05 18:03:34.593 30459-30459/com.example.itstym.chat_3 V/Status is: Error 05-05 18:03:34.593 30459-30459/com.example.itstym.chat_3 V/Exception is: org.jivesoftware.smack.XMPPException$XMPPErrorException: XMPPError: service-unavailable - cancel 05-05 18:03:34.593 30459-30459/com.example.itstym.chat_3 V/Error is: null 

我也检查了文件发送/接收服务可用的ejabberd服务器。 即使进展也不是从0更新,而是从初始到谈判转移。

更新:

发送XML

http://jabber.org/protocol/si'id ='jsi_8960344439394443510'mime-type ='image / jpeg'profile ='http://jabber.org/protocol/si/profile/file-transfer'>第一个文件http://jabber.org/protocol/bytestreams http://jabber.org/protocol/ibb

接收XML

  <iq xml:lang='en' to='user1@replica3377.cloudapp.net/Android' from='user2@replica3377.cloudapp.net/Smack' type='error' id='0GfHL-35'><si xmlns='http://jabber.org/protocol/si' id='jsi_8960344439394443510' mime-type='image/jpeg' profile='http://jabber.org/protocol/si/profile/file-transfer'><file xmlns='http://jabber.org/protocol/si/profile/file-transfer' name='attachment.jpg'><desc>First file</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='503' type='cancel'><service-unavailable xmlns='urn:ietf:params:xml:ns:xmpp-stanzas'/><text xml:lang='en' xmlns='urn:ietf:params:xml:ns:xmpp-stanzas'>User session not found</text></error></iq>, 

它说错误代码503服务不可用。

[这个链接] [1]说503错误意味着你没有提供完整的JId。

要获得完整的jId:

 roster.getPresence(entry.user).from 

它也以user2@replica3377.cloudapp.net的形式返回Jid

更新2:

使用下面的代码我检查天气文件传输服务是否availabe或不

var serviceDiscoveryManager:ServiceDiscoveryManager = ServiceDiscoveryManager.getInstanceFor(connection)serviceDiscoveryManager.addFeature(“ http://jabber.org/protocol/disco#info ”)serviceDiscoveryManager.addFeature(“jabber:iq:privacy”)

  Log.i("Service enable ",FileTransferNegotiator.isServiceEnabled(connection).toString()) 

但是它返回false意味着文件传输服务不可用。

如何启用文件传输服务? FileTransferNegotiator。 setServiceEnabled()函数不可用。