0

FTP を使用し、ファイル名を 2 つの EditText の組み合わせに変更するアプリを作成しようとしています。適切にアップロードするには、「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 アップロードに使用する場合は意味がありません。

助けてください 。_。

4

2 に答える 2

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);
于 2015-09-08T18:11:24.857 に答える