Search code examples
androidandroid-asynctaskftpandroid-edittextftp4j

obtaining the value of an EditText inside Asynctask


I am trying to make an app which uses FTP and changes the filename to a combination of 2 EditTexts. to properly upload it i am uploading it inside a 'asynctask' ,this is my code:

@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;
    }
}

My problem is as follows: i want to use the values of week_text.getText().toString(); and pagina_text.getText().toString(); in my Asynctask, but i cant find a way to achieve this. i also have zero clue on what to do with the parameters behind Asynchtask, i have looked it up multiple times but it just doesnt make sense when using it for a FTP upload.

Help please ._.


Solution

  • Just add the EditText` as parameter:

     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:
        }
    }
    

    And call it:

    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);