1

I'm writing a App that gets input from a user (First Name, Last Name, Phone #, Time of Reservation, Reservation Date, and How many are in the party) then sends that data to a mySQL server. I'm having trouble though. I'm new to android development so I don't really know many tricks or ways to do things. I've researched this and all I have come up with is how to pull data from a database. I have my app pretty much built, I have it to where the user inputs their information and then clicks submit. When they click submit all the information they input disappeared, and nothing appears in the SQL database. Any suggestions that you guys could give, I'd appreciate it :) Thanks! Below is the code I used. I found a book that was using a mySQL database and worked a lot of my code around that.

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

     intent = new Intent();
     intent.setClass(this,Reservation.class); 


final Button Submit = (Button) findViewById(R.id.xButton);
Submit.setOnClickListener(new OnClickListener() {
    public void onClick(View v) {
        // Perform action on clicks
     fName = (EditText)findViewById(R.id.xFirstName);
     lName = (EditText)findViewById(R.id.xLastName);
     phone = (EditText)findViewById(R.id.xPhoneInputBox);
     date = (EditText)findViewById(R.id.xDateInput);
     time = (EditText)findViewById(R.id.xTimeInput);
     partyNum = (EditText)findViewById(R.id.xNumberInput);

     startActivity(intent);

    }           

});


}
}
package lets.Seat;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import android.app.ProgressDialog;
import android.os.Handler;
import android.os.Message;
import android.util.Log;

-------Reservation Class--------------

public class Reservation extends Lets_SeatActivity {
ProgressDialog myprogress;
Handler progresshandler;
Message msg;

 public void run() {

     try {
         FileOutputStream os = getApplication().openFileOutput(fName.getText().toString()+lName.getText().toString()+phone.getText().toString()+date.getText().toString()+time.getText().toString()+partyNum.getText().toString(), 0);

         os.flush();
         os.close();

         // reopen to so we can send this data to server
         File f = new File(getApplication().getFileStreamPath(fName.getText().toString()+lName.getText().toString()+phone.getText().toString()+date.getText().toString()+time.getText().toString()+partyNum.getText().toString()).toString());
         long flength = f.length();

         FileInputStream is = getApplication().openFileInput(fName.getText().toString()+lName.getText().toString()+phone.getText().toString()+date.getText().toString()+time.getText().toString()+partyNum.getText().toString());
         byte data[] = new byte[(int) flength];
         int count = is.read(data);
         if (count != (int) flength) {
             // bad read?
         }
         this.msg = new Message();
         msg.what = 0;
         msg.obj = ("Connecting to Server");
         progresshandler.sendMessage(this.msg);

         URL url = new URL("---Edited out--- If you want to see my PHP file, Just ask :)");
         URLConnection conn = url.openConnection();
         conn.setDoOutput(true);
         BufferedOutputStream wr = new BufferedOutputStream(conn.getOutputStream());
         wr.write(data);
         wr.flush();
         wr.close();

         this.msg = new Message();
         this.msg.what = 0;
         this.msg.obj = ("Data Sent");
         this.progresshandler.sendMessage(this.msg);

         // Get the response
         BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
         String line = "";
         Boolean bSuccess = false;
         while ((line = rd.readLine()) != null) {
             if (line.indexOf("SUCCESS") != -1) {
                 bSuccess = true;
             }
             // Process line...
             Log.v("Lets_SeatActivity", line);
         }
         wr.close();
         rd.close();

         if (bSuccess) {
             this.msg = new Message();
             this.msg.what = 0;
             this.msg.obj = ("Reservation Sent Successfully");
             this.progresshandler.sendMessage(this.msg);

         } else {
             this.msg = new Message();
             this.msg.what = 0;
             this.msg.obj = ("Failed to make Reservation");
             this.progresshandler.sendMessage(this.msg);

             // tell caller we failed
             this.setResult(0);
         }
     } catch (Exception e) {
         Log.d("Lets Seat", "Failed to make reservation" + e.getMessage());
     }
     this.msg = new Message();
     this.msg.what = 1;
     this.progresshandler.sendMessage(this.msg);
 }

}

Donnie
  • 243
  • 1
  • 3
  • 11

1 Answers1

2

Have you stepped through this in the debugger? Are you executing the bSuccess code block? Are you getting a response?

I would suggest that you try to pass your data in as parameters through the url -- or as JSON the way you would for AJAX.

Here's an example: How to send HTTP POST request and receive response?

Here's some code you could use: http://moazzam-khan.com/blog/?p=490

Community
  • 1
  • 1
Alan Moore
  • 6,525
  • 6
  • 55
  • 68
  • I stepped through this, in the debugger, and it stops at the part of File Output stream. I have a feeling it doesn't like that I'm Concatenating my variables. Should I pass it through as a bundle? – Donnie Sep 04 '11 at 21:09
  • I would recommend the same thing that @SkeetOverFlow is suggesting. I've never seen anybody try to send data the way you are doing it. I'll add a little code sample to show you what I mean. – Alan Moore Sep 04 '11 at 21:17
  • Okay, I greatly appreciate it! The way I'm sending the data, is mainly from a book called "Android in Action" and I thought I would be able to manipulate the code do to what I was looking for. – Donnie Sep 04 '11 at 21:21
  • I know the feeling, I gave you a couple of links that might come in handy -- the second one gives you code to do what you're trying to do already. – Alan Moore Sep 04 '11 at 21:32
  • Thank you :) I'm going to try the HTTP Post, it seems a lot simpler to try! Also going to save that 2nd link just in case! Thank you @Alan Moore! – Donnie Sep 04 '11 at 21:44