android webview拦截h5的接口请求并返回处理好的数据
android 可以通过 webview 的 shouldinterceptrequest 方法拦截到 h5 中的网络请求。这是一个 webviewclient 中的回调方法,允许开发者在 webview 发起网络请求时对其进行处理和修改。
具体使用方法如下:
你需要创建一个自定义的
webviewclient,并重写shouldinterceptrequest方法。在该方法中,你可以拦截 webview 发起的网络请求,并返回一个自定义的响应,或让请求继续。
该方法在 api 21 (android 5.0) 及更高版本中引入了两个重载方法:
shouldinterceptrequest(webview view, string url)(api 11)shouldinterceptrequest(webview view, webresourcerequest request)(api 21)
以下是代码示例:
// kotlin 代码示例
webview.webviewclient = object : webviewclient() {
// 对于 api 21 及更高版本
override fun shouldinterceptrequest(
view: webview,
request: webresourcerequest
): webresourceresponse? {
val url = request.url.tostring()
// 这里你可以判断 url,并根据需要拦截或修改请求
if (url.contains("your_target_url")) {
// 可以在这里做一些处理,例如替换请求或返回本地数据
val inputstream = ... // 自定义输入流
return webresourceresponse("text/html", "utf-8", inputstream)
}
// 不拦截,继续请求
return super.shouldinterceptrequest(view, request)
}
// 对于 api 11 到 api 20 的设备
override fun shouldinterceptrequest(
view: webview,
url: string
): webresourceresponse? {
// 这里处理逻辑类似于上面的代码
if (url.contains("your_target_url")) {
// 自定义处理逻辑
val inputstream = ...
return webresourceresponse("text/html", "utf-8", inputstream)
}
return super.shouldinterceptrequest(view, url)
}
}
在 shouldinterceptrequest 方法中,你可以返回一个 webresourceresponse 对象,来改变或替换原始的网络请求,也可以通过默认实现让请求继续执行。
获取网络接口请求的数据(如 api 请求的响应),然后将其返回给 h5,
有以下几种方式可以考虑:
1. 拦截请求并手动发起请求
你可以通过 shouldinterceptrequest 方法拦截 webview 的 api 请求,自己在 java 或 kotlin 中使用 httpurlconnection 或 okhttp 发起网络请求,处理完响应后,再将响应数据传递给 h5。
示例代码:
webview.webviewclient = object : webviewclient() {
override fun shouldinterceptrequest(
view: webview,
request: webresourcerequest
): webresourceresponse? {
val url = request.url.tostring()
// 判断是否是需要拦截的接口请求
if (url.contains("your_api_endpoint")) {
// 通过 okhttp 或 httpurlconnection 发起请求
val response = fetchapidata(url)
// 将获取的响应内容传递给 h5
view.post {
view.evaluatejavascript("javascript:handleapiresponse('${response}')", null)
}
// 返回一个空响应或自定义内容
return webresourceresponse("application/json", "utf-8", null)
}
return super.shouldinterceptrequest(view, request)
}
// 使用 okhttp 发起网络请求(可以根据你的需求选择合适的网络库)
private fun fetchapidata(url: string): string {
// 简单 okhttp 请求示例
val client = okhttpclient()
val request = request.builder().url(url).build()
client.newcall(request).execute().use { response ->
return response.body?.string() ?: ""
}
}
}
在这个例子中,shouldinterceptrequest 会拦截指定的网络请求,然后用 okhttp 发起请求,并将获取的响应数据通过 evaluatejavascript 方法传回到 webview 中的 h5 页面。
2. 通过 webview 和 h5 的 javascript 通信
另一种方式是通过 webview 的 addjavascriptinterface 方法,将 android 代码和 h5 进行通信。当 android 发起请求后,可以通过接口将数据传递给 h5。
在 h5 中定义一个 javascript 函数,用于接收数据:
function handleapiresponse(response) {
console.log("api response: ", response);
// 这里可以对响应进行处理,渲染到页面等
}
在 android 端:
webview.addjavascriptinterface(object {
@javascriptinterface
fun senddatatoh5(data: string) {
// 发送数据到 h5
webview.evaluatejavascript("javascript:handleapiresponse('$data')", null)
}
}, "androidinterface")
webview.webviewclient = object : webviewclient() {
override fun shouldinterceptrequest(
view: webview,
request: webresourcerequest
): webresourceresponse? {
val url = request.url.tostring()
if (url.contains("your_api_endpoint")) {
val response = fetchapidata(url)
// 通过 javascript 接口传递数据到 h5
view.post {
view.evaluatejavascript("javascript:androidinterface.senddatatoh5('${response}')", null)
}
return webresourceresponse("application/json", "utf-8", null)
}
return super.shouldinterceptrequest(view, request)
}
}
这种需要考虑到h5 发过来的网络接口请求类型吗?post 还是 get
是的,在使用 shouldinterceptrequest 拦截 h5 发过来的网络请求时,需要考虑请求的类型(get 或 post)。这是因为 get 和 post 请求的处理方式不同,尤其是 post 请求,它带有请求体(body)数据,而 get 请求通常只包含 url 参数。
shouldinterceptrequest 可以帮助你拦截请求,但对于 post 请求的处理,需要特别注意提取和使用请求体。
如何区分 get 和 post 请求?
在 api 21 及更高版本中,shouldinterceptrequest 方法中的 webresourcerequest 对象包含了更多信息,可以通过它的 getmethod() 方法来区分请求类型。
override fun shouldinterceptrequest(
view: webview,
request: webresourcerequest
): webresourceresponse? {
val url = request.url.tostring()
val method = request.method // 获取请求方法,如 get 或 post
// 根据请求类型处理
if (method == "post") {
// 获取 post 请求的 body(在 android 的 webview 中,直接获取 post body 是不支持的,除非使用特定工具)
// 自定义逻辑处理 post 请求
} else if (method == "get") {
// 处理 get 请求
}
return super.shouldinterceptrequest(view, request)
}
处理 post 请求
android 的 shouldinterceptrequest 本身不直接提供获取 post 请求体的功能。不过你可以使用其他方式来处理,例如通过在 js 中提前将请求体通过某种方式传递给 android。
方法 1:使用 evaluatejavascript 传递 post 请求数据
你可以让 h5 页面在发出 post 请求前,通过 javascript 提前将 post 请求的数据发送到 webview。然后 android 端可以拦截并处理这些数据。
在 h5 中拦截和传递 post 请求数据:
function interceptandsendpostdata(url, data) { // 调用 android 接口,将数据传递给 webview androidinterface.sendpostdata(url, json.stringify(data)); // 继续发起 post 请求 fetch(url, { method: "post", body: json.stringify(data), headers: { "content-type": "application/json" } }).then(response => response.json()) .then(data => console.log(data)); }在 android 中接收 post 数据:
webview.addjavascriptinterface(object { @javascriptinterface fun sendpostdata(url: string, data: string) { // 这里可以处理传递过来的 post 请求数据 log.d("webview", "post 请求 url: $url, 数据: $data") } }, "androidinterface")
方法 2:通过自定义网络层发送 post 请求
另一种方式是在 shouldinterceptrequest 中拦截 post 请求后,手动发起 http 请求,处理请求体和响应数据。虽然 shouldinterceptrequest 本身不直接提供请求体,但你可以在 h5 页面中通过 javascript 提前将 post 请求体传递给 android,或者通过其他网络拦截方式处理(例如使用 okhttp 的拦截器)。
override fun shouldinterceptrequest(
view: webview,
request: webresourcerequest
): webresourceresponse? {
val url = request.url.tostring()
val method = request.method // 获取请求方法,如 get 或 post
if (method == "post") {
// 自定义发起 http 请求,处理 post 请求体和响应
val response = performcustomhttppost(url, request)
return webresourceresponse("application/json", "utf-8", response)
}
return super.shouldinterceptrequest(view, request)
}
fun performcustomhttppost(url: string, request: webresourcerequest): inputstream {
// 使用 okhttp 或 httpurlconnection 发送自定义 post 请求
val client = okhttpclient()
val requestbody = ... // 构建请求体
val request = request.builder()
.url(url)
.post(requestbody)
.build()
val response = client.newcall(request).execute()
return response.body?.bytestream() ?: bytearrayinputstream(bytearray(0))
}
小结
- get 请求:你可以直接通过 url 和请求头信息来拦截和处理。
- post 请求:需要特别处理请求体数据。你可以通过 javascript 提前将 post 请求数据传递给 webview,或通过手动发起 http 请求处理 post 请求及其响应。
shouldinterceptrequest 能判断出来网络请求的类型吗?比如script xhr 等
shouldinterceptrequest 本身并不能直接判断网络请求的具体类型(如 script、xhr 等),因为它没有提供一个字段明确表明请求是某种类型的资源(如 javascript 文件、xhr 请求等)。不过,你可以通过请求的 url 和 请求头信息 来推断请求的类型。
在 api 21 及以上的 android 版本中,webresourcerequest 对象提供了丰富的信息,包括请求的 url、请求方法(get、post 等)、请求头等,可以根据这些信息推断请求类型。
如何通过 url 和请求头判断请求类型?
通过 url 后缀:
- 如果请求的 url 以
.js结尾,通常可以认为这是一个script请求。 - 如果 url 中包含
/api/或类似的标识符,可能是xhr请求。 - css 通常以
.css结尾,图片文件以.jpg、.png、.gif结尾等。
- 如果请求的 url 以
通过请求头: 你可以通过请求的头部来推断请求的类型,例如
accept头部通常指示客户端期望接收到的数据类型。accept: application/json常用于xhr请求。accept: text/css表示请求 css 文件。accept: application/javascript或accept: text/javascript用于 javascript 文件请求。
代码示例:如何通过 url 和请求头推断请求类型
override fun shouldinterceptrequest(
view: webview,
request: webresourcerequest
): webresourceresponse? {
val url = request.url.tostring()
val headers = request.requestheaders
val acceptheader = headers["accept"] // 获取 accept 头
// 判断是否为脚本请求 (script)
if (url.endswith(".js") || acceptheader?.contains("application/javascript") == true) {
log.d("webview", "拦截到脚本请求: $url")
// 在此可以拦截或修改脚本请求
}
// 判断是否为 xhr 请求 (通过 url 或 accept 头判断)
if (url.contains("/api/") || acceptheader?.contains("application/json") == true) {
log.d("webview", "拦截到 xhr 请求: $url")
// 在此可以拦截或修改 xhr 请求
}
// 判断是否为 css 请求
if (url.endswith(".css") || acceptheader?.contains("text/css") == true) {
log.d("webview", "拦截到 css 请求: $url")
// 在此可以拦截或修改 css 请求
}
// 其他资源类型的请求
log.d("webview", "其他类型的请求: $url")
return super.shouldinterceptrequest(view, request)
}
常见资源类型的判断方式
- javascript 文件请求 (
script):- 通过 url 判断:
url.endswith(".js") - 通过
accept头判断:application/javascript或text/javascript
- 通过 url 判断:
- xhr 请求:
- 通过 url 规则判断:如果 url 中包含
/api/、/ajax/或其他常见的 api 路径。 - 通过
accept头判断:application/json
- 通过 url 规则判断:如果 url 中包含
- css 文件请求:
- 通过 url 判断:
url.endswith(".css") - 通过
accept头判断:text/css
- 通过 url 判断:
- 图片文件请求:
- 通过 url 判断:
url.endswith(".jpg")、.png、.gif等 - 通过
accept头判断:image/*
- 通过 url 判断:
- html 页面请求:
- 通过
accept头判断:text/html
- 通过
小结
虽然 shouldinterceptrequest 没有直接提供资源类型的字段,但你可以通过 url 的格式和请求头(特别是 accept 头)来推断请求的类型,从而拦截特定的资源请求。
script xhr font png preflight 分别是什么,应该怎么判断呢?都可以通过 val acceptheader = headers["accept"] 来进行判断吗?
accept 头在 http 请求中指示客户端希望接收的响应数据类型,它确实能在一些情况下帮助我们判断请求的资源类型。但并不是所有类型的请求都会明确使用 accept 头。因此,我们需要结合 url 和 请求头 的其他信息(如 content-type、access-control-request-method 等)来进行判断。
1. script(javascript 请求)
javascript 请求是用来加载 .js 文件的资源请求。
判断依据:
- url 后缀:通常
.js文件后缀。 accept头:application/javascript或text/javascript。
if (url.endswith(".js") || acceptheader?.contains("application/javascript") == true || acceptheader?.contains("text/javascript") == true) {
log.d("webview", "拦截到 javascript 请求: $url")
}
2. xhr 请求(xmlhttprequest / fetch 请求)
xhr 请求通常用于 ajax 请求或使用 fetch api 的异步网络请求。
判断依据:
- url 规则:api 请求通常有特定路径,例如
/api/、/ajax/等。 accept头:application/json(如果返回 json 数据)。- 请求头:xhr 请求会带有
x-requested-with: xmlhttprequest头部(但不是所有情况都有此头部)。
if (url.contains("/api/") || acceptheader?.contains("application/json") == true || headers["x-requested-with"] == "xmlhttprequest") {
log.d("webview", "拦截到 xhr 请求: $url")
}
3. font 请求(字体文件请求)
字体文件通常以 .woff、.woff2、.ttf、.otf 等后缀结尾。
判断依据:
- url 后缀:
.woff、.woff2、.ttf、.otf。 accept头:font/*或application/font-woff。
if (url.endswith(".woff") || url.endswith(".woff2") || url.endswith(".ttf") || url.endswith(".otf") || acceptheader?.contains("font/") == true) {
log.d("webview", "拦截到字体文件请求: $url")
}
4. png 请求(图片请求)
图片请求通常包括 .png、.jpg、.gif 等格式。
判断依据:
- url 后缀:图片文件通常以
.png、.jpg、.jpeg、.gif结尾。 accept头:image/*。
if (url.endswith(".png") || url.endswith(".jpg") || url.endswith(".jpeg") || url.endswith(".gif") || acceptheader?.contains("image/") == true) {
log.d("webview", "拦截到图片请求: $url")
}
5. preflight 请求
preflight 请求是 cors 请求的一部分,通常在跨域 post、put、delete 请求前发送,以确定服务器是否允许该请求。preflight 请求使用 options 方法发起。
判断依据:
- http 方法:
options(preflight 请求是options方法的请求)。 - 请求头:包含
access-control-request-method和access-control-request-headers。
if (request.method == "options" && headers.containskey("access-control-request-method")) {
log.d("webview", "拦截到 preflight 请求: $url")
}结合 url 和 accept 头进行判断
总结不同请求类型的判断依据:
| 请求类型 | url 后缀 | accept 头 | 其他请求头信息 |
|---|---|---|---|
| script (javascript) | .js | application/javascript, text/javascript | |
| xhr (ajax) | /api/, /ajax/ | application/json | x-requested-with: xmlhttprequest |
| font (字体) | .woff, .woff2, .ttf, .otf | font/*, application/font-woff | |
| image (图片) | .png, .jpg, .jpeg, .gif | image/* | |
| preflight (cors) | options 方法,access-control-request-method |
小结
accept头可以帮助判断请求类型,但并不是所有请求都明确使用它。- 根据 url 后缀、请求方法、其他请求头(如
x-requested-with、access-control-request-method)结合判断能更准确识别不同类型的请求。
返回的响应不符合预期的格式,怎么处理
出现 axioserror: network error, code: err_network 错误,通常是因为 webview 在处理拦截的网络请求时,返回的响应不符合预期的格式,或者某些必要的头信息缺失。这里有几个常见的原因及解决办法:
问题可能的原因:
响应流的格式或处理错误: 你正在返回
response.body()?.bytestream(),但是你还需要确保整个webresourceresponse对象是正确配置的,尤其是内容类型、编码格式以及响应头信息。任何一项出错都可能导致 h5(例如通过 axios)认为网络请求失败。响应头缺失或不完整:
webresourceresponse需要提供一些关键的响应头,比如content-type和content-length,这些头在网络请求处理中是必需的。h5 请求的 axios 需要明确知道响应的长度和类型,否则会认为响应无效。返回的流不完整或被提前关闭: 如果返回的流有问题,比如它被提前关闭或者有其他异常情况,可能导致 axios 在处理时报错。
如何正确返回自定义响应
确保你使用 webresourceresponse 构建响应时,包括了所有必要的头信息和流数据。
修改代码示例
override fun shouldinterceptrequest(
view: webview,
request: webresourcerequest
): webresourceresponse? {
val url = request.url.tostring()
if (url.contains("your_api_endpoint")) {
// 发起网络请求获取数据
val response = fetchapidata(url)
// 处理返回的响应流
val bytestream = response.body?.bytestream() ?: bytearrayinputstream(bytearray(0))
val contenttype = response.header("content-type", "application/json") // 设置默认内容类型
val contentlength = response.header("content-length", "-1").tolong() // 设置内容长度
// 构建 webresourceresponse 并返回给 webview
return webresourceresponse(
contenttype, // 内容类型
"utf-8", // 编码格式
200, // http 状态码
"ok", // 状态描述
mapof( // 响应头
"access-control-allow-origin" to "*",
"content-length" to contentlength.tostring()
),
bytestream // 响应内容的输入流
)
}
return super.shouldinterceptrequest(view, request)
}
// 使用 okhttp 发起网络请求
private fun fetchapidata(url: string): response {
val client = okhttpclient()
val request = request.builder().url(url).build()
return client.newcall(request).execute() // 直接返回 response 对象
}
关键修改点:
正确设置内容类型和编码:通过
response.header("content-type")获取原始响应的content-type,并传递给webresourceresponse。通常是application/json。设置
content-length:通过response.header("content-length")获取响应内容的长度,并将其传递给 webview。如果缺少content-length,某些 h5 请求库(如 axios)可能会报错。返回有效的响应流:你需要确保
response.body()?.bytestream()是有效的。如果网络请求失败或流为空,你可以返回一个空的bytearrayinputstream()。设置状态码和状态描述:如果请求成功,确保返回
200 ok,否则应根据具体情况返回相应的状态码和描述。
检查点
确保响应流有效:确保
response.body()?.bytestream()真的有数据,否则 h5 端会认为是无效的响应。你可以通过调试查看响应流的状态。正确设置头信息:特别是
content-type和content-length,它们是网络请求处理的关键。axios 依赖这些头信息来解析响应。错误处理:如果在网络请求过程中发生错误,应返回合适的错误响应,比如
404 not found或500 internal server error,而不是直接返回空的响应流。
调试建议
- 通过日志调试输出 okhttp 请求的状态,确保
fetchapidata函数返回的response对象中包含有效的数据。 - 在 axios 端检查返回的响应头,确保
content-type、content-length等信息是正确的。
shouldinterceptrequest 拦截post请求的时候,如何获取post 请求的 body 数据?
在 android 的 webviewclient 中通过 shouldinterceptrequest 拦截 post 请求并构建自定义的 http 请求时,首先需要手动构建请求体(如从 webresourcerequest 中获取必要的请求信息),然后使用 okhttp 或 httpurlconnection 来发送这个 post 请求。
webresourcerequest 不直接提供 post 请求体,因此要获取并手动构建 post 请求体。这可以通过 requestheaders 来构建,或在前端(h5)发送时提前传递 post 数据。
处理 post 请求并构建请求体的完整示例:
override fun shouldinterceptrequest(
view: webview,
request: webresourcerequest
): webresourceresponse? {
val url = request.url.tostring()
val method = request.method
if (method == "post") {
log.d("webview", "拦截到 post 请求: $url")
// 自定义处理 post 请求,构建请求体并发送
val responsestream = performcustomhttppost(url, request)
// 返回自定义的 webresourceresponse
return webresourceresponse(
"application/json", // 假设返回的是 json 响应
"utf-8",
200, // http 状态码
"ok", // http 状态描述
mapof("access-control-allow-origin" to "*"), // 响应头
responsestream // 响应数据的输入流
)
}
return super.shouldinterceptrequest(view, request)
}
// 自定义处理 post 请求的逻辑
fun performcustomhttppost(url: string, request: webresourcerequest): inputstream {
// 构建 post 请求体(假设 h5 端发送的 post 数据是 json 格式)
val postdata = getpostdata(request) // 假设这里可以提取请求体数据
// 日志记录请求体数据
log.d("webview", "post 请求数据: $postdata")
// 使用 okhttp 发送自定义的 post 请求
val client = okhttpclient()
val requestbody = requestbody.create(
mediatype.parse("application/json; charset=utf-8"), // 假设请求体是 json 数据
postdata ?: "" // post 请求的数据
)
val customrequest = request.builder()
.url(url)
.post(requestbody)
.build()
// 发起请求并返回响应流
val response = client.newcall(customrequest).execute()
log.d("webview", "http 响应码: ${response.code()}") // 日志记录响应状态码
return response.body?.bytestream() ?: bytearrayinputstream(bytearray(0))
}
// 获取 post 数据(需要通过前端传递或其他方式获取)
fun getpostdata(request: webresourcerequest): string? {
// webview 无法直接获取 post body 数据,需要前端配合通过 evaluatejavascript 或其他方式传递
// 假设我们从 requestheaders 中获取部分数据(实际可以根据你的需求进行修改)
val contenttype = request.requestheaders["content-type"]
log.d("webview", "content-type: $contenttype")
// 这里返回模拟的 post 数据,实际情况下需要处理真实的请求体
return "{ \"key\": \"value\" }"
}
关键点: 获取请求体数据:android webview 本身不提供获取 post 请求体的直接方式,需要通过前端协作(如 evaluatejavascript)来传递请求体,或者自行处理模拟请求体。 手动构建 post 请求体:使用 okhttp 的 requestbody.create() 方法手动构建 post 请求体。 记录日志: 通过 log.d() 记录拦截的 url 和请求体数据,便于调试。 记录 http 响应状态码,查看请求是否成功。 处理异常情况:如果响应体为空,返回一个空的 bytearrayinputstream(),以确保 webview 不会崩溃。 关于获取真实的 post 请求体 android 的 webview 没有内置机制直接获取 post 请求体,因此需要通过 javascript 与 android 通信,在 h5 端主动将 post 数据发送给 webview。例如:
// 在 h5 端获取 post 请求体并传递给 android
function sendpostdatatoandroid(data) {
if (window.androidinterface) {
window.androidinterface.sendpostdata(json.stringify(data));
}
}
然后在 android 端通过 addjavascriptinterface 接收数据:
@javascriptinterface
fun sendpostdata(data: string) {
// 处理从 h5 传递过来的 post 数据
log.d("webview", "收到的 post 数据: $data")
}
最后
其实获取post请求体参数内容上述的方法可以尝试一下,当然,还有一种更简单的取巧的方法,如果h5 post请求数量不是很多的话,可以和h5沟通好,直接把请求数据放在请求的url中,中间通过特定字符@隔开,然后我们拿到后进行处理,
// 判断是否为 xhr 请求 (通过 url 或 accept 头判断)
if ((url.contains("/api/") || acceptheader?.contains("application/json") == true ||
headers["x-requested-with"] == "xmlhttprequest") && !url.endswith("html")) {
logutils.i( "拦截到 xhr 请求: $url")
// 在此可以拦截或修改 xhr 请求
var respone: response? = null
if(method == "post"){
val params = url.split("@")
val test = params[0] // 获取 post 请求的 body 数据
val postdata = urldecoder.decode(params[1], standardcharsets.utf_8.tostring())
logutils.i("postdata = $postdata")
respone = fetchapidata2(test,method,postdata)
}else if(method == "get"){
respone = fetchapidata(url,method,null)
}
val bytestream = respone?.body()?.bytestream() ?: bytearrayinputstream(bytearray(0))
val contenttype = respone?.header("content-type", "application/json") // 设置默认内容类型
val contentlength = respone?.header("content-length", "-1")?.tolong() // 设置内容长度
logutils.i("fetchapidata respone = ${respone.tostring()}")
return webresourceresponse(
contenttype, // 内容类型
"utf-8", // 编码格式
200, // http 状态码
"ok", // 状态描述
mapof( // 响应头
"access-control-allow-origin" to "*",
"content-length" to contentlength.tostring()
),
bytestream // 响应内容的输入流
)
// return webresourceresponse("application/json", "utf-8",respone)
} private fun fetchapidata2(url: string, method: string, postdata: string?): response{
logutils.i("fetchapidata2 = $url + $method + $postdata")
val client = unsafeokhttpclient.unsafeokhttpclient.build()
val requestbody = requestbody.create(
mediatype.parse("application/json; charset=utf-8"), // 假设请求体是 json 数据
postdata ?: "" // post 请求的数据
)
val requestbuilder = request.builder()
.url(url)
.post(requestbody)
.build()
return client.newcall(requestbuilder).execute()
} // 根据请求类型进行处理
private fun fetchapidata(url: string, method: string, postdata: map<string, string>?): response {
logutils.i("fetchapidata = $url + $method + $postdata")
val client = unsafeokhttpclient.unsafeokhttpclient.build()
val requestbuilder = request.builder().url(url)
// 根据请求类型构建请求
if (method == "post" && postdata != null) {
val formbody = formbody.builder()
postdata.foreach { (key, value) -> formbody.add(key, value) }
requestbuilder.post(formbody.build())
}
val request = requestbuilder.build()
return client.newcall(request).execute() // 直接返回 response 对象
}总结
到此这篇关于android webview拦截h5的接口请求并返回处理好的数据的文章就介绍到这了,更多相关android webview拦截h5接口请求内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!
发表评论