我在使用Smack Api和Oepnfire Server的android应用程序中使用以下代码进行文件传输。我没有收到任何错误,但问题是当我发送文件时,我在198行获得取消状态。有谁能够帮我?谢谢你。
String to = mRecipient.getText().toString();
String text = mSendText.getText().toString();
// Uri uri = Uri.fromFile(new
// File("file:///android_asset/test.txt"));
if (new File("/data/data/" + getPackageName() + "/"
+ "test.txt").exists()) {
Log.d("XMPPClient File Exist", "exist");
}
SmackConfiguration.setPacketReplyTimeout(15000);
ConnectionConfiguration config = new ConnectionConfiguration(
"192.168.1.200", 5222);
// config.setRosterLoadedAtLogin(true);
// config.setSendPresence(true);
// config.setSASLAuthenticationEnabled(false);
config.setReconnectionAllowed(true);
connection = new XMPPConnection(config);
/*
* ConnectionConfiguration connConfig = new
* ConnectionConfiguration("192.168.1.200", 5222, "");
* connConfig.setReconnectionAllowed(true);;
*
* connection = new XMPPConnection(connConfig);
*/
try {
connection.connect();
} catch (XMPPException e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
}
try {
connection.connect();
} catch (XMPPException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
// connection.login(userName, password);
ServiceDiscoveryManager servicemanager = ServiceDiscoveryManager
.getInstanceFor(connection);
if (servicemanager == null)
servicemanager = new ServiceDiscoveryManager(connection);
servicemanager
.addFeature("http://jabber.org/protocol/disco#info");
servicemanager.addFeature("jabber:iq:privacy");
// Log.d("XMPPClient Txt File", uri.toString());
try {
// Create the outgoing file transfer
manager = new FileTransferManager(connection);
FileTransferNegotiator.setServiceEnabled(connection, true);
OutgoingFileTransfer transfer = manager
.createOutgoingFileTransfer("test@192.168.1.200/Smack");
// Send the file
// getBaseContext().getAssets().open("Entity");
// OutgoingFileTransfer.setResponseTimeout(30000);
transfer.sendFile(new File("/data/data/" + getPackageName()
+ "/" + "test.txt"), "You won't believe this!");
while (!transfer.isDone()) {
try {
Thread.sleep(1000);
Log.i("transfere file", "sending file status "
+ transfer.getStatus() + "progress: "
+ transfer.getProgress());
if (transfer.getStatus() == org.jivesoftware.smackx.filetransfer.FileTransfer.Status.error) {
transfer.cancel();
Log.e("", transfer.getStatus().toString());
Log.e("", "EEEEEERRRRRRRROOORRRRR");
break;
}
} catch (InterruptedException e) {
// TODO Auto-generated catch block
Log.e("aaaaaaaaaaaaaaa", "aaaa" + e);
e.printStackTrace();
}
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
答案 0 :(得分:0)
是的,您必须设置FileTransferListener并接受文件
public class MyFileTransferListener实现FileTransferListener {
@Override
public void fileTransferRequest(final FileTransferRequest request) {
Boolean result = false;
IncomingFileTransfer ift = request.accept();
这是一个示例代码,在接受获取文件之后..也不要忘记在连接到xmpp服务器时启动监听器