php - 创建一个获取用户输入的 android 应用程序,然后通过 PHP 发送到 mySQL 数据库

标签 php android mysql

我正在编写一个应用程序,它从用户那里获取输入(名字、姓氏、电话号码、预订时间、预订日期以及参加聚会的人数),然后将该数据发送到 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);
 }

最佳答案

你在调试器中逐步完成了吗?你在执行 bSuccess 代码块吗?你得到回应了吗?

我建议您尝试通过 url 将您的数据作为参数传递——或者像 AJAX 那样作为 JSON 传递。

这是一个例子:How to send HTTP POST request and receive response?

这是您可以使用的一些代码:http://moazzam-khan.com/blog/?p=490

关于php - 创建一个获取用户输入的 android 应用程序,然后通过 PHP 发送到 mySQL 数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7302001/

相关文章:

mysql - Wildfly 不会因解析异常错误而启动

mysql - 是什么让 PostgreSQL 比 MySQL 更先进?

php - 如何在 Laravel 中为 REST API 创建 token ?

php - 为什么要在我的表中插入一个字符串?

java - 文本识别库,Android、Java 代码?

android - 指定的 child 在谷歌闹钟代码中已有 parent

php - 使用 php 和 mysql 的搜索过滤器分页问题

php - 与旧 Moodle 平台的用户建立新的 Moodle 平台

java - 第二种布局按钮不起作用

MySQL:#1075 - 表定义不正确;自动增量与另一个键?