体验鸿蒙开发第2课-http and image

修改Index.ets:
// 导入http模块。
import http from '@ohos.net.http';
import router from '@ohos.router';
import image from '@ohos.multimedia.image'
@Entry
@Component
struct Index {
  @State message: string = '我是没头脑'

  build() {
    Row() {
      Column() {
        Text(this.message)
          .fontSize(38)
          .fontWeight(FontWeight.Bold)

        // 请求
        Button(('baidu'), { type: ButtonType.Capsule })
          .width(140)
          .fontSize(40)
          .fontWeight(FontWeight.Medium)
          .margin({ top: 20, bottom: 20 })
          .onClick(() => {
            this.httpReq()
          })
        Image($r('app.media.logo'))
          .width(181)
          .height(181)
          .margin({ top: 20, bottom: 20 })
          .width(181)
          .height(181)
          .margin({ top: 20, bottom: 20 })
        Button() {
          Text("Open Page")
            .fontSize(30)
            .fontWeight(FontWeight.Bold)
        }
        .type(ButtonType.Capsule)
        .margin({
          top: 20
        })
        .backgroundColor('#ff9900')
        .width('80%')
        .height('5%')
        // 给按钮绑定点击事件
        .onClick(()=>{
          console.log("点击了按钮") // 跳转第二个页面
          router.pushUrl({url: 'pages/second' }).then(()=>{
            console.log("成功跳转了");
          })
        })

      }
      .width('100%')
    }
    .height('100%')
  }

  private httpReq() {
    // 创建httpRequest对象。
    let httpRequest = http.createHttp();

    let url = "https://www.baidu.com/";
    let promise = httpRequest.request(
      // 请求url地址
      url,
      {
        // 请求方式
        method: http.RequestMethod.GET,
        // 可选,默认为60s
        connectTimeout: 60000,
        // 可选,默认为60s
        readTimeout: 60000,
        // 开发者根据自身业务需要添加header字段
        header: {
          'Content-Type': 'application/json'
        }
      });

    // 处理响应结果。
    promise.then((data) => {
      if (data.responseCode === http.ResponseCode.OK) {
        console.info('Result:' + data.result);
        console.info('code:' + data.responseCode);
        this.message = JSON.stringify(data.result);
      }
    // }).catch((err) => {
    //   console.info('error:' + JSON.stringify(err));
    });
  }
}

second.ets:

// Second.ets
import router from '@ohos.router';
@Entry
@Component
struct Second {
  @State message: string = '你是不高兴'
  build() {
    Row() {
      Column() {
        Text(this.message)
          .fontSize(50)
          .fontWeight(FontWeight.Bold)
        Button() {
          Text('Back')
            .fontSize(25)
            .fontWeight(FontWeight.Bold)
        }
        .type(ButtonType.Capsule)
        .margin({
          top: 20
        })
        .backgroundColor('#ff9900')
        .width('80%')
        .height('5%') // 绑定点击事件
        .onClick(()=>{
          // 返回上一页面
          router.back()
        })
      }
      .width('100%')
    }
    .height('100%')
  }
}

------

C:\Users\rogabet\DevEcoStudioProjects\hwrogachat\entry\src\main\resources\base\media

放入logo.JPG 作为素材.

代码如上。

开发工具地址:

DevEco Studio-HarmonyOS Next Beta版-华为开发者联盟

-----------------

C:\Users\rogabet\DevEcoStudioProjects\hwrogachat\AppScope

app.json5应用公共配置信息

{
  "app": {
    "bundleName": "com.example.hwrogachat",
    "vendor": "example",
    "versionCode": 1000000,
    "versionName": "1.0.0",
    "icon": "$media:app_icon",
    "label": "$string:app_name"
  }
}
----------------------

C:\Users\rogabet\DevEcoStudioProjects\hwrogachat\entry\src\main

module.json5当前模块配置信息

{
  "module": {
    "name": "entry",
    "type": "entry",
    "description": "$string:module_desc",
    "mainElement": "EntryAbility",
    "deviceTypes": [
      "phone"
    ],
    "deliveryWithInstall": true,
    "installationFree": false,
    "pages": "$profile:main_pages",
    "abilities": [
      {
        "name": "EntryAbility",
        "srcEntry": "./ets/entryability/EntryAbility.ets",
        "description": "$string:EntryAbility_desc",
        "icon": "$media:layered_image",
        "label": "$string:EntryAbility_label",
        "startWindowIcon": "$media:startIcon",
        "startWindowBackground": "$color:start_window_background",
        "exported": true,
        "skills": [
          {
            "entities": [
              "entity.system.home"
            ],
            "actions": [
              "action.system.home"
            ]
          }
        ]
      }
    ],
    "extensionAbilities": [
      {
        "name": "EntryBackupAbility",
        "srcEntry": "./ets/entrybackupability/EntryBackupAbility.ets",
        "type": "backup",
        "exported": false,
        "metadata": [
          {
            "name": "ohos.extension.backup",
            "resource": "$profile:backup_config"
          }
        ],
      }
    ]
  }
}

--------------------------

C:\Users\rogabet\DevEcoStudioProjects\hwrogachat\entry\src\main\resources\base\profile

main_pages.json页面配置

{
  "src": [
    "pages/Index",
    "pages/second"
  ]
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

rogabet-note

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值