通过CustomTab或Chrome发布数据 [英] POST data via CustomTab or Chrome
本文介绍了通过CustomTab或Chrome发布数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想通过CustomTab或Chrome发送POST HTTP请求,然后最终显示页面.我进行了很多研究,但没有办法. 有办法吗? 可以通过Volley发送POST请求,然后最终在浏览器中显示响应?
I want send POST HTTP request via CustomTab or Chrome then show page finally. I many research but no way for it. is there a way? can send POST request via Volley then show response in browser finally?
推荐答案
我为此编写了一种解决方法.
I wrote a workaround for that.
小心,是肮脏的;)
步骤:
- 您需要创建一个带有表单的html文件
- 在其中添加与您需要传递给url的值相对应的输入字段
- 将此文件添加到您的资产文件夹
- 在android代码上:
- 读取文件的内容
- 将内容保存到外部缓存目录
- >>此步骤从现在开始是<< ,请按照以下说明进行操作(@Skotos关于如何使用自定义标签意图打开本地html的答案https://stackoverflow.com/a/60078339/2124387 )
- you need to create an html file with a form inside it
- add input fields to it corresponding to the values you need to pass to your url
- add this file to your asset folder
- on android code:
- read the content of the file
- save the content to the external cache directory
- >>THIS STEP IS FUNDAMENTAL<< from now on follow these instructions (@Skotos's answer on how to open a local html with a custom tab intent https://stackoverflow.com/a/60078339/2124387)
示例:
这是我在Assets文件夹中名为
form_template.html
的html文件:this is my html file called
form_template.html
in the assets folder:<html> <script> function submitForm() { document.getElementById("form").submit() } </script> <body onload="submitForm()"> <form id="form" action="{{url}}" method="{{method}}" enctype="{{enctype}}"> {{fields}} </form> </body> </html>
结束,这就是我向其动态传递网址和值的方式
end this is how i pass dynamically url and values to it
Map<String, String> values = ImmutableMap.of( "fooKey", "fooValue", // whatever you "barKey", "barValue" // need here ); try { File redirect = new File(activity.getExternalCacheDir(), "redirect.html"); // To get string from input stream look at here https://stackoverflow.com/a/16110044/2124387 String templateString = getStringFromInputStream(activity.getAssets().open("form_template.html")); List<String> inputFields = new ArrayList<>(); for (String key : values.keySet()) { inputFields.add(String.format("<input type=\"hidden\" name=\"%s\" value=\"%s\" />", key, values.get(key))); } templateString = templateString.replace("{{url}}", url); templateString = templateString.replace("{{method}}", method); // eg. "POST" templateString = templateString.replace("{{enctype}}", encodeType); // eg. "application/x-www-form-urlencoded" templateString = templateString.replace("{{fields}}", StringUtil.join("\n", inputFields)); FileOutputStream fileOutputStream = new FileOutputStream(redirect); fileOutputStream.write(templateString.getBytes()); Uri uri = FileProvider.getUriForFile(activity, BuildConfig.ApplicationId + ".provider", redirect); new Handler().postDelayed(redirect::delete, 5000); CustomTabsIntent.Builder builder = new CustomTabsIntent.Builder(); CustomTabsIntent customTabsIntent = builder.build(); customTabsIntent.intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)) customTabsIntent.launchUrl(this, packageName, url); } catch (IOException e) { e.printStackTrace(); }
这篇关于通过CustomTab或Chrome发布数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文