Search code examples
android-asynctaskandroid-edittext

Using edittext values in asynctask class


I have a simple test app that takes 2 strings from user in edit text and seds it to php file and finally to MySQL database I have 2 edit text and one button in my Java file I created a asycnctask class namd sendpost

I want to use values of 2 edittext inside an asynctask class but it says:

values of gettext must be used inside ui

Button btn;
EditText name;
EditText email;

    @Override
   protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main2);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    btn =(Button) findViewById(R.id.btn);

    btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            new SendPost().execute();
            Intent i = new Intent(getApplicationContext(),MainActivity.class);
            startActivity(i);

        }

class SendPost extends AsyncTask<String, String, String> {
    @Override
    protected String doInBackground(String... params) {

        String NameHolder = name.getText().toString() ;
        String EmailHolder = email.getText().toString() ;
        String Address = "http://nardooon.ir/post.php";
        HashMap hashmap = new HashMap();
        hashmap.put("name", NameHolder);
        hashmap.put("email", EmailHolder);
        return Utils.sendData(Address,hashmap);
    }

    @Override
    protected void onPostExecute(String result) {

        super.onPostExecute(result);

        Toast.makeText(Main2Activity.this, "Data Submit Successfully", Toast.LENGTH_LONG).show();

    }
}
}

I tried to pass this though args but still not working
IT SAYS can not applie (namee & mailee ) in Sendpost ...
What's wrong?

 btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            
            String namee = name.getText().toString();
            String mailee = email.getText().toString();
            new SendPost(namee,mailee).execute();
            Intent i = new Intent(getApplicationContext(),MainActivity.class);
            startActivity(i);

        }
    });



    setSupportActionBar(toolbar);

    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                    .setAction("Action", null).show();
        }
    });
}



class SendPost extends AsyncTask<String, String, String> {
    

    @Override
    protected String doInBackground(String... args) {


        String NameHolder = args[0] ;
        String EmailHolder = args[1] ;
        String Address = "http://nardooon.ir/post.php";
        HashMap hashmap = new HashMap();
        hashmap.put("name", NameHolder);
        hashmap.put("email", EmailHolder);
        return Utils.sendData(Address,hashmap);
    }

Solution

  • I end up using this code:

     private class DownloadFile extends AsyncTask<String, Integer, String> {
    
        private final String url;
    
        DownloadFile(final String url) {
            this.url = url;
        }
    
        @Override
        protected String doInBackground(String... args) {
    
        String NameHolder = this.url ;
         String EmailHolder = args[0]  ;
    

    and use this code to call the class:

    new DownloadFile(txt.getText().toString()).execute(txt2.getText().toString());