文章插图
?
Get请求实现在Get的Text点击事件,实现 ? [Get请求和请求成功之后和请求失败之后(参考 ? 线程管理开发指导需要将子线程切到Ui线程进行显示数据,代码如下
<pre class="prettyprint hljs JAVA" 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;">new Component.ClickedListener() {@Overridepublic void onClick(Component component) {OkHttpClient client = new OkHttpClient();Request.Builder requestBuilder = new Request.Builder();HttpUrl.Builder urlBuilder=HttpUrl.parse("http://web.juhe.cn/environment/air/cityair").newBuilder();//todo 接口链接urlBuilder.addQueryParameter("city","shanghai");//todo参数urlBuilder.addQueryParameter("Key","******");// todo 密钥 key自己申请requestBuilder.url(urlBuilder.build());Call call = client.newCall(requestBuilder.build());call.enqueue(new Callback() {@Overridepublic void onFailure(Call call, IOException e) {//todo 失败回调 需要回到主线程显示结果getUITaskDispatcher().asyncDispatch(new Runnable() {@Overridepublic void run() {textResult.setText(e.getMessage());}});}@Overridepublic void onResponse(Call call, Response response) throws IOException {if(response.isSuccessful()){String result = response.body().string();//todo 处理UI需要切换到UI线程处理getUITaskDispatcher().asyncDispatch(new Runnable() {@Overridepublic void</pre>
Post请求实现在Post的Text实现点击事件,具体参考? ?Android OkHttp常用详解?,代码如下<pre class="prettyprint hljs java" 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;">TextPost.setClickedListener(new Component.ClickedListener() {@Overridepublic void onClick(Component component) {OkHttpClient client = new OkHttpClient();FormBody body = new FormBody.Builder().add("key","*****")// todo 密钥 key自己申请.add("date","10/1")//todo 日期参数.build();Request request = new Request.Builder().url("http://v.juhe.cn/todayOnhistory/queryEvent.php").post(body).build();Call call = client.newCall(request);call.enqueue(new Callback() {@Overridepublic void onFailure(Call call, IOException e) {//todo 失败回调 需要回到主线程显示结果getUITaskDispatcher().asyncDispatch(new Runnable() {@Overridepublic void run() {textResult.setText(e.getMessage());}});}@Overridepublic void onResponse(Call call, Response response) throws IOException {if(response.isSuccessful()){String result = response.body().string();//处理UI需要切换到UI线程处理//todo 失败回调 需要回到主线程显示结果getUITaskDispatcher().asyncDispatch(new Runnable() {@Overridepublic void</pre>
运行效果具体代码如下java 代码
<pre class="prettyprint hljs java" 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;">package com.harmony.alliance.myApplication.slice;import com.harmony.alliance.myapplication.ResourceTable;import ohos.aafwk.ability.AbilitySlice;import ohos.aafwk.content.Intent;import ohos.agp.components.Component;import ohos.agp.components.Text;import okhttp3.*;import java.io.IOException;public class MainAbilitySlice extends AbilitySliceText TextGet,TextPost,textResult;@Overridepublic void onStart(Intent intent) {super.onStart(intent);super.setUIContent(ResourceTable.Layout_ability_main);TextGet=findComponentById(ResourceTable.Id_text_type_get);TextPost=findComponentById(ResourceTable.Id_text_type_Post);textResult=findComponentById(ResourceTable.Id_text_result);TextGet.setClickedListener(new Component.ClickedListener() {@Overridepublic void onClick(Component component) {OkHttpClient client = new OkHttpClient();Request.Builder requestBuilder = new Request.Builder();HttpUrl.Builder urlBuilder=HttpUrl.parse("http://web.juhe.cn/environment/air/cityair").newBuilder();//todo 接口链接urlBuilder.addQueryParameter("city","shanghai");//todo参数urlBuilder.addQueryParameter("Key","6fba58dc50a8e3d92e8a2f63d25c7750");// todo key 密钥requestBuilder.url(urlBuilder.build());Call call = client.newCall(requestBuilder.build());call.enqueue(new Callback() {@Overridepublic void onFailure(Call call, IOException e) {//todo 失败回调 需要回到主线程显示结果getUITaskDispatcher().asyncDispatch(new Runnable() {@Overridepublic void run() {textResult.setText(e.getMessage());}});}@Overridepublic void onResponse(Call call, Response response) throws IOException {if(response.isSuccessful()){String result = response.body().string();//todo 处理UI需要切换到UI线程处理getUITaskDispatcher().asyncDispatch(new Runnable() {@Overridepublic void run() {textResult.setText(result);}});}}});}});TextPost.setClickedListener(new Component.ClickedListener() {@Overridepublic void onClick(Component component) {OkHttpClient client = new OkHttpClient();FormBody body = new FormBody.Builder().add("key","7496ca7e5e12c408ef14e465c2bacc79")// todo 密钥.add("date","10/1")//todo 日期参数.build();Request request = new Request.Builder().url("http://v.juhe.cn/todayOnhistory/queryEvent.php").post(body).build();Call call = client.newCall(request);call.enqueue(new Callback() {@Overridepublic void onFailure(Call call, IOException e) {//todo 失败回调 需要回到主线程显示结果getUITaskDispatcher().asyncDispatch(new Runnable() {@Overridepublic void run() {textResult.setText(e.getMessage());}});}@Overridepublic void onResponse(Call call, Response response) throws IOException {if(response.isSuccessful()){String result = response.body().string();//处理UI需要切换到UI线程处理//todo 失败回调 需要回到主线程显示结果getUITaskDispatcher().asyncDispatch(new Runnable() {@Overridepublic void run() {textResult.setText(result);}});}}});}});}@Overridepublic void onActive() {super.onActive();}@Overridepublic void onForeground(Intent intent) {super.onForeground(intent);}}</pre>
推荐阅读
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- 翡翠|「吴中博物馆」珍藏的整套晚清翡翠,打破大众对老翡翠的固有认知
- 短发|孙艺珍脸圆了,「肩上短发」待产新造型美翻
- |你身上是否有管理者潜质?「上」
- iPhone 一键「降级」iOS 4,效果绝了
- 义务教育数学课程标准(「数字化学习」)
- 智能硬件|HarmonyOS 3再升级:墨水平板与手机一拉即合
- 18个步骤 关闭Harmony OS系统自带广告
- 智能手表|华为Watch 3用上HarmonyOS 3:轻轻一拉即可互联协同
- 医美|海报试用 | 新升级的「虫草粉底」在“养肤”路上越走越光彩了?
- 「Java」封装的实现,访问限定符、包