
本教程旨在指导Android开发者如何将从日历选择器获取的日期ArrayList数据,通过PHP脚本发送并存储到MySQL数据库表中。我们将详细介绍数据结构定义、数据处理以及异步任务的实现,帮助初学者理解Android与PHP之间的数据交互。
为了方便数据的传输和存储,首先需要定义一个与MySQL数据库表结构相对应的Java类。这个类将作为数据载体,封装需要传输的数据。
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;
}
}注意: 确保Profile类的成员变量与MySQL数据库表中的列名和数据类型相匹配。
假设我们有一个ArrayListzuojiankuohaophpcnDate>,其中存储了从日历选择器中选中的日期。我们需要将这些日期转换为Profile对象,然后将这些Profile对象放入一个列表中。
立即学习“PHP免费学习笔记(深入)”;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
public class DateConverter {
public static List<Profile> convertDatesToProfiles(ArrayList<Date> dates) {
List<Profile> profileList = new ArrayList<>();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); // 定义日期格式
for (int i = 0; i < dates.size(); i++) {
Date date = dates.get(i);
Profile profile = new Profile();
profile.setId(i + 1); // 设置ID
profile.setName(sdf.format(date)); // 将日期格式化为字符串并设置为Name
profileList.add(profile);
}
return profileList;
}
}在这个例子中,我们将日期格式化为字符串,并将其设置为Profile对象的name属性。你可以根据实际需求修改Profile对象的属性设置。
为了避免阻塞主线程,我们使用AsyncTask来执行网络请求。
import android.os.AsyncTask;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.List;
import com.google.gson.Gson;
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];
Gson gson = new Gson();
String jsonString = gson.toJson(profileList); // 将List<Profile>转换为JSON字符串
try {
URL url = new URL(PHP_URL + "?data=" + jsonString); // 将JSON字符串作为参数传递给PHP
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET"); // 或者 POST
BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
StringBuilder result = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
result.append(line);
}
reader.close();
return result.toString();
} catch (Exception e) {
e.printStackTrace();
return "Error: " + e.getMessage();
}
}
@Override
protected void onPostExecute(String result) {
// 处理PHP返回的结果
System.out.println("PHP Response: " + result);
}
}使用示例:
ArrayList<Date> selectedDates = (ArrayList<Date>) calendarPickerView.getSelectedDates(); List<Profile> profileList = DateConverter.convertDatesToProfiles(selectedDates); new SendDataTask().execute(profileList);
注意事项:
以下是一个简单的PHP脚本,用于接收JSON数据并将其插入到MySQL数据库中。
<?php
$servername = "localhost";
$username = "your_username";
$password = "your_password";
$dbname = "your_dbname";
// 创建连接
$conn = new mysqli($servername, $username, $password, $dbname);
// 检测连接
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$data = $_GET["data"]; // 接收JSON数据
$profiles = json_decode($data, true); // 解码JSON数据
foreach ($profiles as $profile) {
$id = $profile["id"];
$name = $profile["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 ArrayList数据发送到MySQL数据库的完整示例。通过定义数据结构、数据转换和使用异步任务,可以有效地实现Android与PHP之间的数据交互。请务必根据你的实际需求修改代码,并注意安全性。
以上就是将ArrayList数据发送到MySQL表的列:Android与PHP教程的详细内容,更多请关注php中文网其它相关文章!
PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号