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();
  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值