为了通过鸿蒙 @ohos.net.http 获取网络数据并在详情页展示产品的详细信息,包括图片、标题、价格、日期选择以及提交购买按钮,我们可以按照以下步骤进行开发。这个过程包括从主页面通过点击传递参数到详情页,在详情页获取网络数据并展示产品信息。

项目结构

- src/
    - main/
        - resources/
            - layouts/
                - ability_main.ets    // 主页面布局文件
                - product_detail.ets  // 详情页面布局文件
            - index.json  // 入口配置文件

1. 主页面(ability_main.ets

主页面展示产品列表,每个产品展示图片、标题、价格等信息,用户点击产品时将 Id 参数传递到详情页。

ability_main.ets
import ui;
import @ohos.net.http;
import ohos.agp.components.List;
import ohos.agp.components.Text;
import ohos.agp.components.Image;
import ohos.agp.utils.Uri;

@Entry
@Component
class MainPage extends Page {

    productList: Array<Product> = [];

    // 数据模型
    class Product {
        id: string;
        image: string;
        title: string;
        price: string;
    }

    // 网络请求函数,获取产品列表
    fetchData() {
        const url = "https://api.example.com/products";  // 替换成你的API地址

        http.get(url, (error, response) => {
            if (error) {
                console.log("请求失败: " + error);
                return;
            }

            if (response && response.body) {
                const products: Array<Product> = JSON.parse(response.body);
                this.updateList(products);
            }
        });
    }

    // 更新 List 组件的数据
    updateList(products: Array<Product>) {
        this.productList = products;
    }

    // 页面构建
    build() {
        Column {
            List {
                id: product_list
                width: "match_parent"
                height: "match_parent"
                itemProvider: productList

                itemContent {
                    Row {
                        Image {
                            id: image
                            width: "100px"
                            height: "100px"
                            src: Uri.parse("https://example.com/image.jpg")  // 动态加载图片
                        }
                        Column {
                            Text {
                                id: title
                                text: "Product Title"
                                fontSize: 16
                                marginTop: "10px"
                            }
                            Text {
                                id: price
                                text: "Price: $0"
                                fontSize: 14
                            }
                        }
                        Button {
                            text: "View Details"
                            width: "wrap_content"
                            onClick: () => {
                                // 页面跳转,传递 Id 参数
                                this.navigator.push("product_detail", {
                                    id: this.productList[0].id // 示例: 传递第一个产品的 Id
                                });
                            }
                        }
                    }
                }
            }
        }

        this.fetchData();  // 发起网络请求获取数据
    }
}

2. 详情页面(product_detail.ets

详情页面接收传递的 Id 参数,并根据该 Id 从网络获取产品的详细信息(图片、标题、价格、日期选择、提交购买按钮等)。

product_detail.ets
import ui;
import @ohos.net.http;
import ohos.agp.components.Text;
import ohos.agp.components.Image;
import ohos.agp.components.DatePicker;
import ohos.agp.components.Button;
import ohos.agp.utils.Uri;

@Entry
@Component
class ProductDetailPage extends Page {

    productId: string = ""; // 接收传递过来的 Id
    productDetails: Product = {
        image: "",
        title: "",
        price: "",
        description: "",
        date: ""
    };

    // 数据模型
    class Product {
        image: string;
        title: string;
        price: string;
        description: string;
        date: string;
    }

    // 网络请求函数,获取产品详细信息
    fetchProductDetails(id: string) {
        const url = `https://api.example.com/products/${id}`;  // 替换成你的API地址,传递Id参数

        http.get(url, (error, response) => {
            if (error) {
                console.log("请求失败: " + error);
                return;
            }

            if (response && response.body) {
                const product: Product = JSON.parse(response.body);
                this.updateProductDetails(product);
            }
        });
    }

    // 更新产品详细信息
    updateProductDetails(product: Product) {
        this.productDetails = product;
        this.update();  // 更新页面内容
    }

    // 页面构建
    build() {
        // 获取传递的 Id 参数
        this.productId = this.parameters.id;

        // 根据 Id 请求产品详细信息
        this.fetchProductDetails(this.productId);

        // 页面布局展示产品信息
        Column {
            Image {
                id: product_image
                src: Uri.parse(this.productDetails.image)
                width: "100%"
                height: "250px"
            }
            Text {
                id: product_title
                text: this.productDetails.title
                fontSize: 20
                marginTop: "10px"
            }
            Text {
                id: product_price
                text: "Price: " + this.productDetails.price
                fontSize: 18
                marginTop: "10px"
            }
            Text {
                id: product_description
                text: this.productDetails.description
                fontSize: 16
                marginTop: "10px"
            }
            DatePicker {
                id: purchase_date
                width: "match_parent"
                height: "50px"
                marginTop: "20px"
            }
            Button {
                text: "Buy Now"
                width: "wrap_content"
                height: "50px"
                marginTop: "20px"
                onClick: () => {
                    console.log("Product purchased on: " + this.productDetails.date);
                    // 提交购买操作,例如记录订单等
                }
            }
        }
    }
}

3. 配置入口文件(index.json

index.json 文件中配置应用的入口,并指定页面的目标为 ets

index.json
{
  "module": "com.example",
  "name": "MainPage",
  "type": "Ability",
  "target": "ets"
}

4. 详细说明

1. 主页面(MainPage
  • 使用 http.get 请求获取产品列表。
  • 每个产品展示图片、标题、价格等信息。
  • 点击每个产品的 "View Details" 按钮时,使用 this.navigator.push 跳转到详情页面,并传递该产品的 Id
2. 详情页面(ProductDetailPage
  • 接收主页面传递的 Id 参数,通过 http.get 请求该产品的详细信息(图片、标题、价格、描述等)。
  • 展示产品的详细信息,并提供日期选择器和提交购买按钮。
  • 点击 "Buy Now" 按钮时,记录购买信息并执行相关操作。
3. 日期选择器(DatePicker
  • 用户可以选择购买的日期,选择的日期会自动绑定到 purchase_date 中。
4. 购买按钮
  • 点击 "Buy Now" 按钮后,你可以在控制台查看购买的日期,实际应用中可以进一步处理订单逻辑(例如提交到服务器)。

5. 总结

通过这段代码,我们展示了如何在鸿蒙 ArkTS 中:

  • 在主页面展示产品列表,并通过点击跳转到详情页。
  • 通过传递 Id 参数,从网络获取产品的详细信息。
  • 在详情页面展示图片、标题、价格、描述等信息。
  • 提供日期选择器和购买按钮,供用户选择购买日期并提交购买。

这样实现了一个完整的从列表到详情页的流程,以及通过网络获取和展示数据的功能。

Logo

讨论HarmonyOS开发技术,专注于API与组件、DevEco Studio、测试、元服务和应用上架分发等。

更多推荐