2

I am new to android, How can i insert values from android to php mysql i tried the following code it does not showing any error but not working. can anyone help me. Can anyone correct my mistake.

public class MainActivity extends Activity{

    EditText username, email, password;
    Button btn;
    HttpResponse response;

    public void onCreate(Bundle savedInstanceState){

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        username = (EditText) findViewById(R.id.usr_name);
        email = (EditText) findViewById(R.id.email);
        password = (EditText) findViewById(R.id.password);

        btn = (Button) findViewById(R.id.regbtn);
        btn.setOnClickListener(new View.OnClickListener(){


            public void onClick(View v){

                HttpClient httpclient = new DefaultHttpClient();
                HttpPost httppost = new HttpPost("http://10.0.2.2/androidtest.php");

                try{

                    ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
                    nameValuePairs.add(new BasicNameValuePair("uname", username.toString()));
                    nameValuePairs.add(new BasicNameValuePair("email", email.getText().toString()));
                    nameValuePairs.add(new BasicNameValuePair("password", password.getText().toString()));

                    httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
                    response = httpclient.execute(httppost);

                }catch(Exception e){
                    e.printStackTrace();
                }
            }
        });
    }

}
chiyango
  • 401
  • 10
  • 20

2 Answers2

0

I tested your example on my local VM and for me there was "06-01 00:22:17.671: W/System.err(2024): android.os.NetworkOnMainThreadException" as I commented before.

Dont do networking on the main thread.

An (other) example on how to use async tasks is postet here.

Async Tasks are executed in parallel to the main thread, so they are NOT blocking the main thread. Blocking the main thread is bad because it for example prevents the user interface from beeing redrawn / the interface cant handle input.
Therfore android throws an NetworkOnMainThreadException to prevent you from doing networking (a time consuming tasks) on the main thread.

An example implementation can be found here.

Community
  • 1
  • 1
Langusten Gustel
  • 10,917
  • 9
  • 46
  • 59
  • Your example working great but I am facing a issue. After success message in logcat i check in database i found **android.widget.EditText@43e55928** is inserted in the column instead of what i have given as input. – chiyango Jun 03 '13 at 18:12
  • Hey Devan. I guess only the "uname". Replace **nameValuePairs.add(new BasicNameValuePair("uname", username.toString()));** with **nameValuePairs.add(new BasicNameValuePair("uname", username.getText().toString()));**. Should work now :) (Sorry, cant update the gist :( ) – Langusten Gustel Jun 03 '13 at 18:39
0

Its probably not working because you are trying to send your message to php on the mainUI thread. Try putting your code into an asynctask like this

class Task extends AsyncTask<Void, Void, Void>{

    @Override
    protected void doInBackground(Void... params){


    HttpClient httpclient = new DefaultHttpClient();
            HttpPost httppost = new HttpPost("http://10.0.2.2/androidtest.php");

            try{

                ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
                nameValuePairs.add(new BasicNameValuePair("uname", username.toString()));
                nameValuePairs.add(new BasicNameValuePair("email", email.getText().toString()));
                nameValuePairs.add(new BasicNameValuePair("password", password.getText().toString()));

                httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
                response = httpclient.execute(httppost);

            }catch(Exception e){
                e.printStackTrace();
            }
    }
}

And then call it like this

new Task().execute();
James andresakis
  • 5,335
  • 9
  • 53
  • 88
  • Hi, sorry for late reply i glued with other work. I am not from java background I am newbie to java so can you post whole code where i have to place the above class you provided. – chiyango Jun 01 '13 at 20:49
  • You can just litterally copy and paste the class I provided you into the Activity based class you have and then in one of your button onclicks you would put new Task().execute(); It cant get any simpler than that ;) – James andresakis Jun 03 '13 at 17:29
  • After I click button android.widget.EditText@43e55928 inserted instead of what i have given as input. – chiyango Jun 03 '13 at 18:29
  • Im sorry but I dont understand. If you have more bugs or a crash please update your question with a logcat dump and Ill try to help. – James andresakis Jun 03 '13 at 18:33