HarmonyOS 鸿蒙Next http请求返回 [PC Preview] unknown type

HarmonyOS 鸿蒙Next http请求返回 [PC Preview] unknown type

环境

  • 系统:macOS 13.3.1
  • 开发工具:DevEco Studio 4.0 Beta2
  • 项目类型:OpenHarmony API10
  • 开发语言:ArkUI

具体代码

// 引入包名
import http from '@ohos.net.http';
import RequestMethod from '@ohos.net.http';
import ResponseCode from '@ohos.net.http';

@Entry
@Component
struct Index {
  @State message: string = 'Hello World 2'
  @State username: string = ''
  @State password: string = ''
  @State wholeContent: string = "";

  build() {
    Column() {
      Scroll() {
        Column() {
          Row() {
            Image("https://pic.netbian.com/uploads/allimg/190401/201711-155412103118a5.jpg")
              .width('100%')
              .height('100%')
          }
          .justifyContent(FlexAlign.Center)
          .alignItems(VerticalAlign.Center)
          .backgroundColor(0xF3F3F3)
          .height(200)
          .width('100%')

          Column() {
            TextInput({ placeholder: 'please input username', text: this.username })
            TextInput({ placeholder: 'please input password', text: this.password })
              .margin({ top: 10 })
            Row() {
              Button('http')
                .width('90%')
                .onClick(() => {
                  this.wholePoemRequest();
                })
            }
            .width('100%')
            .height(100)
            .justifyContent(FlexAlign.Center)
          }
          .margin(15)
          .height(300)
        }
      }
      .width('100%')
      .scrollable(ScrollDirection.Vertical)
      .scrollBar(BarState.Off)
    }
    .width('100%')
    .height('100%')
  }

  private wholePoemRequest() {
    console.info('wholePoemRequest');
    let httpRequest = http.createHttp();
    httpRequest.request(
      "https://py.myie9.com/xuxietest/" + "汗滴禾下土",
      {
        method: RequestMethod.RequestMethod.GET,
        readTimeout: 15000,
        connectTimeout: 15000,
      },
      (error, data) => {
        if (error) {
          console.log("error code: " + error.code + ", msg: " + error.message)
        } else {
          let code = data.responseCode
          if (ResponseCode.ResponseCode.OK == code) {
            this.wholeContent = data.result.toString();
            let header = JSON.stringify(data.header);
            console.log("result: " + this.wholeContent);
            console.log("header: " + header);
          } else {
            console.log("response code: " + code);
          }
        }
      }
    );
  }
}

http请求返回 [PC Preview] unknown type

debug截图:

debug截图

完整demo地址 https://gitee.com/reylen/oh-hello-wold.git


更多关于HarmonyOS 鸿蒙Next http请求返回 [PC Preview] unknown type的实战教程也可以访问 https://www.itying.com/category-93-b0.html

14 回复

[PC Preview] unknown result

[PC Preview] unknown cookies

是什么原因?如何解决?

更多关于HarmonyOS 鸿蒙Next http请求返回 [PC Preview] unknown type的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html


同样的问题 {“result”:"[PC Preview] unknown result",“header”:{},“cookies”:"[PC Preview] unknown cookies"}

怎么解决,

  • 问题一
  • 问题二
  • 问题三
  1. 换了真机
  2. Header 中 添加 User-Agent,

遇到了相同问题,请问最后是怎么解决的呀?

暂时没找到解决办法,可能下个版本就好了,

你的getOptions里少了header里的属性配置

//Get请求配置
export let getOptions = {
  method: http.RequestMethod.GET,
  //请求头
  header: {
    'Content-Type': 'application/json'
  },
  //连接超时
  connectTimeout: 60000,
  //读取超时
  readTimeout: 60000,
}
应该和这个没关系,我之前拿官方文档完整的http请求实例代码来,也是一样的结果现在用的是DevEco Studio 4.0 Beta2版本,sdk也是用的里面的最新的,感觉像是sdk里面问题,或者是新版有变更,

我用的也是4.0Beata2,API9我这边是OK的QAQ,

找HarmonyOS工作还需要会Flutter的哦,有需要Flutter教程的可以学学大地老师的教程,很不错,B站免费学的哦:BV1S4411E7LY/?p=17

OpenHarmony API 10,版本4.0.9.6,请求是不行的。OpenHarmony API 9版本,同样的请求代码是可以请求到结果。

export interface HttpResponse { /** * result can be a string (API 6) or an ArrayBuffer(API 8). Object is deprecated from API 8. * If {@link HttpRequestOptions#expectDataType} is set, the system preferentially returns this parameter. / result: string | Object | ArrayBuffer; /* * If the resultType is string, you can get result directly. * If the resultType is Object, you can get result such as this: result[‘key’]. * If the resultType is ArrayBuffer, you can use ArrayBuffer to create the binary objects. * * @since 9 / resultType: HttpDataType; /* * Server status code. / responseCode: ResponseCode | number; /* * All headers in the response from the server. / header: Object; /* * @since 8 */ cookies: string; }

猜测返回的有可能是ResponseCode或者number所以会提示为未知类型,可以主动转成string或者number

const resultCode = parseInt(JSON.stringify(data.responseCode)) if ( resultCode == http.ResponseCode.OK){ //网络请求成功 } else { //网络请求失败 }

resultCode: NaN

在HarmonyOS(鸿蒙Next)中,当使用HTTP请求时,如果返回的结果类型显示为 unknown type,这通常是由于开发环境或调试工具在处理HTTP响应时无法正确解析或识别返回的数据类型。这种情况可能发生在使用某些开发工具(如DevEco Studio的PC预览模式)时,工具无法正确解析HTTP响应的内容格式,导致显示为未知类型。

要解决这个问题,可以检查HTTP请求的响应头,确保返回的数据格式与预期的格式一致。常见的HTTP响应格式包括JSON、XML、HTML等。如果响应格式正确但工具仍无法识别,可以尝试直接查看HTTP响应的原始数据,或者使用其他调试工具(如Postman)来验证请求和响应的正确性。此外,确保开发环境和工具的版本是最新的,以避免因工具本身的问题导致的解析错误。

回到顶部