当前位置: 首页 > news >正文

鸿蒙harmony将注册的数据包装成json发送到后端的细节及过程

代码资源

https://download.csdn.net/download/hashiqimiya/92240321

上面的代码是springboot后端源码。

接下来是鸿蒙的注册页面代码:

import http from '@ohos.net.http';// Header 接口
interface Header {'Content-Type': string;
}// RequestOptions 接口
interface RequestOptions {url: string;method: string;header: Header;body: string;
}@Entry
@Component
struct RegisterPage {@State username: string = '';@State password: string = '';@State confirmPassword: string = '';@State errorMsg: string = '';build() {Column({ space: 20 }) {Text("用户注册").fontSize(36)// .marginBottom(40).textAlign(TextAlign.Center)//TextInput({ placeholder: "请输入用户名", text: this.username }).onChange((value: string) => {this.username = value}).width('80%').height(40).padding(10).border({ width: 1, color: '#ccc', radius: 12 }).margin({ top: 100 })Row({ space: 10 }) {TextInput({placeholder: "请输入密码",text: this.password,}).type(InputType.Password) // ✅ 正确设置方式.onChange((value: string) => {this.password = value}).width('80%').height(40).padding(10).border({ width: 1, color: '#ccc', radius: 12 })}// 注册按钮Button("注册").onClick(() => {this.handleRegister();}).width('80%').height(40).padding(10).backgroundColor('#ff851c1c').fontColor('white').border({ width: 1, color: '#ff7e5f', radius: 8 }).margin({ top: 20 })Text(this.errorMsg).fontSize(24)}.alignItems(HorizontalAlign.Center).width('100%')}handleRegister() {// 检查输入是否为空if (this.username === "" || this.password === "") {this.errorMsg = "用户名和密码不能为空";return;}// 打包成 JSON 格式// 将 JSON 对象转换为字符串// const jsonString = JSON.stringify(userData);//参数const params = JSON.stringify({username: this.username,password: this.password,})// 创建 HTTP 请求const httpRequest = http.createHttp();// const request:RequestOptions = {//   url: 'http://localhost:8080/api/register', // 替换为你的后端API地址//   method: 'POST',//   header: {//     'Content-Type': 'application/json'//   },//   body: params// };httpRequest.request('http://localhost:8080/api/register',{method: http.RequestMethod.POST,header: {'Content-Type': 'application/json'},extraData: params},(err, data) => {if (err) {console.error('请求失败:', err);this.errorMsg = "注册失败,请稍后再试";} else {console.log('请求成功:', data);this.errorMsg = "注册成功";}});}
}

在上面代码中的接口链接是

http://localhost:8080/api/register

包含localhost。

如果运行在模拟器应该使用的是电脑ip,把localhost改为电脑本地ip。

在preview预览器中的localhost表示的是电脑的本地接口,可调用电脑后端接口,在模拟器中的localhost表示的是模拟器的ip。

注意:

Android的10.0.2.2就是电脑后端的服务,不是电脑ip。

http://www.dtcms.com/a/577046.html

相关文章:

  • JavaWeb(后端进阶)
  • VOC浓度快速测定仪在厂界预警中的实战应用:PID传感器技术与数据分析
  • 【SRE】安装Grafana实践
  • 在 PHP 中打印数据(调试、输出内容)
  • 网站运营有什么用做公司网站需要了解哪些东西
  • 段描述符属性测试
  • Ubuntu安装mysql5.7及常见错误问题
  • 第四届图像处理、计算机视觉与机器学习国际学术会议(ICICML 2025)
  • 网站后台编辑网站开发科普书
  • 单位加强网站建设专门做素菜的网站
  • Rust 在内存安全方面的设计方案的核心思想是“共享不可变,可变不共享”
  • NXP的GUI Guider开发LVGL
  • 《金仓KingbaseES vs 达梦DM:从迁移到运维的全维度TCO实测对比》
  • 【开题答辩全过程】以 基于Java的相机专卖网的设计与实现为例,包含答辩的问题和答案
  • 增量爬取策略:如何持续监控贝壳网最新成交数据
  • 400Hz 橡胶弹性体动刚度扫频试验系统指标
  • Weavefox 携手 GLM-4.6/4.5V 打造新一代智能厨房小助手
  • 如何建立网站后台wordpress 主题 翻译
  • 深入理解 Java 双亲委派机制:JVM 类加载体系全解析
  • Linux 进程间关系与守护进程
  • 基于 Cursor 的智能测试用例生成系统 - 项目介绍与实施指南
  • 时序数据库选型指南:从大数据视角切入,聚焦 Apache IoTDB
  • Node.js 环境变量配置实战:从入门到精通
  • 嵌入式系统入门指南
  • 一次丝滑的内网渗透拿下域控
  • 福建亨利建设集团有限公司网站互展科技网站建设
  • 网页变成PDF下载到本地
  • Spring Boot 3 + Flyway 全流程教程
  • 【洛谷】枚举专题-二进制枚举 从子集到矩阵问题,经典应用与实现
  • 网站信息可以wordpress可视化编辑器插件