因为asmack有一些功能没有实现,其中包括传输文件的功能,所以要先修改asmack的源码。
如果不修改会出现这样一个包
<streamhost jid="myid@mydomain.com/smack" host="127.0.0.1" port="7777"/>
服务器地址会被解析成127.0.0.1 导致这样一个错误。貌似smack 3.2.1 IBB 传输时有bug也会报这个错。
Could not establish socket with any provided host
推荐使用强大的eclipse来编译和打包源码,只在要在源码路径下建一个工程,eclipse会帮你生成好目录,
asmack依赖两个包,xmlpull和android的Apache包。后者不用理会,只需要吧xmlpull这个包加进去就可以了,不用理会报错,直接Export就行。
只需要吧 org.jivesoftware.smackx.filetransfer.Socks5TransferNegotiator.discoverLocalIP() 改为
private String discoverLocalIP() throws UnknownHostException {
try {
for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements();) {
NetworkInterface intf = en.nextElement();
for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements();) {
InetAddress inetAddress = enumIpAddr.nextElement();
if (!inetAddress.isLoopbackAddress()) {
return inetAddress.getHostAddress().toString();
}
}
}
} catch (SocketException ex) {
Logger.error("Error retrieving the local IP", ex);
}
throw new UnknownHostException("Failed to retrieve local IP");
//return InetAddress.getLocalHost().getHostAddress();
}
就可以了。
源代码中是这样的。。。。。。。非常坑爹 - - !
private String discoverLocalIP() throws UnknownHostException {
return InetAddress.getLocalHost().getHostAddress();
}
英文原版网址:
http://code.google.com/p/asmack/issues/detail?id=61
private static void sendFile(String user,XMPPConnection conn,File file) throws Exception{
FileTransferManager manager = new FileTransferManager(conn);
OutgoingFileTransfer transfer = manager.createOutgoingFileTransfer(user);
transfer.sendFile(file,"data backup");
manager.addFileTransferListener(new FileTransferListener() {
public void fileTransferRequest(FileTransferRequest request) {
try{
Log.e("Have file","file");
IncomingFileTransfer transfer = request.accept();
transfer.recieveFile(new File("complete_works.txt"));
}catch(Exception e){
Log.e("RecFile Ex In!",e.getMessage());
}
}
});
}
private static void setFileListener(final XMPPConnection conn) {
Thread thread = new Thread() {
public void run() {
ServiceDiscoveryManager sdm = ServiceDiscoveryManager
.getInstanceFor(conn);
if (sdm == null)
sdm = new ServiceDiscoveryManager(conn);
sdm.addFeature("http://jabber.org/protocol/disco#info");
sdm.addFeature("jabber:iq:privacy");
// Create the file transfer manager
final FileTransferManager managerListner = new FileTransferManager(conn);
FileTransferNegotiator.setServiceEnabled(conn, true);
Log.i("File transfere manager", "created");
// Create the listener
managerListner.addFileTransferListener(new FileTransferListener() {
@Override
public void fileTransferRequest(final FileTransferRequest request) {
Log.i("Recieve File","new file transfere request new file transfere request new file transfere request");
Log.i("file request","from" + request.getRequestor());
IncomingFileTransfer transfer = request.accept();
Log.i("Recieve File alert dialog", "accepted");
File file = new File("/sdcard/data/"+ request.getFileName());
try {
if (!file.exists()){
Log.i("have no file",file.getPath());
file.createNewFile();
}
transfer.recieveFile(file);
}catch (Exception e) {
e.printStackTrace();
}
}
});
}
};
thread.start();
}