在我的 android 项目中,我需要显示搜索栏和网络速度(文件使用 FTP 协议从服务器上传/下载的速度有多快)。在我的情况下,当我尝试从模拟器 sdcard 上传文件时,它会在文件存储在服务器中时显示速度和搜索栏,但我的要求是我需要在文件上传时连续显示搜索栏和网络速度进入服务器。我不明白我在代码中哪里做错了。以下是我将文件上传到 ftp 服务器的代码:
提前致谢。
@Override
protected String doInBackground(String... arg0) {
int count = 0;
FTPClient ObjFtpCon = new FTPClient();
//Toast.makeText(con, "FTPasync doInBackground() is called" ,Toast.LENGTH_SHORT).show();
try {
runOnUiThread(new Runnable() {
public void run() {
bar.setProgress(0);
//real_time.setText(0 + " secs");
test_avg.setText(0+ " kbps");
//peak.setText(0+" kbps");
}
});
updateUI(pp, R.drawable.pause);
//ObjFtpCon.connect("ftp.customhdclips.com");
ObjFtpCon.connect("ftp."+map.get("url").toString());
updateUI(status, "Connecting");
//if (ObjFtpCon.login("fstech@customhdclips.com", "fstech123")) {
if (ObjFtpCon.login(map.get("username").toString(), map.get("password").toString())) {
updateUI(status, "Connected");
ObjFtpCon.enterLocalPassiveMode(); // important!
ObjFtpCon.cwd("/");// to send the FTP CWD command to the server, receive the reply, and return the reply code.
//if (mode == 0) {
if(Integer.parseInt((map.get("oprn").toString()))== 0){ // Download File Using FTP Protocol
System.out.println("download test is called");
File objfile = new File(Environment.getExternalStorageDirectory() + File.separator + "/logo.png");
objfile.createNewFile();
FileOutputStream objFos = new FileOutputStream(objfile);
boolean blnresult = ObjFtpCon.retrieveFile("/logo.png", objFos);
objFos.close();
if (blnresult) {
// toast("Download succeeded");
// toast("Stored at : " +
// objfile.getAbsolutePath());
}
//***********************************************************
/*
File objfile = new File(
Environment.getExternalStorageDirectory()
+ File.separator + "/test.txt");
// System.out.println("total" + objfile.getTotalSpace() + " bytes");
objfile.createNewFile();
FileOutputStream objFos = new FileOutputStream(objfile);
boolean blnresult = ObjFtpCon.retrieveFile("/test.txt",
objFos);
objFos.close();
if (blnresult) {
System.out.println("download in ftp is successful");
// toast("Download succeeded");
// toast("Stored at : " +
// objfile.getAbsolutePath());
}*/
}
else {
// Upload
System.out.println("upload test is called");
//Toast.makeText(con, "upload FTP test is called", Toast.LENGTH_SHORT).show();
//ContextWrapper context = null;
//assetManager= context.getAssets();
assetManager = getResources().getAssets();
input1 = assetManager.open("hello.txt");
long total = 0;
long sleepingTime= 0;
final long started = System.currentTimeMillis();
int size = input1.available();
byte[] buffer = new byte[size];
byte dataByte[] = new byte[1024];
//byte dataByte[] = new byte[(int)((CharSequence) input1).length()];
//input1.read(buffer);
//final int lenghtOfFile = data.getBytes().length;
final int lenghtOfFile = input1.toString().getBytes().length;
System.out.println("length of file....."+lenghtOfFile);
ByteArrayInputStream byteArrayIS = new ByteArrayInputStream(input1.toString().getBytes());
System.out.println("ByteArrayIS value is...."+byteArrayIS);
System.out.println("started time --"+started);
updateUI(status, "Uploading");
while ((count = byteArrayIS.read(dataByte)) != -1)
{
System.out.println("read value is...."+byteArrayIS.read(dataByte));
while (sleep1) {
System.out.println("While Loop Sleep1 is Called");
Thread.sleep(1000);
sleepingTime +=1000;
}
total += count;
System.out.println("Total Value IS:: "+total);
System.out.println("Count Value Is:: "+count);
final int progress = (int) ((total * 100) / lenghtOfFile);
System.out.println("Progress Value IS::" + progress);
final long speed = total;
//duration = ((System.currentTimeMillis() - started)-sleepingTime) / 1000; --- When using this line then it's not updating Avarage
boolean result = ObjFtpCon.storeFile("/test.txt", input1);
//boolean result = ObjFtpCon.storeFile(map.get("file_address").toString()+"/test.txt", input1); --- When using this line then it's not updating Status
duration = ((System.currentTimeMillis() - started)-sleepingTime) / 1000;
runOnUiThread(new Runnable() {
public void run() {
//bar.setProgress(progress);
// trans.setText("" + progress);
//duration = ((System.currentTimeMillis() - started)-sleepingTime) / 1000;
//duration = ((System.currentTimeMillis() - started)-sleepingTime) / 1000;
//real_time.setText(duration + " secs");
if (duration != 0) {
test_avg.setText((((speed / duration)*1000)*0.0078125) + " kbps");
bar.setProgress(progress);
/*if (pk <= (speed / duration) / 1024) {
pk = (speed / duration) / 1024;
}*/
if (pk <= ((speed / duration)*1000)*0.0078125) {
pk = (long)(((speed / duration)*1000)*0.0078125);
}
//peak.setText(pk + " kbps");
}
}
});
//in.close();
if (result) {
updateUI(status, "Uploaded");
// toast("Uploading succeeded");
// toast("Uploaded at /test.txt");
//duration = ((System.currentTimeMillis() - started)-sleepingTime) / 1000;
System.out.println("curreent time..... "+System.currentTimeMillis());
System.out.println("started time --"+started);
System.out.println("sleep tome...."+sleepingTime);
System.out.println("duration is....."+duration);
/*runOnUiThread(new Runnable() {
public void run() {
bar.setProgress(progress);
// trans.setText("" + progress);
//duration = ((System.currentTimeMillis() - started)-sleepingTime) / 1000;
real_time.setText(duration + " secs");
if (duration != 0) {
test_avg.setText((speed / duration) / 1024
+ " kbps");
if (pk <= (speed / duration) / 1024) {
pk = (speed / duration) / 1024;
}
peak.setText(pk + " kbps");
}
}
});*/
}
/*while(!result){Thread.sleep(1000);}*/
}
byteArrayIS.close();
}
}
else{
System.out.println("password entered is incorrect");
//Toast.makeText(con, "Username or/and password is incorrect", Toast.LENGTH_SHORT).show();
}
}
catch (Exception e) {
e.printStackTrace();
// toast(e.getLocalizedMessage());
}
try {
ObjFtpCon.logout();
ObjFtpCon.disconnect();
}
catch (IOException e) {
e.printStackTrace();
// toast(e.getLocalizedMessage());
}
return null;
}