Android 获取Asynctask中EditText的值

Android 获取Asynctask中EditText的值,android,android-asynctask,ftp,android-edittext,ftp4j,Android,Android Asynctask,Ftp,Android Edittext,Ftp4j,我正在尝试制作一个应用程序,它使用FTP并将文件名更改为两个编辑文本的组合。为了正确地上传它,我正在“asynctask”中上传它,这是我的代码: @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_upload); EditTe

我正在尝试制作一个应用程序,它使用FTP并将文件名更改为两个编辑文本的组合。为了正确地上传它,我正在“asynctask”中上传它,这是我的代码:

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_upload);

        EditText week_text = (EditText) findViewById(R.id.week_edit);
        EditText pagina_text = (EditText) findViewById(R.id.pagina_edit);
        String week = "w" + week_text.getText().toString() + "_";
        String pagina = "p" + pagina_text.getText().toString() + ".jpg";

        Button foto_keuze = (Button)findViewById(R.id.foto_keuze_button);
        Button upload_button = (Button)findViewById(R.id.upload_button);
        Typeface Impact = Typeface.createFromAsset(getAssets(), "fonts/Impact.ttf");
        foto_keuze.setTypeface(Impact);
        upload_button.setTypeface(Impact);

        targetImage = (ImageView)findViewById(R.id.imageView);

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

    }

public void upload_klik (View view) {
    EditText week_text = (EditText) findViewById(R.id.week_edit);
    EditText pagina_text = (EditText) findViewById(R.id.pagina_edit);
    upload_task.execute(week_text, pagina_text);
}

protected class upload_task extends AsyncTask<EditText, Object, String> {

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

        EditText w = params[0];
        EditText p = params[1];

        Intent intent = getIntent();
        Bundle bundle = intent.getExtras();
        String ret = "Done!";
        if(!bundle.isEmpty()) {
            String afdeling_url = bundle.getString("afdeling_url", "DKW/");
            String afdeling_preFix = bundle.getString("afdeling_preFix", "dkw");
            String locatie_url = bundle.getString("locatie_url", "delf_wend");

            String new_fileName = afdeling_preFix + w + p;

            File f = new File(foto_path);
            File sdcard = Environment.getExternalStorageDirectory();
            File to = new File(sdcard, new_fileName);
            f.renameTo(to);


            if(f == null){
                Toast.makeText(upload.this, "Geen foto geselecteerd", Toast.LENGTH_SHORT).show();
            }

            if(f != null) {

                try{
                    Toast.makeText(getApplicationContext(), afdeling_url + afdeling_preFix, Toast.LENGTH_SHORT).show();
                    client.setPassive(true);
                    client.setAutoNoopTimeout(30000);
                    client.connect(FTP_HOST, 21);
                    client.login(FTP_USER, FTP_PASS);
                    client.setType(FTPClient.TYPE_BINARY);
                    client.changeDirectory(locatie_url + afdeling_url);
                    client.upload(to, new FTP_LISTENER());

                    restart();

                }
                catch (Exception e){
                    e.printStackTrace();
                    try {
                        client.disconnect(true);
                        Toast.makeText(getApplicationContext(), "Upload voltooid", Toast.LENGTH_SHORT);
                    }
                    catch (Exception e2) {
                        e2.printStackTrace();
                    }
                }
            }

        }
        return ret;
    }
}
@覆盖
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_上传);
EditText week\u text=(EditText)findViewById(R.id.week\u edit);
EditText pagina_text=(EditText)findviewbyd(R.id.pagina_edit);
String week=“w”+week\u text.getText().toString()+“\u”;
字符串pagina=“p”+pagina_text.getText().toString()+”.jpg”;
按钮foto_keuze=(按钮)findViewById(R.id.foto_keuze_按钮);
按钮上传按钮=(按钮)findViewById(R.id.upload按钮);
Typeface Impact=Typeface.createFromAsset(getAssets(),“font/Impact.ttf”);
foto_keuze.字体设置(冲击);
上传按钮。设置字体(影响);
targetImage=(ImageView)findViewById(R.id.ImageView);
StrictMode.ThreadPolicy policy=新建StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(策略);
}
公共无效上载(视图){
EditText week\u text=(EditText)findViewById(R.id.week\u edit);
EditText pagina_text=(EditText)findviewbyd(R.id.pagina_edit);
上传任务。执行(星期文本、页码文本);
}
受保护类上载任务扩展异步任务{
@凌驾
受保护的字符串doInBackground(EditText…参数){
EditText w=参数[0];
EditText p=参数[1];
Intent=getIntent();
Bundle=intent.getExtras();
String ret=“完成!”;
如果(!bundle.isEmpty()){
String afdeling_url=bundle.getString(“afdeling_url”,“DKW/”);
String afdeling_preFix=bundle.getString(“afdeling_preFix”,“dkw”);
String locatie_url=bundle.getString(“locatie_url”、“delf_wend”);
字符串new_fileName=afdeling_前缀+w+p;
文件f=新文件(foto_路径);
文件sdcard=Environment.getExternalStorageDirectory();
文件到=新文件(SD卡,新文件名);
f、 抵运(至);
如果(f==null){
Toast.makeText(upload.this,“Geen foto geselecteerd”,Toast.LENGTH_SHORT.show();
}
如果(f!=null){
试一试{
Toast.makeText(getApplicationContext(),afdeling_url+afdeling_前缀,Toast.LENGTH_SHORT).show();
client.setPassive(true);
客户端设置自动nooptimeout(30000);
client.connect(FTP_主机,21);
客户端登录(FTP用户,FTP通行证);
setType(FTPClient.TYPE_二进制文件);
client.changeditory(locatie\u url+afdeling\u url);
client.upload(到新的FTP_侦听器());
重启();
}
捕获(例外e){
e、 printStackTrace();
试一试{
client.disconnect(true);
Toast.makeText(getApplicationContext(),“Upload voltooid”,Toast.LENGTH\u SHORT);
}
捕获(异常e2){
e2.printStackTrace();
}
}
}
}
返回ret;
}
}
我的问题如下:我想使用
week_text.getText().toString()的值
pagina_text.getText().toString()在我的异步任务中,但我找不到实现这一点的方法。
我对Asynchtask背后的参数做了什么也没有任何线索,我已经查了很多次了,但是当使用它进行FTP上传时,它就没有意义了


请提供帮助。.

只需添加EditText`作为参数:

 protected class upload_task extends AsyncTask<EditText, Object, String> {

    @Override
    protected String doInBackground(EditText... params) {
        EditText editText1 = params[0];
        EditText editText2 = params[1];
       ///rest of code:
    }
}

只需添加EditText`作为参数:

 protected class upload_task extends AsyncTask<EditText, Object, String> {

    @Override
    protected String doInBackground(EditText... params) {
        EditText editText1 = params[0];
        EditText editText2 = params[1];
       ///rest of code:
    }
}

只需添加EditText`作为参数:

 protected class upload_task extends AsyncTask<EditText, Object, String> {

    @Override
    protected String doInBackground(EditText... params) {
        EditText editText1 = params[0];
        EditText editText2 = params[1];
       ///rest of code:
    }
}

只需添加EditText`作为参数:

 protected class upload_task extends AsyncTask<EditText, Object, String> {

    @Override
    protected String doInBackground(EditText... params) {
        EditText editText1 = params[0];
        EditText editText2 = params[1];
       ///rest of code:
    }
}

只需将字符串值传递给执行方法,如下所示

new upload_task().execute(edtText1.getText.toString,edtText2.getText.toString);
然后


只需将字符串值传递给执行方法,如下所示

new upload_task().execute(edtText1.getText.toString,edtText2.getText.toString);
然后


只需将字符串值传递给执行方法,如下所示

new upload_task().execute(edtText1.getText.toString,edtText2.getText.toString);
然后


只需将字符串值传递给执行方法,如下所示

new upload_task().execute(edtText1.getText.toString,edtText2.getText.toString);
然后



此代码将不起作用,因为您无法在
doInBackground()
@Lal中显示
Toast
。这两个Toast只供我查看代码是否正确执行,我将删除它们:)不,它不会显示任何
Toast
。这会使你的应用程序崩溃。@Lal我已经更新了代码,因为我之前没有使用Asynctask来上传它,我只是使用了onClick。你应该将edittext值作为参数传递给Asynctask。这段代码不起作用,因为你不能在
doInBackground()中显示
Toast
@Lal这两个祝酒词只是让我看看代码是否正确执行,我将删除它们:)不,它不会显示任何
祝酒词。这会使你的应用程序崩溃。@Lal我已经更新了代码,因为我之前没有使用Asynctask来上传它,我只是使用了onClick。你应该将edittext值作为参数传递给Asynctask。这段代码不起作用,因为你不能在
doInBackground()中显示
Toast
@Lal这两个祝酒词只是让我看看代码是否正确执行,我将删除它们:)不,它不会显示任何
祝酒词。这会使你的应用程序崩溃。@Lal我已经更新了代码,因为我之前没有使用Asynctask来上传它,我只是使用了onClick。你应该将edittext值作为参数传递给Asynctask。这段代码不起作用,因为你不能在
doInBackground()中显示
Toast
@Lal这两个祝酒词只是让我看看代码是否正确执行,我将删除它们:)不,它不会显示任何
祝酒词。它只是使你的应用程序崩溃。@Lal我已经更新了代码,因为