2015-09-08 67 views
0

我正在嘗試製作一個使用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上傳時它沒有任何意義。

請幫助._。

+0

這段代碼不會工作,因爲你不能在'doInBackground()中顯示'吐司' – Lal

+0

@Lal 2個敬酒只是爲了看我的代碼是否正確執行,我將它們刪除:) – MalumAtire832

+0

不會顯示任何「吐司」。它只是崩潰你的應用程序。 – Lal

回答

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

這樣做,非常感謝你,我現在瞭解Asynctask的基礎知識。 我有一些關於圖像選擇的錯誤,不知道我是否可以修復它:) 非常感謝你 – MalumAtire832

+1

更好的傳遞字符串,因爲Naveed說。無論如何,你不想在'doInBackground'中操作視圖,他所需要的只是它們的值。 – nasch

1

只是傳遞字符串值,以類似下面

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 
}