我正在编写一个可以从用户那里获得输入的应用程序(名字,姓氏,电话号码,预约时间,预约日期以及聚会中有多少人)然后将该数据发送到mySQL服务器。我虽然遇到了麻烦。我是Android开发的新手,所以我真的不知道很多技巧或方法。我研究了这个,我想出的是如何从数据库中提取数据。我的应用程序几乎已构建完成,我将其用于用户输入其信息的位置,然后单击“提交”。当他们点击提交时,他们输入的所有信息都消失了,SQL数据库中没有任何内容。你们有什么建议可以给我,我很感激:)谢谢!以下是我使用的代码。我发现了一本使用mySQL数据库的书,并且围绕着我的代码编写了很多代码。
@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;
-------预约班--------------
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);
}
}
答案 0 :(得分:2)
您是否在调试器中完成了此操作?你在执行bSuccess代码块吗?你收到回复了吗?
我建议你尝试通过url将数据作为参数传递 - 或者像AJAX一样传递给JSON。
以下是一个示例:How to send HTTP POST request and receive response?
以下是您可以使用的一些代码:http://moazzam-khan.com/blog/?p=490