
本教程旨在指导 Android 初学者如何将从 TimeSquare CalendarPicker 中选择的日期数据(存储在 ArrayList 中)通过 PHP 脚本发送到 MySQL 数据库。文章将详细介绍如何构建数据模型、处理数据以及使用异步任务将数据上传到服务器,并提供示例代码帮助你理解整个过程。
首先,你需要创建一个 Java 类来表示你要存储在 MySQL 数据库中的数据结构。这个类应该包含与数据库表列相对应的属性。例如,如果你的数据库表包含 id、id_card、name、phone 和 address 列,那么你的 Profile 类应该如下所示:
public class Profile {
private Integer id;
private String id_card;
private String name;
private String phone;
private String address;
public Profile() {
super();
}
public Profile(Integer id, String id_card, String name, String phone, String address) {
super();
this.id = id;
this.id_card = id_card;
this.name = name;
this.phone = phone;
this.address = address;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getIdCard() {
return id_card;
}
public void setIdCard(String id_card) {
this.id_card = id_card;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
}现在,你需要将从 CalendarPicker 中获取的日期数据转换为 Profile 对象列表。 由于CalendarPicker返回的是Date对象,你需要先将其格式化为字符串才能存储到数据库中。
假设你有一个 ArrayListzuojiankuohaophpcnDate> 类型的 selectedDates 列表,你可以使用以下代码将其转换为 Profile 对象列表:
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
public List<Profile> createProfileList(ArrayList<Date> selectedDates) {
List<Profile> profileList = new ArrayList<>();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); // 定义日期格式
int i = 1; // 用于生成唯一的 ID
for (Date date : selectedDates) {
Profile profile = new Profile();
profile.setId(i++);
profile.setName(sdf.format(date)); // 将日期格式化为字符串并设置为 name 属性
profileList.add(profile);
}
return profileList;
}注意: 上面的代码假设你的数据库表有一个名为 name 的列,用于存储日期字符串。你需要根据你的实际数据库表结构进行调整。
由于网络操作可能耗时,因此不应在主线程中执行。 你可以使用 AsyncTask 在后台线程中执行网络操作。
import android.os.AsyncTask;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
public class SendDataTask extends AsyncTask<List<Profile>, Void, String> {
private static final String PHP_URL = "YOUR_PHP_SCRIPT_URL"; // 替换为你的 PHP 脚本 URL
@Override
protected String doInBackground(List<Profile>... lists) {
List<Profile> profileList = lists[0];
StringBuilder response = new StringBuilder();
try {
for (Profile profile : profileList) {
// 构建 URL
String urlString = PHP_URL + "?id=" + profile.getId() + "&name=" + profile.getName(); // 构建包含数据的 URL
URL url = new URL(urlString);
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
try {
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));
String line;
while ((line = bufferedReader.readLine()) != null) {
response.append(line);
}
bufferedReader.close();
} finally {
urlConnection.disconnect();
}
}
} catch (Exception e) {
e.printStackTrace();
return "Error: " + e.getMessage();
}
return response.toString();
}
@Override
protected void onPostExecute(String result) {
// 处理来自 PHP 脚本的响应
// 例如,显示 Toast 消息
}
}注意:
你需要创建一个 PHP 脚本来接收来自 Android 应用程序的数据并将其插入到 MySQL 数据库中。
<?php
$servername = "localhost";
$username = "your_username";
$password = "your_password";
$dbname = "your_database";
// 创建连接
$conn = new mysqli($servername, $username, $password, $dbname);
// 检测连接
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// 获取数据
$id = $_GET["id"];
$name = $_GET["name"];
// 插入数据
$sql = "INSERT INTO your_table (id, name) VALUES ('$id', '$name')";
if ($conn->query($sql) === TRUE) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
$conn->close();
?>注意:
最后,你需要在你的 Android 代码中调用 SendDataTask 来发送数据。
// 获取选定的日期 ArrayList<Date> selectedDates = (ArrayList<Date>) calendarPickerView.getSelectedDates(); // 创建 Profile 对象列表 List<Profile> profileList = createProfileList(selectedDates); // 执行 AsyncTask new SendDataTask().execute(profileList);
本教程提供了一个将 ArrayList 数据发送到 MySQL 表列的完整示例。请记住,这只是一个基本示例,你需要根据你的实际需求进行修改。
注意事项:
通过遵循这些步骤和注意事项,你可以成功地将 ArrayList 数据发送到 MySQL 表列。
以上就是将 ArrayList 数据发送到 MySQL 表列的教程的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号