我正在嘗試製作一個使用FTP並將文件名更改爲2個EditTexts組合的應用程序。正確上傳我正在上傳了「的AsyncTask」裏面,這是我的代碼:獲取Asynctask中的EditText的值
@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;
}
}
我的問題是:我想使用的week_text.getText().toString();
和pagina_text.getText().toString();
值在我的AsyncTask,但我不能找到一種方法來實現這一點。 我對於Asynchtask背後的參數該如何處理也沒有任何線索,我已經多次查找過它,但是當它用於FTP上傳時它沒有任何意義。
請幫助._。
這段代碼不會工作,因爲你不能在'doInBackground()中顯示'吐司' – Lal
@Lal 2個敬酒只是爲了看我的代碼是否正確執行,我將它們刪除:) – MalumAtire832
不會顯示任何「吐司」。它只是崩潰你的應用程序。 – Lal