0

I have 2 activities. First activity has and form which user can insert some information. Then there is a button which lead to second activity where user insert some more information and buton Save which save in MySQL database. I use intent.putExtra but doesn't save anything in DB. Since I'm new to android I'm not sure that this is correct. I've used as reference this post How to get extra data from intent in android? This is first activity

public class Reservation extends Activity {
    static String Name;
static String Email;
static String Phone;
static String Comment;
static String DateTime;
static String numberOfPeople;

private EditText editText1, editText3, editText2, editText4, txtDate, editText5; 
private Button btnMenues, btnCalendar, btnTimePicker;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.reservation);

    editText1 = (EditText) findViewById(R.id.personName);
    editText3 = (EditText) findViewById(R.id.personPhone);
    editText2 = (EditText) findViewById(R.id.personEmail);
    editText4 = (EditText) findViewById(R.id.personComment);
    txtDate = (EditText) findViewById(R.id.datePicker);
    editText5 = (EditText) findViewById(R.id.editText5);

    btnCalendar = (Button) findViewById(R.id.btnCalendar);
    btnMenues = (Button) findViewById(R.id.continueWithReservation);


    btnMenues.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            //Name = editText1.getText().toString();
            //Phone = editText3.getText().toString();
            //Email = editText2.getText().toString();
            //Comment = editText4.getText().toString();
            //DateTime = txtDate.getText().toString();
            //DateTime = txtTime.getText().toString();
            //numberOfPeople = numberOfPeoples.getText().toString();
            //new SummaryAsyncTask().execute((Void) null);

            Intent intent = new Intent(Reservation.this, ReservationSecond.class);
            String Name = editText1.getText().toString();
            String Email = editText2.getText().toString();
            String Phone = editText3.getText().toString();
            String Comment = editText4.getText().toString();
            String DateTime = txtDate.getText().toString();
            String numberOfPeople = editText5.getText().toString();

            intent.putExtra(Name, Name.getBytes().toString());
            intent.putExtra(Email, Email.getBytes().toString());
            intent.putExtra(Phone, Phone.getBytes().toString());
            intent.putExtra(Comment, Comment.getBytes().toString());
            intent.putExtra(DateTime, DateTime.getBytes().toString());
            intent.putExtra(numberOfPeople, numberOfPeople.getBytes().toString());
            startActivity(intent);
        }
    }); 
}
}

and this is second activity which has to receive the data and save in BD

public class ReservationSecond extends Activity {

String getName;
String getEmail;
String getPhone;
String getComment;
String getDateTime;
String getnumberOfPeople;

private Button btnMenues;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.reservation_second);

    btnMenues = (Button) findViewById(R.id.finish);

    btnMenues.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            Bundle extras = getIntent().getExtras();
            if (extras != null) {
                getName = extras.getString("Name");
                getEmail = extras.getString("Email");
                getPhone = extras.getString("Phone");
                getComment = extras.getString("Comment");
                getDateTime = extras.getString("DateTime");
                getnumberOfPeople = extras.getString("numberOfPeople");
        }

            new SummaryAsyncTask().execute((Void) null);

            startActivity(getIntent());
        }
    }); 
}

class SummaryAsyncTask extends AsyncTask<Void, Void, Boolean> {

    private void postData(String getNameToData, String getEmailData, String getPhoneData,
            String getCommentData, String getDateTimeData, String getnumberOfPeopleData) {

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

        try {
            ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(4);
            nameValuePairs.add(new BasicNameValuePair("Name", getNameToData));
            nameValuePairs.add(new BasicNameValuePair("Email", getEmailData));
            nameValuePairs.add(new BasicNameValuePair("Phone", getPhoneData));
            nameValuePairs.add(new BasicNameValuePair("Comment", getCommentData));
            nameValuePairs.add(new BasicNameValuePair("Date", getDateTimeData));
            nameValuePairs.add(new BasicNameValuePair("numberOfPeople", getnumberOfPeopleData));
            httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
            HttpResponse response = httpclient.execute(httppost);
        }
        catch(Exception e)
        {
            Log.e("log_tag", "Error:  "+e.toString());
        }
    }

    @Override
    protected Boolean doInBackground(Void... params) {
        postData(getName, getEmail, getPhone, getComment, getDateTime, getnumberOfPeople );
        return null;
    }

}
}

Update. In second activity like this?

if (extras != null) {
                extras.getString("Name");
                extras.getString("Email");
                extras.getString("Phone");
                extras.getString("Comment");
                extras.getString("DateTime");
                extras.getString("numberOfPeople");
            }

instead of

                if (extras != null) {
                String Name = extras.getString("Name");
                String Email = extras.getString("Email");
                String Phone = extras.getString("Phone");
                String Comment = extras.getString("Comment");
                String DateTime = extras.getString("DateTime");
                String numberOfPeople = extras.getString("numberOfPeople");
            }

This is the getReservation.php

$icon = mysql_connect("localhost","user","password");
if(!$icon)
{
   die('Could not connect : ' . mysql_erroe());
}
mysql_select_db("database", $icon)or die("database selection error");
echo json_encode($data);

$Name=$_POST['Name'];
$Phone=$_POST['Phone'];
$Email=$_POST['Email'];
$Comment=$_POST['Comment'];
$DateTime=$_POST['DateTime'];
$numberOfPeople=$_POST['numberOfPeople'];

 mysql_query("INSERT INTO reservation (Name, Phone, Email, Comment, DateTime, numberOfPeople)
VALUES ('$Name', '$Phone', '$Email', '$Comment', NOW(), '$numberOfPeople')",$icon);

mysql_close($icon);

Community
  • 1
  • 1
Goro
  • 499
  • 1
  • 13
  • 31

3 Answers3

1

See the documentation for how putExtra() works and what you have to give it as input enter image description here

In order to get your extra's like this

extras.getString("Name");

you have to put them like this

intent.putExtra("Name", Name.getBytes().toString());

note the extra quotation marks around Name

Also, because you are passing strings, you can just do

intent.putExtra("Name", Name);

the -> bytes -> String conversion is redundant

Tim
  • 41,901
  • 18
  • 127
  • 145
  • Thank's. I've tried like this but still doesn't save anything in DB when I press `finish` button in second activity. – Goro Nov 04 '14 at 08:02
  • What is the output? any errors? There are a lot of possible causes for the DB write to not work, we can not always guess it by looking at the code – Tim Nov 04 '14 at 08:05
  • Nothing. Not errors, not warnings.It just reload the activity as must do. Where I can check for the output? – Goro Nov 04 '14 at 08:05
  • On your webserver maybe? We have no way to know what happens there – Tim Nov 04 '14 at 08:06
  • Sorry but no errors also on host. Before to put second activity it saves normally in database. But after I made second activity doesn't want. – Goro Nov 04 '14 at 08:16
1

You are handing over the value as both key and value. That cannot work since a changing value would also change the key and prevent you from getting your information in the second Activity.

Try this:

intent.putExtra("Name", Name.getBytes().toString());
intent.putExtra("Email", Email.getBytes().toString());
intent.putExtra("Phone", Phone.getBytes().toString());
intent.putExtra("Comment", Comment.getBytes().toString());
intent.putExtra("DateTime", DateTime.getBytes().toString());
intent.putExtra("numberOfPeople", numberOfPeople.getBytes().toString());
Philipp Jahoda
  • 50,880
  • 24
  • 180
  • 187
1

You are passing the value entered by user as the key in extras. So there won't be any value if you try to get that by "Name" key.

Use

    intent.putExtra("Name", Name.getBytes().toString());

instead of

    intent.putExtra(Name, Name.getBytes().toString());

Also in second activity use global variables you declared instead of creating local ones. As you are passing global variables and initializing the local ones.

Use

    if (extras != null) {
            Name = extras.getString("Name");
            Email = extras.getString("Email");
            Phone = extras.getString("Phone");
            Comment = extras.getString("Comment");
            DateTime = extras.getString("DateTime");
            numberOfPeople = extras.getString("numberOfPeople");
    }
RaviVadera
  • 401
  • 2
  • 13
  • I think it does not matter. And might be that are added to convert string to other encoding. It answers the question, doesn't it? – RaviVadera Nov 04 '14 at 08:00
  • Thank's but still doesn't save anything in DB when I press finish button in second activity. – Goro Nov 04 '14 at 08:05
  • You are using local variables in second activity's onclick method, you should use the variables declared in class instead of redeclaring it and it should work. – RaviVadera Nov 04 '14 at 08:07
  • Tried your edit also this what I write in my edit. Nothing in DB. I will post the `.php` script also – Goro Nov 04 '14 at 08:11
  • No you haven't added the assignment in your edit. Assign the global variables you have declared. See the difference. I have used Name = extras.getString("Name"); and you have used only extras.getString("Name"); – RaviVadera Nov 04 '14 at 08:15
  • Sorry I've added it on my side but forgot to edit it here. Now I edited it. – Goro Nov 04 '14 at 08:18
  • I saw something strange. On second activity I must click 2 times on `Finish` button. Then save in DB but empty rows i.e. doesn't take the info from first activity or is loosed after first click on `Finish` – Goro Nov 04 '14 at 08:22
  • I think the problem here is you are using same names for local and global variables, same problem with asynctask when you call the postData method. Name global variables different, assign them from extras, use it when calling the postData. And in declaration of postData use different names than global variables. Clean up your code, it is messed up. Also use camel case for variable names. Also log the value of variables and try to debug. Check whether the values are available at each point. – RaviVadera Nov 04 '14 at 08:27
  • I've edited my second activity in question. Change them like this? – Goro Nov 04 '14 at 08:32
  • use different names in private void postData(String getName, String getEmail, String getPhone, String getComment, String getDateTime, String getnumberOfPeople) and use the new names while adding BasicNameValuePairs – RaviVadera Nov 04 '14 at 08:35
  • Done this (updated in question) and still nothing goes to DB. – Goro Nov 04 '14 at 08:39
  • How can I check them? – Goro Nov 04 '14 at 08:56
  • can we continue here http://chat.stackoverflow.com/rooms/64219/discussion-between-ravivadera-and-goro – Goro Nov 04 '14 at 09:02