0

I have created an android mobile app on Eclipse Juno and I am trying to enter information from my mobile app to my database which is on php my Admin. The database is connected through a localhost wamp server off bitnami.

When I enter the data on the app it is saying that it has been entered successfully, however when I go to the database there is no data being stored. I was wondering if anyone could point me in what I am doing wrong.

My code in my app is:

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;

import com.example.independentretailers.R;
import android.annotation.SuppressLint; 
import android.app.Activity; 
import android.os.Bundle; 
import android.os.StrictMode;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class customersignup extends Activity {

EditText etFirstName, etSurname, etPhoneNumber, etEmail, etPassword;
Button bSave;
@SuppressLint("NewApi")
@Override
protected void onCreate (Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    //setup strict mode policy 
    StrictMode.ThreadPolicy policy = new    StrictMode.ThreadPolicy.Builder().permitAll().build();
    StrictMode.setThreadPolicy(policy);
    setContentView(R.layout.customersignup);
    //for firstname
    etFirstName = (EditText) findViewById(R.id.editText1);
    //for surname 
    etSurname = (EditText) findViewById(R.id.editText2);
    //for phone number
    etPhoneNumber = (EditText) findViewById(R.id.editText3);
    //for email 
    etEmail = (EditText) findViewById(R.id.editText4); 
    //for password
    etPassword = (EditText) findViewById(R.id.editText5);
    //setting up ID for the button 
    bSave = (Button) findViewById(R.id.button1);
    //setting up onclick listener 
    bSave.setOnClickListener(new View.OnClickListener(){

        InputStream is = null;
        @Override 
        public void onClick(View arg0){
            //storing values inside edit texts inside strings 
            String FirstName = ""+etFirstName.getText().toString();
            String Surname = ""+etSurname.getText().toString(); 
            String PhoneNumber = ""+etPhoneNumber.getText().toString();
            String Email = ""+etEmail.getText().toString();
            String Password = ""+etPassword.getText().toString(); 

            //setting name pair values 
            List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);
            //adding string values inside the name value pairs 
            nameValuePairs.add(new BasicNameValuePair("First Name",   FirstName));
            nameValuePairs.add(new BasicNameValuePair("Surname", Surname));
            nameValuePairs.add(new BasicNameValuePair("Phone Number",   PhoneNumber));
            nameValuePairs.add(new BasicNameValuePair("Email", Email));
            nameValuePairs.add(new BasicNameValuePair("Password",    Password));

            //setting up the connection inside the try catch 

            try{ 
                //setting up the default client

            HttpClient httpClient = new DefaultHttpClient();

            HttpPost httpPost = new    HttpPost("http://10.0.2.2/tutorial.php");
            //passing the name value pair inside the httppost 

        httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

        HttpResponse response = httpClient.execute(httpPost);
        //setting up the entity 
        HttpEntity entity = response.getEntity();

        //define input stream reader 
        is = entity.getContent(); 

        //displaying message for data entered successfully 
        String msg = "Data entered successfully";
        Toast.makeText(getApplicationContext(), msg,     Toast.LENGTH_LONG).show();
            }
            catch (ClientProtocolException e)
            {
                Log.e("ClientProtocol", "Log_tag");
                e.printStackTrace(); 
            } catch (IOException e) 
            { 
                Log.e("Log_tag", "IOException");
                e.printStackTrace();
            }

        }

    });


}
}

My PHP text file is:

$con=mysql_connect('localhost', 'root', 'pancakes');
mysql_select_db("independentretailers",$con); 

$FirstName=$_POST['First Name']; 
$Surname=$_POST['Surname'];
$PhoneNumber=$_POST['Phone Number'];
$Email=$_POST['Email'];
$Password=$_POST['Password'];

 mysql_query("insert into customer(First Name, Surname, Phone Number, Email,   Password)    values('{$FirstName}','{$Surname}','{$PhoneNumber}','{$Email}','{$Password}')");

//mysql_close();
?> 
Tmcc
  • 1
  • 1
  • 5

1 Answers1

0

you are printing the response at all. you are just toasting the msg which you have created. Update: You are running network related operation on the ui thread. You will get NetworkOnMainThreadException post honeycomb.

Use a Thread or Asynctask.

Check the answer to this question.

Community
  • 1
  • 1
gopiariv
  • 454
  • 7
  • 9
  • I found this tutorial online for adding data to a database from an android application and they have done it the same way I had and their data had saved, is there something I have missed? the tutorial is : https://www.youtube.com/watch?v=MdyZKewSwFg – Tmcc Apr 13 '15 at 08:15
  • does your localhost has password? – gopiariv Apr 14 '15 at 12:28
  • it does yes, is that the problem? – Tmcc Apr 14 '15 at 16:48