Ali*_*ani 8 java android android-volley chrome-custom-tabs android-customtabs
我想通过 CustomTab 或 Chrome 发送 POST HTTP 请求,然后最终显示页面。我进行了很多研究,但没有办法。有办法吗?可以通过 Volley 发送 POST 请求然后最终在浏览器中显示响应吗?
我为此写了一个解决方法。
小心,很脏;)
脚步:
例子:
这是我form_template.html在资产文件夹中调用的 html 文件:
<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>
Run Code Online (Sandbox Code Playgroud)
这就是我动态传递 url 和值的方式
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 /sf/answers/1127703111/
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();
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2333 次 |
| 最近记录: |