1.問題描述:
在系統網絡代理在被清除後,鴻蒙web組件嘗試打開網頁時,獲取到了系統網絡代理,導致網頁打開失敗。
解決方案:
- 檢查網絡權限配置 確保在 module.json5 配置文件中已聲明必要的網絡權限:
"requestPermissions": [
{
"name": "ohos.permission.INTERNET"
},
{
"name": "ohos.permission.GET_NETWORK_INFO"
}
]
缺少權限可能導致網絡請求被系統攔截。
- 調整Web組件屬性 在Web組件中添加以下關鍵屬性以增強網絡訪問能力:
Web({
src: 'https://example.com'
})
.domStorageAccess(true) // 啓用DOM存儲
.fileAccess(true) // 允許文件訪問
.javaScriptAccess(true) // 啓用JavaScript
部分網頁功能(如WebSocket)需要開啓特定權限。
- 處理代理殘留問題
清除緩存:手動清除應用緩存或重啓應用,避免代理配置殘留。
設置自定義UserAgent:部分網絡問題可通過覆蓋默認UserAgent解決:
Web({
src: 'https://example.com'
})
.userAgent('Mozilla/5.0 HarmonyOS-WebView')
- 服務端兼容性驗證
若涉及跨域請求(如開發環境調用遠程API),需服務端配置CORS響應頭(如 Access-Control-Allow-Origin: *)。
通過Postman等工具直接測試接口,排除服務端問題。
- SDK版本與網絡配置
確保使用最新版SDK,舊版本可能存在網絡組件兼容性問題。
檢查系統全局代理設置是否完全關閉(路徑:系統設置 > 網絡和互聯網 > 代理)。
若問題僅在Web組件中復現,而系統瀏覽器正常,需重點排查組件屬性配置及權限聲明。開發過程中可通過DevTools日誌(FocusedHitDataChange 類錯誤)定位具體失敗環節。
2.問題描述:
目前有的流量管理模塊https://developer.huawei.com/consumer/cn/doc/harmonyos-guides...裏面的api,要麼是統計整個設備的,要麼是統計某個應用總的,有什麼辦法分開統計某一個應用的蜂窩和Wi-Fi嗎?
解決方案:
【解決方案】
固定時間分別獲取網卡和蜂窩實時上行和下行流量,兩個數值的差額即為每天設備的流量統計。
部分代碼示例:
TypeScript
import { statistics, socket } from '@kit.NetworkKit';
import { BusinessError } from '@kit.BasicServicesKit';
@Entry
@Component
struct Index {
@State message: string = 'Statistics';
getIfaceBytes() {
// 獲取指定網卡實時下行流量
statistics.getIfaceRxBytes("wlan0").then((stats: number) => {
console.log(`wlan0 down size = ${stats}`);
});
// 獲取指定網卡實時上行流量
statistics.getIfaceTxBytes("wlan0").then((stats: number) => {
console.log(`wlan0 up size = ${stats}`);
});
}
getCellularBytes() {
// 獲取蜂窩實時下行流量
statistics.getCellularRxBytes((error: BusinessError, stats: number) => {
if (error) {
console.error(`getCellularRxBytes err, code: ${error.code}, message: ${error.message}`);
return;
}
console.log(`cellular down size = ${stats}`);
});
// 獲取蜂窩實時上行流量
statistics.getCellularTxBytes((error: BusinessError, stats: number) => {
if (error) {
console.error(`getCellularTxBytes err, code: ${error.code}, message: ${error.message}`);
return;
}
console.log(`cellular up size = ${stats}`);
});
}
build() {
RelativeContainer() {
Text(this.message)
.id('Statistics')
.fontSize($r('app.float.page_text_font_size'))
.fontWeight(FontWeight.Bold)
.alignRules({
center: { anchor: '__container__', align: VerticalAlign.Center },
middle: { anchor: '__container__', align: HorizontalAlign.Center }
})
.onClick(() => {
this.message = 'Welcome';
this.getIfaceBytes();
this.getCellularBytes()
})
}
.height('100%')
.width('100%')
}
}
3.問題描述:
調用getCellularRxBytes/getCellularTxBytes接口異常,{"code":2103012,"message":"Get iface name failed"},如何解決?
解決方案:
在使用Wi-Fi場景下,調用getCellularTxBytes會出現上述異常,切換到蜂窩流量調用即可。
4.問題描述:
手動連接無網Wi-Fi,不會彈“當前WLAN不可上網,是否繼續使用此WLAN”彈窗。
解決方案:
彈框只在首次連接彈出,不會重複彈出。將熱點刪除後重新連接,彈框會再次彈出。
5.問題描述:
系統打開VPN導致ohso.request下載接口失敗,其它RCP接口請求可以正常使用。
解決方案:
上傳下載是在獨立的SA進程,所以走VPN的應用不能使用ohso.request接口,RCP是在應用進程傳輸數據。
6.問題描述:
創建VPN時Config配置中網關地址與VPN IP地址是否可以不一致?
解決方案:
網關地址與VPN分配的IP地址不在同一網段,會出現路由失效的情況,設備無法通過默認網關轉發數據,導致連接超時或無法訪問目標網絡。應保證網關地址和VPN IP地址在同一網段,或將網關地址置空。
7.問題描述:
調試報錯碼2300023,Failed to write the received data to the disk/application.這個異常是接口超5M限制了嗎?有什麼解決方案?
解決方案:
因為http請求中下載文件超過5M的閾值導致圖片過大而報錯,http發起請求的響應消息的最大字節限制默認值是510241024,設置響應數據最大字節限制為100M或是採用流式傳輸。
【修改建議】
- 方案一:maxLimit修改為100M。
http.createHttp().request(url,
{
method: http.RequestMethod.GET,
connectTimeout: 60000,
readTimeout: 60000,
maxLimit: 100 * 1024 * 1024,
},
- 方案二:採用流式傳輸。
// 在dataReceive中拼接圖片數據
let imageChunks: ArrayBuffer[] = [];
httpRequest.on('dataReceive', (data: ArrayBuffer) => {
imageChunks.push(data);
console.info('收到分片,大小:' + data.byteLength + ' bytes');
});
httpRequest.on('dataEnd', () => {
// 計算總長度
let totalLength = imageChunks.reduce((sum, chunk) => sum + chunk.byteLength, 0);
let fullImage = new Uint8Array(totalLength);
// 拼接所有分片數據
let offset = 0;
for (let chunk of imageChunks) {
fullImage.set(new Uint8Array(chunk), offset);
offset += chunk.byteLength;
}
console.info('圖片接收完成,總大小:' + totalLength + 'bytes');
const base64 = btoa(String.fromCharCode(...fullImage));
const imgSrc = `data:image/png;base64,${base64}`;
console.info('可用於展示的圖片 src:', imgSrc);
});