Android 用Post方式開啟外部瀏覽器
- 可以使用data:html/text;+Html的URL ENCODE字串轉換成Uri,給外部瀏覽器使用。
- 利用html的form TAG帶入要POST參數內容,利用window.onload開啟時,自動執行submit。
- 這樣就可以post方式讓外部瀏覽器,去開啟一個網頁.
範例:
StringBuilder htmlUrl = new StringBuilder();
htmlUrl.append("<html><body onLoad=\"document.getElementById('form').submit()\" >");
htmlUrl.append("<form id=\"form\" method=\"post\" action=\"網址\">");
htmlUrl.append("<input type=\"hidden\" name=\"p1\" value=\"v1\">");
htmlUrl.append("<input type=\"hidden\" name=\"p2\" value=\"v2\">");
htmlUrl.append("</form>");
htmlUrl.append("</body></html>");
Uri uri = null;
try {
uri = Uri.parse("data:html/text,"
+ URLEncoder.encode(htmlUrl.toString(),"utf-8").replaceAll("\\+","%20"));
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
Intent it = new Intent(Intent.ACTION_VIEW, uri);
startActivity(it);