我想訪問Android上的FTP服務器上的文件。從FTP服務器,Android開發訪問文本文件
由於某些原因,每當我導入任何外部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();
}
}
}
你是如何加入的罐子到項目 – Orlymee
,我首先要右鍵點擊項目名稱,properties..add外部JAR ... –