WebView内使用post加载url并添加header
来源:互联网 发布:常熟淘宝开店 编辑:程序博客网 时间:2024/06/07 07:16
最近项目内需求,使用WebView加载网页,加载网页的时候需要post参数去让网页生成数据,还要在头部添加特殊标识
WebView原生的api里边有post参数的api
//post是一个byte[] webview.postUrl(url,post) ;
添加header的Api有
//headers是一个mapwebview.loadUrl(url,headers);
这两个Api只能单独使用,不能两个同时使用;
纠结了很长时间,逛玩eoe, csdn各大网站搜索无果,
最后在stackoverflow 中找到 类似的问题,并且解决,
不废话了 贴代码
public class MyWebViewClient extends WebViewClient { @SuppressLint("NewApi") @Override public WebResourceResponse shouldInterceptRequest(WebView view, String url) { <pre name="code" class="java" style="font-size:12px; line-height: 16px; font-family: arial, sans-serif; white-space: nowrap;"> //在这个函数内,可以拦截WebView内的所有url,通过拦截url进行重新封装HttpUrlConnection 将header添加进连接,post参数写入//然后重新生成一个WebResourceResponse if(!TextUtils.isEmpty(params)){ String mParams = params ; params= null; try { URL mUrl=new URL(url); HttpURLConnection connection= (HttpURLConnection)mUrl.openConnection(); connection.setDoInput(true); connection.setDoOutput(true); connection.setUseCaches(false); connection.setRequestMethod("POST"); connection.setRequestProperty("resource", "android"); connection.setRequestProperty("client", "clientapp"); DataOutputStream os=new DataOutputStream(connection.getOutputStream()); os.writeBytes(mParams); os.flush(); params =null; return new WebResourceResponse("text/html", connection.getContentEncoding(), connection.getInputStream()); } catch (Exception e) { e.printStackTrace(); }finally{ params =null; } } return super.shouldInterceptRequest(view, url); }
打完收工................
0 0
- WebView内使用post加载url并添加header
- android webview内的post请求(点击按钮)添加header行不通
- Android 显示 WebView ,加载URL 时,向webview的 header 里面传递参数
- [iOS开发] 加载webView页面添加POST请求
- Android webview加载url
- (一)webView加载url
- 简单WebView加载url
- WebView加载URL配置
- WebView 应用内跳转url
- android使用WebView加载图片(URl)自适应屏幕
- Android中WebView中拦截所有请求并替换URL(支持AJAX的post请求类型)
- Android的webview加载本地html、本apk内html和远程URL
- Android的webview加载本地html、本apk内html和远程URL
- Android的webview加载本地html、本apk内html和远程URL
- Android的webview加载本地html、本apk内html和远程URL (简单明了我喜欢)
- Android的webview加载本地html、本apk内html和远程URL
- Android(3)——webview加载本地html、本apk内html和远程URL
- Android的webview加载本地html、本apk内html和远程URL
- 第13周上机实践项目6——体验文件操作(3-实战)
- Odoo中菜单打开时增加默认搜索条件
- 基于oracle数据库 emp表的一些练习
- 新 版 骗 分 导 论
- CentOS7.0下完美部署Solr 搜索引擎
- WebView内使用post加载url并添加header
- AIX 6.1安装python
- Runtime类
- 降低UIScrollView快速滑动后滚动的速度
- Android判断App是否在前台运行
- 【基础练习】链表排序,反转,划分,拷贝等
- IOS 网络请求返回数组解决方法
- 康华:浅谈软件可维护性问题
- ftp服务搭建