无法通过asynctask android上传文件

时间:2013-11-20 12:03:57

标签: android

嘿伙计们我遇到了一个问题,我想用ftp上传一个文件,我也上传了通过但它显示的文件大小为0kb,文件的实际大小为1 mb如果我调用上传方法我点击它的工作,但它不适用于异步任务帮助...

Button btn;
Runnable mUpdateUITimerTask;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);




    btn = (Button) findViewById(R.id.button1);
    btn.setOnClickListener(this);

}

public void onClick(View v) {


    (new UpDateOperation()).execute("");

}

public void uploadFile(File fileName) {

    FTPClient client = new FTPClient();
    StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
            .permitAll().build();
    StrictMode.setThreadPolicy(policy);

    try {

        client.connect(FTP_HOST, 21);
        client.login(FTP_USER, FTP_PASS);

        if (client.isAuthenticated() == true) {

            Log.d("hello", "world" + fileName.getName());
            client.setType(FTPClient.TYPE_BINARY);

            client.upload(fileName);

        }

    } catch (Exception e) {
        e.printStackTrace();
        try {
            client.disconnect(true);
            Log.d("chal", "ja");
        } catch (Exception e2) {
            e2.printStackTrace();
        }
    }

}

public class UpDateOperation extends AsyncTask<String, Void, String> {

    @Override
    protected String doInBackground(String... params) {



        uploadFile(null);
        return "hello";

    }

}

}

1 个答案:

答案 0 :(得分:1)

你应该将实际文件作为第一个参数传递给AsyncTask(比如,或者只是在uploadFile方法中创建它并传递带有文件名的String