HarmonyOS 鸿蒙Next 前后端连接的问题
HarmonyOS 鸿蒙Next 前后端连接的问题 如图,第一次真机测试的时候,信息是能够出来的。但是第二次运行开始,数据就都传不过来了~
在线跟各位大佬们求助~已经在这里卡住好多天了~
代码情况如下:
// 数据请求————获取相应咨询师的信息
async onAttached(){
var that=this;
console.log("咨询师ID"+this.zixunshiid)
let url="http://124.71.103.20/setting/settingInfo?userId="+this.zixunshiid;
// 每一个httpRequest对应一个http请求任务,不可复用
let httpRequest = http.createHttp();
// 用于订阅http响应头,此接口会比request请求先返回。可以根据业务需要订阅此消息
await httpRequest.on('headerReceive', (err, data) => {
if (!err) {
console.info('header: '+ data.header);
} else {
console.info('error:'+ err.data);
}
});
await httpRequest.request(
// 填写http请求的url地址,可以带参数也可以不带参数。URL地址需要开发者自定义。GET请求的参数可以在extraData中指定
url,
{
method: 'POST', // 可选,默认为“GET”
// 开发者根据自身业务需要添加header字段
header: {
'Content-Type': 'application/json'
},
// 当使用POST请求时此字段用于传递内容
readTimeout: 60000, // 可选,默认为60000ms
connectTimeout: 60000 // 可选,默认为60000ms
},(err, data) =>{
if (!err.data) {
// data.result为http响应内容,可根据业务需要进行解析
let datar = JSON.parse(data.result);
that.fharray1=datar.data.yunstudioList[0].timeArray
for (let i = 0; i < that.fharray1.length; i++) {
console.log(that.fharray1[i]);
that.fharray.push({style: "style_off",time:that.fharray1[i]})
}
that.notice_detail=datar.data.yunstudioList[0].yunmessage
that.date=datar.data.yunstudioList[0].date1
that.address=datar.data.yunstudioList[0].address
that.brief=datar.data.setting.brief
that.zixunshiid=datar.data.yunstudioList[0].consultantId
that.tempall=datar.data.yunstudioList[0].tempall
that.tempsurplus="?"
that.temporary1=datar.data.yunstudioList[0].tempArray
for (let i = 0; i < that.temporary1.length; i++) {
console.log(that.temporary1[i]);
that.temporary.push({style: "style_off1",index:that.temporary1[i]})
}
console.log('打印1'+datar.data.setting.id);
console.log('打印1'+that.fharray1);
console.log('打印3'+that.fharray[0].style);
console.log('打印3'+that.fharray[1].time);
console.log(that.fharray+that.notice_detail);
console.log(that.date+that.address+that.brief+that.zixunshiid+that.tempall+that.tempsurplus+that.temporary)
console.info('Result:' + datar.data[0].username);
console.info('code:' + data.responseCode);
// data.header为http响应头,可根据业务需要进行解析
console.info('header:' + data.header);
} else {
console.info('error2:' + err.data);
}
}
);
},
// 以上是获取当前页面的信息
只执行到这一步: let url=“http://124.71.103.20/setting/settingInfo?userId=”+this.zixunshiid;
而if (!err)后面的内容都没有执行。
更多关于HarmonyOS 鸿蒙Next 前后端连接的问题的实战教程也可以访问 https://www.itying.com/category-93-b0.html
6 回复
楼主你好,在config.json中添加
"deviceConfig": { "default": { "network": { "cleartextTraffic": true } } }
以下代码本地测试每次都可以成功 // 数据请求————获取相应咨询师的信息
// 数据请求————获取相应咨询师的信息
async postRequest() {
var that = this;
let url = "http://124.71.103.20/setting/settingInfo";
// 每一个httpRequest对应一个http请求任务,不可复用
let httpRequest = http.createHttp();
// 用于订阅http响应头,此接口会比request请求先返回。可以根据业务需要订阅此消息
await httpRequest.on('headerReceive', (err, data) => {
if (!err) {
console.info('header: ' + JSON.stringify(data));
} else {
console.info('error:' + JSON.stringify(err));
}
});
await httpRequest.request(
// 填写http请求的url地址,可以带参数也可以不带参数。URL地址需要开发者自定义。GET请求的参数可以在extraData中指定
url,
{
method: 'POST', // 可选,默认为“GET”
// 开发者根据自身业务需要添加header字段
header: {
'Content-Type': 'application/json'
},
// 当使用POST请求时此字段用于传递内容
readTimeout: 60000, // 可选,默认为60000ms
connectTimeout: 60000 // 可选,默认为60000ms
}, (err, data) => {
if (!err) {
console.info('Result:' + data.result);
console.info('code:' + data.responseCode);
// data.header为http响应头,可根据业务需要进行解析
console.info('header:' + data.header);
} else {
console.info('error2:' + err.data);
}
}
);
}
更多关于HarmonyOS 鸿蒙Next 前后端连接的问题的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html
姓名: 张三
职位: 软件工程师
简介: 拥有超过10年的软件开发经验,擅长Java和Python。
欢迎开发小伙伴们进来帮帮楼主
你好,请问你有实现登录功能,连接后端吗?能讲一下吗?
抓包或者看下服务端日志,看看链接信息吧