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([email protected],
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的發送文件:異常,而使用Android的咂嘴
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類型= '圖像/ JPEG' 輪廓=' http://jabber.org/protocol/si/profile/file-transfer「>第一個文件http://jabber.org/protocol/bytestreamshttp://jabber.org/protocol/ibb
接收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>,
它說,錯誤代碼503服務不可用。
[此鏈接] [1]說503錯誤意味着你沒有提供完整的JId。使用下面的代碼我
:
要獲得完整的JID:
roster.getPresence(entry.user).from
它也返回JID在[email protected]形式
更新2檢查天氣文件傳輸服務是否可用
var serviceDiscoveryManager:ServiceDiscoveryManager = Se rviceDiscoveryManager.getInstanceFor(連接) serviceDiscoveryManager.addFeature( 「http://jabber.org/protocol/disco#info 」) serviceDiscoveryManager.addFeature(「 胡言亂語:IQ:隱私」)
Log.i("Service enable ",FileTransferNegotiator.isServiceEnabled(connection).toString())
但它返回false,這意味着文件傳輸服務不可用。
如何啓用文件傳輸服務? FileTransferNegotiator。 setServiceEnabled()函數不可用。