HarmonyOS鸿蒙Next中开发的应用在移动网络下无法访问http接口,提示Couldn't resolve host name
HarmonyOS鸿蒙Next中开发的应用在移动网络下无法访问http接口,提示Couldn’t resolve host name
先贴出代码及配置, 代码很简单就是点击按钮,请求接口,拿到数据后,显示在 Text 组件中。
import http from '@ohos.net.http';
@Entry
@Component
struct Index {
@State message: string = 'Hello World'
@State buttonEnable: boolean = true
private scroller: Scroller = new Scroller()
async sendRequest() {
this.buttonEnable = false
let httpRequest = http.createHttp();
httpRequest.on('headersReceive', (header) => {
console.info('header: ' + JSON.stringify(header));
});
console.info("sendRequest")
httpRequest.request('https://dummyjson.com/products/1', {
method: http.RequestMethod.GET,
header: { 'Content-Type': 'application/json' },
readTimeout: 50000,
connectTimeout: 50000,
usingProtocol: undefined,
usingCache: false
}, (err, data) => {
console.info(JSON.stringify(data), JSON.stringify(err))
this.buttonEnable = true
if (!err) {
let result = JSON.parse(data.result as string);
this.message = result["description"];
this.scroller.scrollEdge(Edge.Top)
}
else {
this.message = JSON.stringify(err);
}
})
}
build() {
Column() {
Flex({ direction: FlexDirection.Column }) {
Scroll(this.scroller) {
Text(this.message).fontSize(22).padding({
left: 10, right: 10
})
}.flexGrow(1)
Button('Start')
.width(100)
.height(50)
.enabled(this.buttonEnable)
.onClick(async () => {
this.sendRequest()
})
}
}
.width('100%')
.height('100%')
.justifyContent(FlexAlign.Center)
.alignItems(HorizontalAlign.Center)
}
}
这段代码在 Wi-Fi 环境下运行非常正常,但是当切到移动数据后,就无法访问了,并且提示 {"code":2300006,"message":"Couldn't resolve host name"}
通过 hdc shell curl 访问接口能正常返回数据。
权限已经配置:
{
"module": {
"name": "entry",
"type": "entry",
"description": "$string:module_desc",
"mainElement": "EntryAbility",
"deviceTypes": [
"phone",
"tablet"
],
"deliveryWithInstall": true,
"installationFree": false,
"pages": "$profile:main_pages",
"requestPermissions": [
{
"name": "ohos.permission.INTERNET",
}
],
"abilities": [
{
"name": "EntryAbility",
"srcEntry": "./ets/entryability/EntryAbility.ts",
"description": "$string:EntryAbility_desc",
"icon": "$media:icon",
"label": "$string:EntryAbility_label",
"startWindowIcon": "$media:icon",
"startWindowBackground": "$color:start_window_background",
"exported": true,
"skills": [
{
"entities": [
"entity.system.home"
],
"actions": [
"action.system.home"
]
}
]
}
]
}
}
这个是什么问题导致的? 是因为开发的 app 不能使用移动数据?
网站换了好几个,换成了百度的地址也是一样。
更多关于HarmonyOS鸿蒙Next中开发的应用在移动网络下无法访问http接口,提示Couldn't resolve host name的实战教程也可以访问 https://www.itying.com/category-93-b0.html
手机设置-系统和更新-重置-还原网络设置
更多关于HarmonyOS鸿蒙Next中开发的应用在移动网络下无法访问http接口,提示Couldn't resolve host name的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html
谢谢,困扰了好久,终于解决了,
感谢!我就知道社区里有大神!
你好,请问你解决了问题了吗?我这边也出现了?
工单的恢复太慢了, 已放弃继续使用鸿蒙开发.
哥们找到问题了吗?我也出现这个问题了
调用自己公司服务器的接口可以,调用外部的就不行了,
提了工单,但是目前还没有回复,我的是在移动数据的情况下才无法访问,连baidu的服务器也通不了,但是的确移动网络是可以访问的。