当前位置: 首页 > news >正文

Android-okhttp调接口传参简单举例

步骤1:在主线程中创建thread调接口

        new Thread(new Runnable() {@Overridepublic void run() {getServiceList();}}).start();

步骤2:okhttp调接口

private void getServiceList(){Message msg = new Message();try{OkHttpClient okHttpClient = new OkHttpClient();FormBody.Builder formBodyBuilder = new FormBody.Builder();formBodyBuilder.add("token", "xxx").add("id", "idxxx");if (constellationArr != null) {for (String constellation : constellationArr) {formBodyBuilder.add("constellationArr", constellation);}}FormBody formBody = formBodyBuilder.build();///Request request = new Request.Builder().url("调接口url").post(formBody).build();// 创建 Call 对象并执行请求Call call = okHttpClient.newCall(request);Response response = call.execute();// 处理响应if (response.isSuccessful()) {// 响应成功String responseBody = response.body().string();// 在这里处理响应体JSONObject jsonObject = new JSONObject(responseBody);boolean success = jsonObject.getBoolean("success");String message = jsonObject.getString("msg");String obj = jsonObject.getString("obj");Log.e(TAG, obj);if (success) {// 使用 TypeToken 获取泛型类型TypeToken<ArrayList<ServicePersonShortBean>> typeToken = new TypeToken<ArrayList<ServicePersonShortBean>>() {};ArrayList<ServicePersonShortBean> servicePersonShortBeanList = gson.fromJson(obj, typeToken.getType());Bundle bundle = new Bundle();bundle.putSerializable("servicePersonShortBeanList", servicePersonShortBeanList);msg.setData(bundle);msg.what = 1;handler.sendMessage(msg);} else {Bundle bundle = new Bundle();if(message.equals("token找不到")){bundle.putString("message", "请您登录后查询");}else{bundle.putString("message", message);}msg.setData(bundle);msg.what = 2;handler.sendMessage(msg);}} else {// 响应失败// 在这里处理失败情况msg.what = 3;handler.sendMessage(msg);}// 记得关闭响应体response.close();} catch (IOException e) {if (e.toString().contains("request failed")) {msg.what = 4;handler.sendMessage(msg);} else {msg.what = 5;handler.sendMessage(msg);}e.printStackTrace();} catch (JSONException e) {e.printStackTrace();msg.what = 3;handler.sendMessage(msg);}}

步骤三:处理返回信息,在主线程中进行UI展示

// 在主线程中创建 HandlerHandler handler = new Handler(Looper.getMainLooper()) {@Overridepublic void handleMessage(Message msg) {super.handleMessage(msg);//根据信息编码及数据做出相对应的处理switch (msg.what) {case 1:{Bundle bundle = msg.getData();servicePersonShortBeanList = (ArrayList<ServicePersonShortBean>) bundle.getSerializable("servicePersonShortBeanList");showServicerList();new Thread(new Runnable() {@Overridepublic void run() {loadMoreData();}}).start();break;}case 2:{Bundle bundle = msg.getData();String message = bundle.getString("message");Toast.makeText(ServicePersonListActivity.this, message,Toast.LENGTH_SHORT).show();break;}case 3:Toast.makeText(ServicePersonListActivity.this, "获取xxx信息失败,请稍后再试!", Toast.LENGTH_SHORT).show();break;case 4:Toast.makeText(ServicePersonListActivity.this, "网络请求失败,请稍后重试", Toast.LENGTH_SHORT).show();break;case 5:Toast.makeText(ServicePersonListActivity.this, "网络异常,请检查您的网络\"", Toast.LENGTH_SHORT).show();break;case 6:{//加载更多Bundle bundle = msg.getData();ArrayList<ServicePersonShortBean> moreServicePersonShortBeanList = (ArrayList<ServicePersonShortBean>) bundle.getSerializable("moreServicePersonShortBeanList");servicePersonShortBeanList.addAll(moreServicePersonShortBeanList);servicePersonAdapter.notifyDataSetChanged();break;}default:break;}}};

注:链式调用写法举例

FormBody formBody = formBody = new FormBody.Builder().add("token", token).add("id", id).build();
http://www.lryc.cn/news/354607.html

相关文章:

  • 复习java5.26
  • 学 Python 具体能干什么?
  • 福昕PDF使用技巧
  • 条款8:了解各种不同意义的new和delete
  • windows 搭建 go开发环境
  • Android 布局中@NULL的使用和代码实现方式详解
  • 服务器数据恢复—同友存储raid5阵列上层虚拟机数据恢复案例
  • 我得近况说明
  • C语言——在头⽂件中#if、_STDC_等字⾏起什么作⽤?
  • 解密MySQL中的临时表:探究临时表的神奇用途
  • Go 语言简介 -- 高效、简洁与现代化编程的完美结合
  • 绝缘鞋计量校准周期多长时间合适?校验检测方法是什么?
  • python-13(案例讲解)
  • 【深度学习】最强算法之:人工神经网络(ANN)
  • Unity vscode在mac上的编译环境设置
  • 【Java】在高并发场景下,保证 Redis 缓存一致性的几种方案
  • GaussDB数据库的备份与恢复
  • 03-02-Vue组件之间的传值
  • 昂达固态硬盘数据恢复方法:全面解析与操作指南
  • C++的红黑树
  • Keras深度学习框架第二十九讲:在自定义训练循环中应用KerasTuner超参数优化
  • 手机App收集个人信息,用户是否有权拒绝?
  • 云下到云上,丽迅物流如何实现数据库降本50% | OceanBase案例
  • STM32无源蜂鸣器播放音乐
  • 【云原生】kubernetes中的认证、权限设置---RBAC授权原理分析与应用实战
  • 【Python设计模式04】策略模式
  • 私域用户画像分析:你必须知道的3个关键点!
  • 【MATLAB源码-第74期】基于matlab的OFDM-IM索引调制系统不同频偏误码率对比,对比OFDM系统。
  • 优于其他超导量子比特数千倍!猫态量子比特实现超过十秒的受控比特翻转时间
  • QtXlsx库编译使用