get
// 将参数附加到 URL String urlWithParams = HttpConfig.GET_USER_NAME + "?qrCodeContent=" + msg; // 构建请求 Request request = new Request.Builder() .url(urlWithParams) .addHeader("Authorization", authToken) .addHeader("satoken", satokenToken) .get() // 使用GET请求 .build();
post
// 构建JSON请求体 JSONObject jsonObject = new JSONObject(); try { jsonObject.put("qrCodeContent", scannedQrCodeContent); jsonObject.put("remark", remark); jsonObject.put("relation", relationship); jsonObject.put("isAuthorize", isAuthorize); Log.d(TAG, "构建的 JSON 数据: " + jsonObject.toString()); } catch (JSONException e) { e.printStackTrace(); Toast.makeText(getContext(), "数据构建失败", Toast.LENGTH_SHORT).show(); return; } // 将 JSON 转换为 RequestBody RequestBody body = RequestBody.create( jsonObject.toString(), MediaType.parse("application/json; charset=utf-8") ); // 构建 POST 请求,并添加请求头 Request request = new Request.Builder() .url(HttpConfig.USER_RELATION) .addHeader("Authorization", authToken) // 添加 Authorization 头部 .addHeader("satoken", satokenToken) // 添加 satoken 头部 .post(body) // 设置请求体为之前构建的 JSON .build(); Log.d(TAG, "发送的 POST 请求: " + request.toString()); Log.d(TAG, "请求体: " + jsonObject.toString());
标签:请求,get,jsonObject,addHeader,JSON,toString,post,Android From: https://www.cnblogs.com/lachesism/p/18397055