「Harmony OS」「JAVA UI」鸿蒙应用如何集成OKHttp网络三方库

准备资料接口准备准备get请求接

「Harmony OS」「JAVA UI」鸿蒙应用如何集成OKHttp网络三方库

文章插图
 
Post接口
「Harmony OS」「JAVA UI」鸿蒙应用如何集成OKHttp网络三方库

文章插图
 
如何集成在项目级别的build.gradle添加如下代码<pre class="prettyprint hljs Nginx" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 1.5em; font-size: 14px; line-height: 1.5em; word-break: break-all; overflow-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">allprojects {repositories {maven {url 'https://repo.huaweicloud.com/repository/maven/'}maven {url 'https://developer.huawei.com/repo/'</pre>在entry的build.gradle添加如下代码<pre class="prettyprint hljs delphi" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 1.5em; font-size: 14px; line-height: 1.5em; word-break: break-all; overflow-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">'io.openharmony.tpc.thirdlib:okgo:1.0.2'implementation 'io.openharmony.tpc.thirdlib:okrx:1.0.2'implementation 'io.openharmony.tpc.thirdlib:okrx2:1.0.2'implementation 'io.openharmony.tpc.thirdlib:okserver:1.0.2'</pre>在entry的config.json添加 权限 ? 代码如下
<pre class="prettyprint hljs lua" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 1.5em; font-size: 14px; line-height: 1.5em; word-break: break-all; overflow-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">"reqPermissions": [{"name": "ohos.permission.INTE.NET"</pre>注意事项如果接口是https的话需要在config.json的deviceConfig里添加如下代码
<pre class="prettyprint hljs lua" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 1.5em; font-size: 14px; line-height: 1.5em; word-break: break-all; overflow-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">"deviceConfig": {"default": {"network": {"cleartextTraffic": true</pre>界面实现在layout的xml中写三个Text标签,第一个Text用于触发get请求的事件,第二个Text用于触发Post请求的事件,第三个Text用于显示结果,代码和效果图如下
<pre class="prettyprint hljs vim" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 1.5em; font-size: 14px; line-height: 1.5em; word-break: break-all; overflow-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;"><?xml version="1.0" encoding="utf-8"?><DirectionalLayoutxmlns:ohos="http://schemas.huawei.com/res/ohos"ohos:height="match_parent"ohos:width="match_parent"ohos:alignment="horizontal_center"ohos:orientation="vertical"><Textohos:id="$+id:text_type_get"ohos:height="100vp"ohos:width="match_parent"ohos:text_alignment="center"ohos:layout_alignment="horizontal_center"ohos:text="Get请求"ohos:text_size="40vp"<Textohos:id="$+id:text_type_Post"ohos:height="100vp"ohos:text_alignment="center"ohos:width="match_parent"ohos:background_element="#ed6262"ohos:layout_alignment="horizontal_center"ohos:text="Post请求"ohos:text_size="40vp"<Textohos:id="$+id:text_result"ohos:height="match_parent"ohos:multiple_lines="true"ohos:text_alignment="center"ohos:width="match_parent"ohos:layout_alignment="horizontal_center"ohos:text="显示结果"ohos:text_size="40vp"</DirectionalLayout></pre>


推荐阅读