1.鸿蒙权限
2.引入包
3.具体案例
一 网络权限
config.json修改节点:deviceConfig + reqPermissions
"deviceConfig": { "default": { "network": { "//": "鸿蒙的默认是 Https 访问模式,如果您的请求网址是 Http 开头的", "cleartextTraffic": true } } }
"reqPermissions": [ { "//": "访问internet", "name": "ohos.permission.INTERNET" } ],
二 引入包
build.gradle 增加包:okhttp + gson ,分别处理http和json格式化
implementation 'com.squareup.okhttp3:okhttp:3.10.0'
compile 'com.google.code.gson:gson:2.6.2'
三 具体案例
package com.example.myapplication3.bean; public class Product { private int id; private String productName; private float price; private String type; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getProductName() { return productName; } public void setProductName(String productName) { this.productName = productName; } public float getPrice() { return price; } public void setPrice(float price) { this.price = price; } public String getType() { return type; } public void setType(String type) { this.type = type; } @Override public String toString() { return "Product{" + "id=" + id + ", productName='" + productName + '\'' + ", price=" + price + ", type='" + type + '\'' + '}'; } }
package com.example.myapplication3.bean; import com.example.myapplication3.ResourceTable; import ohos.aafwk.ability.AbilitySlice; import ohos.agp.components.*; import ohos.hiviewdfx.HiLog; import ohos.hiviewdfx.HiLogLabel; import java.util.ArrayList; import java.util.List; public class ProductProvider extends BaseItemProvider { public final HiLogLabel hiLogLabel = new HiLogLabel(HiLog.LOG_APP, 0x00201, "ProductProvider"); private List<Product> data = new ArrayList<Product>(); private AbilitySlice slice; public ProductProvider(List<Product> data, AbilitySlice slice) { this.data = data; this.slice = slice; } @Override public int getCount() { return data != null ? data.size() : 0; } @Override public Object getItem(int i) { return data != null ? data.get(i) : null; } @Override public long getItemId(int i) { return i; } @Override public Component getComponent(int i, Component component, ComponentContainer componentContainer) { final Component cpt; HiLog.debug(hiLogLabel, "getComponent" + i); HiLog.debug(hiLogLabel, data.get(i).getProductName()); if (component == null) { cpt = LayoutScatter.getInstance(slice) .parse(ResourceTable.Layout_ability_main_listitem, null, false); } else { cpt = component; } Text id = (Text) cpt.findComponentById(ResourceTable.Id_text_id); Text name = cpt.findComponentById(ResourceTable.Id_text_name); id.setText(String.valueOf(data.get(i).getId())); name.setText(data.get(i).getProductName()); return cpt; } }
package com.example.myapplication3.slice; import com.example.myapplication3.Data.City; import com.example.myapplication3.ResourceTable; import com.example.myapplication3.bean.Product; import com.example.myapplication3.bean.ProductProvider; import com.google.gson.Gson; import com.google.gson.reflect.TypeToken; import ohos.aafwk.ability.AbilitySlice; import ohos.aafwk.content.Intent; import ohos.agp.components.*; import ohos.hiviewdfx.HiLog; import ohos.hiviewdfx.HiLogLabel; import okhttp3.*; import java.io.IOException; import java.lang.reflect.Type; import java.util.ArrayList; import java.util.List; public class MainAbilitySliceListContainer extends AbilitySlice { public final HiLogLabel hiLogLabel = new HiLogLabel(HiLog.LOG_APP, 0x00201, "MainAbilitySliceListContainer"); ListContainer listContainer; //http请求 public void DoGet() { String url = "http://localhost:5001/products"; OkHttpClient okHttpClient = new OkHttpClient().newBuilder().build(); final Request request = new Request.Builder().url(url).build(); Call call = okHttpClient.newCall(request); call.enqueue(new Callback() { @Override public void onFailure(Call call, IOException e) { HiLog.debug(hiLogLabel, "onFailure" + e.getMessage()); } @Override public void onResponse(Call call, Response response) throws IOException { String s = response.body().string(); List<Product> data = new Gson().fromJson(s, new TypeToken<List<Product>>() { }.getType()); HiLog.debug(hiLogLabel, "onResponse:size:" + data.size()); ProductProvider productProvider = new ProductProvider(data, MainAbilitySliceListContainer.this); //更新ui getUITaskDispatcher().asyncDispatch(new Runnable() { @Override public void run() { listContainer.setItemProvider(productProvider); productProvider.notifyDataChanged(); } }); } }); } @Override protected void onStart(Intent intent) { super.onStart(intent); super.setUIContent(ResourceTable.Layout_ability_main_listcontainer); listContainer = (ListContainer) findComponentById(ResourceTable.Id_listcontainer1); DoGet(); //http请求 } @Override protected void onActive() { super.onActive(); } @Override protected void onForeground(Intent intent) { super.onForeground(intent); } }
<?xml version="1.0" encoding="utf-8"?> <DirectionalLayout xmlns:ohos="http://schemas.huawei.com/res/ohos" ohos:height="match_parent" ohos:width="match_parent" ohos:alignment="center" ohos:orientation="vertical"> <ListContainer ohos:id="$+id:listcontainer1" ohos:height="match_content" ohos:layout_alignment="center" ohos:width="match_parent"> </ListContainer> </DirectionalLayout>
<?xml version="1.0" encoding="utf-8"?> <DirectionalLayout xmlns:ohos="http://schemas.huawei.com/res/ohos" ohos:height="match_parent" ohos:width="match_parent" ohos:alignment="center" ohos:orientation="vertical"> <DirectionalLayout ohos:height="match_parent" ohos:width="match_parent" ohos:alignment="center" ohos:orientation="horizontal"> <Text ohos:id="$+id:text_id" ohos:height="match_content" ohos:width="match_content" ohos:text="" ohos:text_size="40vp" /> <Text ohos:id="$+id:text_name" ohos:height="match_content" ohos:width="match_content" ohos:text="" ohos:text_size="40vp" /> </DirectionalLayout> <Component ohos:height="1vp" ohos:width="match_parent" ohos:background_element="gray"></Component> </DirectionalLayout>