我现在想要写一个可以与MIT appinventor的cloudDB共享数据的HTML,请问我怎么做
4条回答 默认 最新
关注让【宝码香车】来帮您解答,本回答参考DeepSeek编写,并整理提供。
如果答案让您满意,请采纳、点赞、关注,非常感谢!
.
每日一句正能量,让它带给您好心情:努力不是为了超越别人,而是为了成为更好的自己。
.MIT App Inventor CloudDB 数据共享 HTML 实现
我将为您创建一个可以与 MIT App Inventor 的 CloudDB 共享数据的 HTML 页面。这个页面将允许您从 CloudDB 读取数据、向 CloudDB 写入数据,并实时监听数据变化。
实现思路
- 使用 JavaScript 的 Fetch API 与 CloudDB 的 Web API 进行通信
- 实现数据读取、写入和监听功能
- 创建用户友好的界面来展示和操作数据
完整代码实现
<!DOCTYPE html> <html lang="zh-CN"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>MIT App Inventor CloudDB 数据共享</title> <style> * { box-sizing: border-box; margin: 0; padding: 0; font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif; } body { background: linear-gradient(135deg, #6a11cb 0%, #2575fc 100%); color: #333; line-height: 1.6; padding: 20px; min-height: 100vh; } .container { max-width: 800px; margin: 0 auto; background: rgba(255, 255, 255, 0.95); border-radius: 15px; box-shadow: 0 10px 30px rgba(0, 0, 0, 0.2); padding: 30px; overflow: hidden; } header { text-align: center; margin-bottom: 30px; padding-bottom: 20px; border-bottom: 1px solid #eee; } h1 { color: #2575fc; margin-bottom: 10px; font-size: 2.2rem; } .description { color: #666; font-size: 1.1rem; } .config-section { background: #f8f9fa; padding: 20px; border-radius: 10px; margin-bottom: 25px; } .form-group { margin-bottom: 15px; } label { display: block; margin-bottom: 5px; font-weight: 600; color: #444; } input, textarea, select { width: 100%; padding: 12px; border: 1px solid #ddd; border-radius: 5px; font-size: 16px; transition: border 0.3s; } input:focus, textarea:focus, select:focus { border-color: #2575fc; outline: none; box-shadow: 0 0 0 2px rgba(37, 117, 252, 0.2); } .btn { display: inline-block; background: #2575fc; color: white; border: none; padding: 12px 20px; border-radius: 5px; cursor: pointer; font-size: 16px; font-weight: 600; transition: all 0.3s; margin-right: 10px; margin-bottom: 10px; } .btn:hover { background: #1a68e8; transform: translateY(-2px); box-shadow: 0 5px 15px rgba(0, 0, 0, 0.1); } .btn-secondary { background: #6c757d; } .btn-secondary:hover { background: #5a6268; } .btn-success { background: #28a745; } .btn-success:hover { background: #218838; } .actions { display: flex; flex-wrap: wrap; margin-bottom: 25px; } .data-section { margin-top: 25px; } .data-display { background: #f8f9fa; border: 1px solid #e9ecef; border-radius: 5px; padding: 15px; margin-top: 10px; min-height: 100px; max-height: 300px; overflow-y: auto; white-space: pre-wrap; font-family: monospace; } .status { padding: 10px; border-radius: 5px; margin-top: 15px; text-align: center; font-weight: 600; } .status.success { background: #d4edda; color: #155724; border: 1px solid #c3e6cb; } .status.error { background: #f8d7da; color: #721c24; border: 1px solid #f5c6cb; } .instructions { background: #e7f3ff; border-left: 4px solid #2575fc; padding: 15px; margin: 20px 0; border-radius: 0 5px 5px 0; } footer { text-align: center; margin-top: 30px; padding-top: 20px; border-top: 1px solid #eee; color: #666; font-size: 0.9rem; } @media (max-width: 600px) { .container { padding: 15px; } h1 { font-size: 1.8rem; } .btn { width: 100%; margin-right: 0; } } </style> </head> <body> <div class="container"> <header> <h1>MIT App Inventor CloudDB 数据共享</h1> <p class="description">通过此页面与您的 MIT App Inventor 应用共享数据</p> </header> <div class="instructions"> <h3>使用说明:</h3> <p>1. 在 MIT App Inventor 项目中启用 CloudDB 组件并获取项目ID和令牌</p> <p>2. 在下方配置区域填写您的项目信息</p> <p>3. 使用操作按钮与 CloudDB 进行数据交互</p> </div> <div class="config-section"> <h2>CloudDB 配置</h2> <div class="form-group"> <label for="projectId">项目ID (Project ID):</label> <input type="text" id="projectId" placeholder="输入您的项目ID"> </div> <div class="form-group"> <label for="token">访问令牌 (Token):</label> <input type="text" id="token" placeholder="输入您的访问令牌"> </div> <div class="form-group"> <label for="tag">标签 (Tag):</label> <input type="text" id="tag" placeholder="输入数据标签" value="sharedData"> </div> </div> <div class="actions"> <button class="btn" id="getDataBtn">获取数据</button> <button class="btn btn-success" id="setDataBtn">设置数据</button> <button class="btn btn-secondary" id="clearDataBtn">清除数据</button> <button class="btn" id="startListeningBtn">开始监听</button> <button class="btn btn-secondary" id="stopListeningBtn">停止监听</button> </div> <div class="form-group"> <label for="dataInput">要存储的数据:</label> <textarea id="dataInput" placeholder='输入要存储的数据,例如: {"name": "张三", "score": 95}' rows="4"></textarea> </div> <div class="data-section"> <h2>数据展示</h2> <div class="data-display" id="dataDisplay"> 数据将在这里显示... </div> </div> <div id="statusMessage" class="status"></div> <footer> <p>MIT App Inventor CloudDB 数据共享界面 | 使用 CloudDB Web API</p> </footer> </div> <script> // CloudDB API 基础URL const CLOUDDB_BASE_URL = 'https://clouddb.appinventor.mit.edu'; // 获取DOM元素 const projectIdInput = document.getElementById('projectId'); const tokenInput = document.getElementById('token'); const tagInput = document.getElementById('tag'); const dataInput = document.getElementById('dataInput'); const dataDisplay = document.getElementById('dataDisplay'); const statusMessage = document.getElementById('statusMessage'); // 获取操作按钮 const getDataBtn = document.getElementById('getDataBtn'); const setDataBtn = document.getElementById('setDataBtn'); const clearDataBtn = document.getElementById('clearDataBtn'); const startListeningBtn = document.getElementById('startListeningBtn'); const stopListeningBtn = document.getElementById('stopListeningBtn'); // 监听器相关变量 let listeningInterval = null; // 显示状态消息 function showStatus(message, isError = false) { statusMessage.textContent = message; statusMessage.className = 'status ' + (isError ? 'error' : 'success'); // 3秒后清除状态消息 setTimeout(() => { statusMessage.textContent = ''; statusMessage.className = 'status'; }, 3000); } // 验证配置 function validateConfig() { const projectId = projectIdInput.value.trim(); const token = tokenInput.value.trim(); const tag = tagInput.value.trim(); if (!projectId) { showStatus('错误: 请输入项目ID', true); return false; } if (!token) { showStatus('错误: 请输入访问令牌', true); return false; } if (!tag) { showStatus('错误: 请输入标签', true); return false; } return { projectId, token, tag }; } // 从CloudDB获取数据 async function getData() { const config = validateConfig(); if (!config) return; try { const response = await fetch( `${CLOUDDB_BASE_URL}/api/v1/${config.projectId}/data/${config.tag}`, { headers: { 'Authorization': `Bearer ${config.token}` } } ); if (response.ok) { const data = await response.json(); dataDisplay.textContent = JSON.stringify(data, null, 2); showStatus('数据获取成功'); } else { dataDisplay.textContent = '错误: 无法获取数据'; showStatus('错误: 无法获取数据', true); } } catch (error) { dataDisplay.textContent = '错误: ' + error.message; showStatus('错误: ' + error.message, true); } } // 向CloudDB存储数据 async function setData() { const config = validateConfig(); if (!config) return; const data = dataInput.value.trim(); if (!data) { showStatus('错误: 请输入要存储的数据', true); return; } try { // 尝试解析JSON数据 const parsedData = JSON.parse(data); const response = await fetch( `${CLOUDDB_BASE_URL}/api/v1/${config.projectId}/data/${config.tag}`, { method: 'POST', headers: { 'Authorization': `Bearer ${config.token}`, 'Content-Type': 'application/json' }, body: JSON.stringify(parsedData) } ); if (response.ok) { showStatus('数据存储成功'); // 存储成功后获取最新数据 getData(); } else { showStatus('错误: 无法存储数据', true); } } catch (error) { showStatus('错误: 数据格式不正确,请使用有效的JSON格式', true); } } // 清除CloudDB中的数据 async function clearData() { const config = validateConfig(); if (!config) return; if (!confirm('确定要清除数据吗?此操作不可撤销。')) { return; } try { const response = await fetch( `${CLOUDDB_BASE_URL}/api/v1/${config.projectId}/data/${config.tag}`, { method: 'DELETE', headers: { 'Authorization': `Bearer ${config.token}` } } ); if (response.ok) { showStatus('数据清除成功'); dataDisplay.textContent = '数据已清除'; } else { showStatus('错误: 无法清除数据', true); } } catch (error) { showStatus('错误: ' + error.message, true); } } // 开始监听数据变化 function startListening() { const config = validateConfig(); if (!config) return; if (listeningInterval) { showStatus('已经在监听数据变化'); return; } showStatus('开始监听数据变化...'); listeningInterval = setInterval(getData, 3000); // 每3秒获取一次数据 } // 停止监听数据变化 function stopListening() { if (listeningInterval) { clearInterval(listeningInterval); listeningInterval = null; showStatus('已停止监听数据变化'); } else { showStatus('当前没有在监听数据变化'); } } // 添加事件监听器 getDataBtn.addEventListener('click', getData); setDataBtn.addEventListener('click', setData); clearDataBtn.addEventListener('click', clearData); startListeningBtn.addEventListener('click', startListening); stopListeningBtn.addEventListener('click', stopListening); // 页面加载时显示示例数据 window.addEventListener('load', () => { dataInput.value = JSON.stringify({ message: "Hello from HTML!", timestamp: new Date().toISOString(), user: "Web User" }, null, 2); }); </script> </body> </html>使用说明
- 获取 CloudDB 配置信息:
- 在 MIT App Inventor 项目中启用 CloudDB 组件
- 获取项目ID (Project ID) 和访问令牌 (Token)
- 配置页面:
- 在页面上方的配置区域填写项目ID、访问令牌和标签
- 标签是用于标识数据的键名
- 操作数据:
- 获取数据:从 CloudDB 读取当前存储的数据
- 设置数据:将输入框中的数据存储到 CloudDB
- 清除数据:删除 CloudDB 中对应标签的数据
- 开始监听:每3秒自动获取一次数据(实现实时更新)
- 停止监听:停止自动获取数据
- 数据格式:
- 数据应以 JSON 格式输入,例如:
{"name": "张三", "score": 95}
注意事项
- 确保您的 MIT App Inventor 项目已正确配置 CloudDB 组件
- 确保项目ID和访问令牌正确无误
- 数据监听功能会每3秒请求一次数据,请勿过于频繁使用以免超过 API 限制
这个 HTML 页面提供了完整的数据交互功能,您可以直接使用它来与 MIT App Inventor 的 CloudDB 进行数据共享。
本回答被题主选为最佳回答 , 对您是否有帮助呢?评论 打赏 举报解决 1无用