获取Asynctask中的EditText的值

时间:2015-09-08 18:00:17

标签: android android-asynctask ftp android-edittext ftp4j

我正在尝试创建一个使用FTP的应用程序,并将文件名更改为2个EditTexts的组合。要正确上传它,我将其上传到一个' 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;
    }
}

我的问题如下:我想在我的Asynctask中使用week_text.getText().toString();pagina_text.getText().toString();的值,但我无法找到实现此目的的方法。 我对于如何处理Asynchtask背后的参数也没有任何线索,我已经多次查找了它,但是当它用于FTP上传时它没有意义。

请帮助._。

2 个答案:

答案 0 :(得分:2)

只需传递String值即可执行如下方法

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

然后

@Override
protected String doInBackground(String... params) {
    String editText1Value = params[0];
    String editText2Value = params[1];
   ///then do what ever you want
}

答案 1 :(得分:1)

只需将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 week_text = (EditText) findViewById(R.id.week_edit);
EditText pagina_text = (EditText) findViewById(R.id.pagina_edit);
new upload_task().execute(week_text, paging_text);