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) fin开发者_运维知识库dViewById(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);
}
}
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
精彩评论