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

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

        new Thread(new Runnable() {
            @Override
            public 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展示

// 在主线程中创建 Handler
    Handler handler = new Handler(Looper.getMainLooper()) {
        @Override
        public 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() {
                        @Override
                        public 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();

相关推荐

  1. Android-okhttp接口简单举例

    2024-05-26 04:02:31       34 阅读
  2. Android Okhttp断点续

    2024-05-26 04:02:31       57 阅读
  3. Android OkHttp

    2024-05-26 04:02:31       34 阅读
  4. shell脚本调用http接口

    2024-05-26 04:02:31       23 阅读
  5. 深度学习策略简要总结

    2024-05-26 04:02:31       57 阅读
  6. SpringBoot和Vue接口调用方式

    2024-05-26 04:02:31       51 阅读
  7. Android】HttpURLConnection、OkHttp

    2024-05-26 04:02:31       52 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-05-26 04:02:31       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-05-26 04:02:31       106 阅读
  3. 在Django里面运行非项目文件

    2024-05-26 04:02:31       87 阅读
  4. Python语言-面向对象

    2024-05-26 04:02:31       96 阅读

热门阅读

  1. qt里的QPieSeries饼图

    2024-05-26 04:02:31       32 阅读
  2. Python 点云裁剪

    2024-05-26 04:02:31       37 阅读
  3. 汇编实现流水灯

    2024-05-26 04:02:31       33 阅读
  4. 入门Kotlin的学习之路经验总结

    2024-05-26 04:02:31       35 阅读
  5. icloud如何高效利用

    2024-05-26 04:02:31       32 阅读
  6. NDIS小端口驱动(六)

    2024-05-26 04:02:31       28 阅读
  7. unity 常用工具和代码

    2024-05-26 04:02:31       32 阅读
  8. docker image prune -f 命令什么用途

    2024-05-26 04:02:31       32 阅读
  9. dirsearch指令大全

    2024-05-26 04:02:31       41 阅读
  10. golang问题

    2024-05-26 04:02:31       28 阅读