从Android应用程序提交数据到MySQL数据库

时间:2014-10-31 13:16:56

标签: java php android mysql

我正在尝试编写表单进行预订并将其保存为mysql。因此,name, phone, email, comment, time, date, table, people形式中有4-5个字段。我必须告诉我,我是Java和Android的新手,不知道该怎么做,但我正在努力。另外我认为代码现在真的很乱,所以我需要一些帮助。 以下是Reservation.java

的代码
public class Reservation extends Activity implements
                             OnClickListener {

String Name;
String Email;
String Phone;
String Comment;
String DateTime;
String Time;
String Table;
String numberOfPeople;

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

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

    editText1 = (EditText) findViewById(R.id.editText1);
    editText3 = (EditText) findViewById(R.id.editText3);
    editText2 = (EditText) findViewById(R.id.editText2);
    editText4 = (EditText) findViewById(R.id.editText4);
    txtDate = (EditText) findViewById(R.id.txtDate);
    txtTime = (EditText) findViewById(R.id.txtTime);

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


    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();
            new SummaryAsyncTask().execute((Void) null);
        }
    }); 
}

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

    private void postData(String name, String email, String phone,
            String comment, String date, String table, String numberofpeople) {

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

        try {
            ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(4);
            nameValuePairs.add(new BasicNameValuePair("Name", name));
            nameValuePairs.add(new BasicNameValuePair("Email", email));
            nameValuePairs.add(new BasicNameValuePair("Phone", phone));
            nameValuePairs.add(new BasicNameValuePair("Comment", comment));
            nameValuePairs.add(new BasicNameValuePair("Date", date));
            nameValuePairs.add(new BasicNameValuePair("Table", table));
            nameValuePairs.add(new BasicNameValuePair("numberOfPeople", numberofpeople));
            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(Name, Email, Phone, Comment, DateTime, Time, Table );
        return null;
    }
 }
}

目前我得到的错误是

  

类型Reservation必须实现继承的抽象方法View.OnClickListener.onClick(View)   更新:

如何在数据库中保存一行或两行的日期和时间?如果是一行如何在android方面做到这一点?

UDPATE2: 现在如何实施btnCalendar, btnTimePicker我将它们放在reservation.xml中并且它们处于形式中。我必须在此代码中实现日期和时间选择器吗?

4 个答案:

答案 0 :(得分:1)

您的类实现了onClick侦听器,但是您缺少方法。

删除implement语句或覆盖方法

答案 1 :(得分:1)

你的logcat问题在这里:

public class Reservation extends Activity implements OnClickListener

您正在实施OnClickListener,然后您正在onClickListener中实施匿名btnMenues。删除implements OnClickListener,错误就会消失。

答案 2 :(得分:1)

implements OnClickListener但是你没有超越onClick(View view) onclicklistener接口的方法,这是强制性的。因此,请先覆盖方法,然后从click listener方法中移除oncreate

有关详细信息,请参阅以下链接: -

https://anujarosha.wordpress.com/2011/11/13/how-to-implements-onclicklistener-for-a-view-item-in-android/

答案 3 :(得分:1)

我认为问题是

public class Reservation extends Activity implements OnClickListener

删除

implements OnClickListener

你不需要这个