android将空行插入mysql

时间:2016-11-14 10:37:56

标签: php android mysql

我正在研究如何预订停车位申请。 现在,我要添加信息页面。 但是android代码在mysql数据库中放了空行。 网络连接正常,但空数据行插入mysql。 我怎样才能将数据插入mysql? 请帮帮我

public class DateAndTime extends Activity {


EditText Name, Mobile, Addrs, Fee;
TextView Otime, Ctime;
String pic,name, mobile, addrs, otime, ctime, fee;// 바꾸고 이미지도 추가.
ImageView Pic;
ViewFlipper flipper;

/**
 * ATTENTION: This was auto-generated to implement the App Indexing API.
 * See https://g.co/AppIndexing/AndroidStudio for more information.
 */
private GoogleApiClient client;

//View
private TextView mText1;
private TextView mText2;
private Button mPickDate1;
private Button mPickTime1;
private Button mPickDate2;
private Button mPickTime2;

 private int mYear1;
 private int mYear2;
 private int mMonth1;
 private int mMonth2;
 private int mDay1;
 private int mDay2;
 private int mHour1;
 private int mHour2;
 private int mMinute1;
 private int mMinute2;

static final int DATE_DIALOG_ID_1 = 0;
static final int TIME_DIALOG_ID_1 = 1;
static final int DATE_DIALOG_ID_2 = 2;
static final int TIME_DIALOG_ID_2 = 3;

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

    mText1 = (TextView) findViewById(R.id.text1);
    mPickDate1 = (Button) findViewById(R.id.pickDate1);
    mPickTime1 = (Button) findViewById(R.id.pickTime1);

    mText2 = (TextView) findViewById(R.id.text2);
    mPickDate2 = (Button) findViewById(R.id.pickDate2);
    mPickTime2 = (Button) findViewById(R.id.pickTime2);

    mPickDate1.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            showDialog(DATE_DIALOG_ID_1);
        }
    });

    mPickDate2.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            showDialog(DATE_DIALOG_ID_2);
        }
    });

    mPickTime1.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            showDialog(TIME_DIALOG_ID_1);
        }
    });

    mPickTime2.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            showDialog(TIME_DIALOG_ID_2);
        }
    });

    final Calendar c = Calendar.getInstance();
    mYear1 = c.get(Calendar.YEAR);
    mMonth1 = c.get(Calendar.MONTH);
    mDay1 = c.get(Calendar.DAY_OF_MONTH);
    mHour1 = c.get(Calendar.HOUR_OF_DAY);
    mMinute1 = c.get(Calendar.MINUTE);

    mYear2 = c.get(Calendar.YEAR);
    mMonth2 = c.get(Calendar.MONTH);
    mDay2 = c.get(Calendar.DAY_OF_MONTH);
    mHour2 = c.get(Calendar.HOUR_OF_DAY);
    mMinute2 = c.get(Calendar.MINUTE);

    updateDisplay();

    Pic = (ImageView)findViewById(R.id.pic);
    //flipper = (ViewFlipper)findViewById(R.id.flipper);
    Name = (EditText) findViewById(R.id.et_owner);
    Mobile = (EditText) findViewById(R.id.et_mob);
    Addrs = (EditText) findViewById(R.id.et_addrs);
    Otime = (TextView) findViewById(R.id.text1);
    Ctime = (TextView) findViewById(R.id.text2);
    Fee = (EditText) findViewById(R.id.et_fee);

    // ATTENTION: This was auto-generated to implement the App Indexing API.
    // See https://g.co/AppIndexing/AndroidStudio for more information.
    client = new GoogleApiClient.Builder(this).addApi(AppIndex.API).build();
}

public void updateDisplay() {
    mText1.setText(String.format("시작 : %d년 %d월 %d일 %d시 %d분", mYear1, mMonth1 + 1, mDay1, mHour1, mMinute1));
    mText2.setText(String.format("종료 : %d년 %d월 %d일 %d시 %d분", mYear2, mMonth2 + 1, mDay2, mHour2, mMinute2));
}

public DatePickerDialog.OnDateSetListener mDateSetListener1 =
        new DatePickerDialog.OnDateSetListener() {
            @Override
            public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
                mYear1 = year;
                mMonth1 = monthOfYear;
                mDay1 = dayOfMonth;
                updateDisplay();
            }
        };
public DatePickerDialog.OnDateSetListener mDateSetListener2 =
        new DatePickerDialog.OnDateSetListener() {
            @Override
            public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
                mYear2 = year;
                mMonth2 = monthOfYear;
                mDay2 = dayOfMonth;
                updateDisplay();
            }
        };

public TimePickerDialog.OnTimeSetListener mTimeSetListener1 =
        new TimePickerDialog.OnTimeSetListener() {
            @Override
            public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
                mHour1 = hourOfDay;
                mMinute1 = minute;
                updateDisplay();
            }
        };

public TimePickerDialog.OnTimeSetListener mTimeSetListener2 =
        new TimePickerDialog.OnTimeSetListener() {
            @Override
            public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
                mHour2 = hourOfDay;
                mMinute2 = minute;
                updateDisplay();
            }
        };

@Override
protected Dialog onCreateDialog(int id) {
    switch (id) {
        case DATE_DIALOG_ID_1:
            return new DatePickerDialog(this, mDateSetListener1, mYear1, mMonth1, mDay1);

        case TIME_DIALOG_ID_1:
            return new TimePickerDialog(this, mTimeSetListener1, mHour1, mMinute1, false);

        case DATE_DIALOG_ID_2:
            return new DatePickerDialog(this, mDateSetListener2, mYear2, mMonth2, mDay2);

        case TIME_DIALOG_ID_2:
            return new TimePickerDialog(this, mTimeSetListener2, mHour2, mMinute2, false);
    }
    return null;
}
public void saveInfo(View view) {
    pic = null;
    name = Name.getText().toString();
    mobile = Mobile.getText().toString();
    addrs = Addrs.getText().toString();
    otime = Otime.getText().toString();
    ctime = Ctime.getText().toString();
    fee = Fee.getText().toString();
    DateAndTime.BackgroundTask backgroundTask = new DateAndTime.BackgroundTask();
    backgroundTask.execute(pic, name,  mobile, addrs, otime, ctime, fee);
    //finish();
}

/**
 * ATTENTION: This was auto-generated to implement the App Indexing API.
 * See https://g.co/AppIndexing/AndroidStudio for more information.
 */
public Action getIndexApiAction() {
    Thing object = new Thing.Builder()
            .setName("ParkAddInfo Page") // TODO: Define a title for the content shown.
            // TODO: Make sure this auto-generated URL is correct.
            .setUrl(Uri.parse("http://35.160.135.119/add_park_info.php"))
            .build();
    return new Action.Builder(Action.TYPE_VIEW)
            .setObject(object)
            .setActionStatus(Action.STATUS_TYPE_COMPLETED)
            .build();
}

@Override
public void onStart() {
    super.onStart();

    // ATTENTION: This was auto-generated to implement the App Indexing API.
    // See https://g.co/AppIndexing/AndroidStudio for more information.
    client.connect();
    AppIndex.AppIndexApi.start(client, getIndexApiAction());
}

@Override
public void onStop() {
    super.onStop();

    // ATTENTION: This was auto-generated to implement the App Indexing API.
    // See https://g.co/AppIndexing/AndroidStudio for more information.
    AppIndex.AppIndexApi.end(client, getIndexApiAction());
    client.disconnect();
}

class BackgroundTask extends AsyncTask<String, Void, String> {
    String park_add_info_url;

    @Override
    protected void onPreExecute() {

        park_add_info_url = "http://35.160.135.119/add_park_info.php"; // insert the Domain name http://androidtut.comli.com//add_info.php
    }

    @Override
    protected String doInBackground(String... args)  {
        String pic, name,  mobile, addrs, otime, ctime, fee;
        pic = args[0];
        name = args[1];
        mobile = args[2];
        addrs = args[3];
        otime = args[4];
        ctime = args[5];
        fee = args[6];

        try {
            URL url = new URL(park_add_info_url);
            HttpURLConnection httpURLConnection =(HttpURLConnection) url.openConnection();
            httpURLConnection.setRequestMethod("POST");
            httpURLConnection.setDoOutput(true);
            OutputStream outputStream = httpURLConnection.getOutputStream();
            BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream, "UTF-8"));
            StringBuilder stringBuilder = new StringBuilder("pic=");
            stringBuilder.append(pic); stringBuilder.append("&owner="); stringBuilder.append(name); stringBuilder.append("&mobile=");
            stringBuilder.append(mobile); stringBuilder.append("&addrs=");
            stringBuilder.append(addrs); stringBuilder.append("&otime="); stringBuilder.append(otime); stringBuilder.append("&ctime=");
            stringBuilder.append(ctime); stringBuilder.append("&fee=");  stringBuilder.append(fee); stringBuilder.append("'");
            String data_string = URLEncoder.encode(stringBuilder.toString(),"utf-8");
            bufferedWriter.write(data_string);
            bufferedWriter.flush();
            bufferedWriter.close();
            outputStream.close();
            InputStream inputStream = httpURLConnection.getInputStream();
            inputStream.close();
            httpURLConnection.disconnect();
            return "One row of data inserted..";


        } catch (IOException e) {//TODO:check network dialog window
            e.printStackTrace();
        }

        return null;
    }

    @Override
    protected void onProgressUpdate(Void... values) {
        super.onProgressUpdate(values);
    }

    @Override
    protected void onPostExecute(String result) {
        Toast.makeText(getApplicationContext(), result, Toast.LENGTH_LONG).show();
    }

}

这是php代码。

<?php
require "time_init.php"; //connecting mysql

$pic = $_POST["pic"];
$owner = $_POST["owner"];
$mobile = $_POST["mobile"];
$addrs = $_POST["addrs"];
$otime = $_POST["otime"];
$ctime = $_POST["ctime"];
$fee = $_POST["fee"];

$sql = "INSERT INTO park_info VALUES ('$pic', '$owner', '$mobile', '$addrs',  '$otime', '$ctime', '$fee');";
mysqli_query($con, $sql)

?>

1 个答案:

答案 0 :(得分:1)

我认为比建立请求更好的方法是使用Volley来处理请求工作。

  

Volley是一个HTTP库,它使Android应用程序的网络更容易,最重要的是,更快。