0

I am a new android developer . I am building an app , in which a registration form is filled and its contents data is to be stored on the local host of mine . I have made the form but i don't know how to code for sending the data to local host. help me code from the basic so that WHEN A SEND BUTTON IS CLICKED , THE DATA FILLED IN THE FORM GETS STORED ON THE LOCAL HOST.

this is my code..................................

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main_activity_server);
    send1 = (Button)findViewById(R.id.Send);

    send1.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {


            // TODO Auto-generated method stub
            testInput ti=new testInput();
                //Create the intent



               ti.postData("Sent Data");



            TextView num1View = (TextView) findViewById(R.id.T5); 
            num1View.setText("your data is stored");

            //Create the intent





        }
    });

}   
public void postData(String toPost) {
    // Create a new HttpClient and Post Header
    HttpClient httpclient = new DefaultHttpClient();
    HttpPost httppost = new HttpPost("my local host name");

    //This is the data to send
    String name = toPost;
    String number= toPost;
    String email =toPost;
    String suggestion= toPost;//any data to send

    try {
    // Add your data
    List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);
    nameValuePairs.add(new BasicNameValuePair("action", name));
    nameValuePairs.add(new BasicNameValuePair("action", number));
    nameValuePairs.add(new BasicNameValuePair("action", email));
    nameValuePairs.add(new BasicNameValuePair("action", suggestion));



    httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

    // Execute HTTP Post Request

    ResponseHandler<String> responseHandler = new BasicResponseHandler();
    String response = httpclient.execute(httppost, responseHandler);

    //This is the response from a php application
    String reverseString = response;
    Toast.makeText(this, "response" + reverseString, Toast.LENGTH_LONG).show();

    } catch (ClientProtocolException e) {
    Toast.makeText(this, "CPE response " + e.toString(), Toast.LENGTH_LONG).show();
    // TODO Auto-generated catch block
    } catch (IOException e) {
    Toast.makeText(this, "IOE response " + e.toString(), Toast.LENGTH_LONG).show();
    // TODO Auto-generated catch block
    }

    }//end postData()
krips
  • 1
  • 5

2 Answers2

0
  1. You will need to get the values of your fields. Assuming they are EditText, write something like yourEditText.getText().toString() to parse it as Strings and get the fields you will send.
  2. To send them, you may want to read Android Volley Tutorial - Making HTTP GET, POST, PUT, DELETE Requests
0

Follow this few steps

1) collect data into variable or in Array

2) make connection with database

3) get your database in writeable mode

4) fire insert query and close your database

Ando Masahashi
  • 3,112
  • 2
  • 24
  • 41
  • i have collected data into an array . but i dont know how to make connections with the database. can you help me in that. – krips Dec 07 '13 at 17:56
  • i have put up my code . its showing error in testInput ti=new testInput(); ti.postData("Sent Data"); – krips Dec 07 '13 at 18:07
  • i have taken this code from http://stackoverflow.com/questions/16315415/how-send-data-to-website-by-using-android-app – krips Dec 07 '13 at 18:08
  • [follow this link](http://www.androidhive.info/2012/05/how-to-connect-android-with-php-mysql/) will help you to connect with mysql database – Ando Masahashi Dec 07 '13 at 18:12