我正在尝试访问Android中的FTP服务器上的文件。
出于某种原因,每当我导入任何外部jar如commons-net或ftp4j时,我的应用程序在运行时崩溃。我通过评论所有引用罐子的行并且应用程序正常运行来验证这一点。
我想访问FTP服务器上的文本文件中的数据并将其打印到TextView。我整天都被困在这一天。我该如何解决?包含在清单中的权限是互联网和外部存储。
package com.testing.pack;
import java.io.BufferedInputStream;
import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.http.client.HttpClient;
import android.app.Activity;
import android.graphics.Typeface;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.TextView;
public class TestingActivity extends Activity implements OnClickListener {
/** Called when the activity is first created. */
TextView tv;
Button go;
HttpClient client;
FTPClient ftp;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.main);
tv = (TextView) findViewById(R.id.tvTry);
Typeface face = Typeface.createFromAsset(getAssets(),
"Roboto-LightItalic.ttf");
tv.setTypeface(face);
go = (Button) findViewById(R.id.bGO);
go.setOnClickListener(this);
try {
//Attempt using commons-net
FTPClient ftpClient = new FTPClient();
ftpClient.connect("ftp://........./");
ftpClient.login(".......", ".....");
ftpClient.changeWorkingDirectory("......");
ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
BufferedInputStream buffIn=null;
buffIn=new BufferedInputStream(new FileInputStream(file));
ftpClient.enterLocalPassiveMode();
ftpClient.storeFile("test.txt", buffIn);
buffIn.close();
ftpClient.logout();
ftpClient.disconnect();
//Attempt using ftp4j
//FTPClient ftp = new FTPClient();
//ftp.connect("........");
//ftp.login(".......", ".....");
//ftp.changeDirectoryUp();
//ftp.changeDirectory(".....");
//ftp.changeDirectory("public_ftp");
//ftp.download("test.txt", new java.io.File("....."));
} catch (Exception e) {
}
}
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
getData ex = new getData();
try {
tv.setText(ex.getInternetData());
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
答案 0 :(得分:1)
您是否使用Eclipse添加jar?而是创建一个libs文件夹并将罐子复制到其中。
以下是一个类似的问题:How can I use external JARs in an Android project?