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
I have also check on the ejabberd server that file sending/receiving service is available. Even the progress is not also not updating from 0 but the status is changed from initial to negotiating transfer.
Update:
Sent XML
http://jabber.org/protocol/si' id='jsi_8960344439394443510' mime-type='image/jpeg' profile='http://jabber.org/protocol/si/profile/file-transfer'>First filehttp://jabber.org/protocol/bytestreamshttp://jabber.org/protocol/ibb
Receive XML
<iq xml:lang='en' to='[email protected]/Android' from='[email protected]/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>,
It says error code 503 service unavailable.
[this link][1] say 503 error means you are not providing the full JId.
To get the full jId:
roster.getPresence(entry.user).from
it is also returning Jid in the form of [email protected]
Update 2:
using following code I checked weather file transfer service is availabe or not
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())
But it is returning false that means file transfer service is unavailable.
How to enable file transfer service? FileTransferNegotiator. setServiceEnabled() function is not available.