0

I'm building a registering area on my App and everything is working fine.

I'm using PHP to insert, and Java to get the values, so, would like to pass the values from the EditTexts to the AsynkTask class and submit them to my Database.

How can i do that?

public class register extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.register);

        final EditText e_id=(EditText) findViewById(R.id.editText1);
        final EditText e_name=(EditText) findViewById(R.id.editText2);
        Button clickButton = (Button) findViewById(R.id.button1);
        clickButton.setOnClickListener( new OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        insert_user mTask = new insert_user();
                        mTask.execute("I WOULD LIKE TO PUT THE EDITTEXT VALUE HERE");

                        // TODO Auto-generated method stub
                    }
                });
    }


    class insert_user extends AsyncTask<String, Integer, String> {

        // Runs in UI before background thread is called
        @Override
        protected void onPreExecute() {
            super.onPreExecute();

            // Do something like display a progress bar
        }

        // This is run in a background thread
        @Override
        protected String doInBackground(String... params) {
            String passed = params[0];
            ArrayList<NameValuePair> nameValuePairs = new 

            ArrayList<NameValuePair>();


            nameValuePairs.add(new BasicNameValuePair("id","1"));
            nameValuePairs.add(new BasicNameValuePair("name",passed));

            InputStream is = null;
            try
            {
                 HttpClient httpclient = new DefaultHttpClient();
                 HttpPost httppost = new HttpPost("http://10.0.2.2/example/insert.php");
                 httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
                 HttpResponse response = httpclient.execute(httppost); 
                 HttpEntity entity = response.getEntity();
                 is = entity.getContent();
                 Log.e("pass 1", "connection success ");
             }
             catch(Exception e)
             {
                 Log.e("Fail 1", e.toString());

             }     

             String result = null;
            try
            {
                 BufferedReader reader = new BufferedReader
            (new InputStreamReader(is,"iso-8859-1"),8);
                 StringBuilder sb = new StringBuilder();
                 String line;
                while ((line = reader.readLine()) != null)
                 {
                     sb.append(line + "\n");
                 }
                 is.close();
                 result = sb.toString();
                 Log.e("pass 2", "connection success ");
             }
             catch(Exception e)
             {
                  Log.e("Fail 2", e.toString());
             }     

             try
             {
                  JSONObject json_data = new JSONObject(result);
                  String id = json_data.getString("id");
                  String name =json_data.getString("name");
             }
             catch(Exception e)
             {
                 Log.e("Fail 3", e.toString());
             }
            return result;

    }
        // This runs in UI when background thread finishes
        @Override
        protected void onPostExecute(String result) {
            super.onPostExecute(result);

            // Do things like hide the progress bar or change a TextView

        }
    }
}

3 Answers3

0

You can override the constructor. Something like:

class insert_user extends AsyncTask<String, Integer, String> {

    String mText;

    public insert_user(String text) {
        super();
        mText = text;       // save the value
    }

    protected String doInBackground(String... params) {
        // do something with mText
        ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();


        nameValuePairs.add(new BasicNameValuePair("id","1"));
        nameValuePairs.add(new BasicNameValuePair("name",mText));

. . .

    }

.
.
.
}

then instantiate and run with

 insert_user mTask = new insert_user("EditText Value");
 mTask.execute();

To read the e_name value:

insert_user mTask = new insert_user(e_name.getText().toString());
mTask.execute();

This will set the value for the lifecycle of the AsyncTask, including pre-execute.

If you only need the String value in doInBackground, then you can pass an array of arguments directly, great article at Android: How can I pass parameters to AsyncTask's onPreExecute()?

Community
  • 1
  • 1
CSmith
  • 13,318
  • 3
  • 39
  • 42
0

The way you are passing edittext information seems correct. Are you getting crash or something? Could you post logcat information?

Samir
  • 3,139
  • 2
  • 17
  • 24
  • I created a variable outside the onclick: final String getvalue = e_name.getText().toString(); and i called it mTask.execute(getvalue); but i received this error: 04-10 17:46:01.736: E/AndroidRuntime(4556): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.turanja/com.example.turanja.register}: java.lang.NullPointerException – Antonio Jeremias Apr 10 '15 at 17:47
  • Shouldn't you be extracting text from edittext withing onclick? And please post the whole logcat including all of that crash messages. – Samir Apr 10 '15 at 17:59
0

You can simply have these two lines of code directly within your doInBackGround() method and extract the content of the EditText from these fields.

final EditText e_id=(EditText) findViewById(R.id.editText1);
final EditText e_name=(EditText) findViewById(R.id.editText2);

// Extracting content
String eid = e_id.getText().toString();
String ename = e_name.getText().toString();

This is the simplest solution. The other option is to pass them through the AsyncTask constructor.

ucsunil
  • 7,378
  • 1
  • 27
  • 32