我正在使用ftp連接從服務器下載文本文件。 但偏偏我得到異常:使用FTP服務器的Intent下載文本文件。
android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.PICK dat=64.78.178.19 typ=vnd.android.cursor.dir/lysesoft.andftp.uri (has extras) }
我在manifest.xml中還定義了活動。下面
是我清單文件
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.saltriver.hourdoc"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="8" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.INTERNET"/>
<application android:icon="@drawable/ic_launcher" android:label="@string/app_name">
<activity android:name=".HourdocActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
代碼下載文本文件。
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button downloadFilesButton = (Button) findViewById(R.id.button_download_files_id);
downloadFilesButton.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
Intent intent = new Intent();
intent.setAction(Intent.ACTION_PICK);
// FTP URL (Starts with ftp://, sftp://, ftps:// or scp:// followed by hostname and port).
Uri ftpUri = Uri.parse(ConstantCodes.ftphost);
intent.setDataAndType(ftpUri, ConstantCodes.ftpuri);
// Download
intent.putExtra("command_type", "download");
// FTP credentials (optional)
intent.putExtra("ftp_username", ConstantCodes.userid);
intent.putExtra("ftp_password", ConstantCodes.pwd);
//intent.putExtra("ftp_keyfile", "/sdcard/dsakey.txt");
//intent.putExtra("ftp_keypass", "optionalkeypassword");
// FTP settings (optional)
intent.putExtra("ftp_pasv", "true");
//intent.putExtra("ftp_resume", "true");
//intent.putExtra("ftp_encoding", "UTF-8");
//intent.putExtra("ftps_mode", "implicit");
// Activity title
intent.putExtra("progress_title", "Downloading files ...");
// Remote files to download.
intent.putExtra("remote_file1", ConstantCodes.ftp_remotefile1);
//intent.putExtra("remote_file2", "/remotefolder/subfolder/file2.zip");
// Target local folder where files will be downloaded.
intent.putExtra("local_folder", ConstantCodes.localfolder_path);
intent.putExtra("close_ui", "true");
startActivityForResult(intent,ConstantCodes.DOWNLOAD_FILES_REQUEST);
}
});
}
protected void onActivityResult(int requestCode, int resultCode, Intent intent)
{
Log.i(TAG, "Result: "+resultCode+ " from request: "+requestCode);
if (intent != null)
{
String transferredBytesStr = intent.getStringExtra("TRANSFERSIZE");
String transferTimeStr = intent.getStringExtra("TRANSFERTIME");
Log.i(TAG, "Transfer status: " + intent.getStringExtra("TRANSFERSTATUS"));
Log.i(TAG, "Transfer amount: " + intent.getStringExtra("TRANSFERAMOUNT") + " file(s)");
Log.i(TAG, "Transfer size: " + transferredBytesStr + " bytes");
Log.i(TAG, "Transfer time: " + transferTimeStr + " milliseconds");
// Compute transfer rate.
if ((transferredBytesStr != null) && (transferTimeStr != null))
{
try
{
long transferredBytes = Long.parseLong(transferredBytesStr);
long transferTime = Long.parseLong(transferTimeStr);
double transferRate = 0.0;
if (transferTime > 0) transferRate = ((transferredBytes) * 1000.0)/(transferTime * 1024.0);
Log.i(TAG, "Transfer rate: " + transferRate + " KB/s");
}
catch (NumberFormatException e)
{
// Cannot parse string.
}
}
}
}
建議我在哪裏做錯了。
您好,我使用相同的上傳,但它提供了異常:java.lang.NoClassDefFoundError:org.apache.commons。 net.ftp.FTPClient。你可以幫我嗎? – Dhruvisha