在asynctask类中使用edittext值

时间:2018-04-26 18:53:35

标签: android-asynctask android-edittext

我有一个简单的测试应用程序,在编辑文本中从用户获取2个字符串并将其发送到php文件,最后到mysql数据库 我hava 2编辑文本和一个按钮 在我的java文件中,我创建了一个asycnctask类namd sendpost

我想在asynctask类中使用2 edittext的值,但它说:
必须在ui

中使用gettext的值

请告诉我这个

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

    }
}

}

我试图传递这个虽然args但仍然无法正常工作 IT SAYS不能在Sendpost中使用(namee&amp; mailee)... 请告诉我什么是错的

非常感谢提前

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

1 个答案:

答案 0 :(得分:0)

我最终使用此代码并且工作正常

Traceback (most recent call last):
  File "netattack2.py", line 47, in <module>
    from scapy.all import *
  File "/usr/local/lib/python2.7/dist-packages/scapy/all.py", line 10, in <module>`dtytyty`
    from .config import *
  File "/usr/local/lib/python2.7/dist-packages/scapy/config.py", line 11, in <module>
    from .data import *
  File "/usr/local/lib/python2.7/dist-packages/scapy/data.py", line 184, in <module>
    TCP_SERVICES,UDP_SERVICES=load_services("/etc/services")
  File "/usr/local/lib/python2.7/dist-packages/scapy/data.py", line 109, in load_services
    f=open(filename, errors='ignore')
TypeError: 'errors' is an invalid keyword argument for this function

并使用此代码调用类

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