意图和参数后推出的默认浏览器意图、浏览器、参数

2023-09-13 00:17:39 作者:池暝

可能重复:   How我可以开放的Andr​​oid浏览器与指定的POST参数?

我想做些什么 像这样的:

I would like to do something like this:

startActivity(新意图(Intent.ACTION_VIEW,Uri.parse(http://www.somepage.com?par1=val1&par2=val2));

startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.somepage.com?par1=val1&par2=val2"));

不过,我不想用,但还是用邮寄的参数。 我怎样才能做到这一点如上所述?

But I dont want to send the parameters with get but with post. How can I do this as described above?

许多在此先感谢, 纳瓦霍

Many thanks in advance, navajo

推荐答案

这是可以做到的,但在一个取巧的办法。

It can be done, but in a tricky way.

您可以创建一个自动少许的html文件的形式提交,读入到一个字符串替换PARAMS并将其嵌入在意向作为数据的URI,而不是URL。 有一对夫妇有点负面的东西,它只能直接调用默认的浏览器,而招将存储在浏览器历史记录,它将如果导航回出现。

You can create a little html file with an auto submit form, read it into a string, replace params and embed it in the intent as a data uri instead of a url. There are a couple little negative things, it only works calling default browser directly, and trick will be stored in browser history, it will appear if you navigate back.

下面是一个例子:

HTML文件(/ RES / RAW):

HTML file (/res/raw):

<html>
    <body onLoad="document.getElementById('form').submit()">
        <form id="form" target="_self" method="POST" action="${url}">
            <input type="hidden" name="param1" value="${value}" />
            ...
        </form>
    </body>
</html>

来源$ C ​​$ C:

Source code:

private void browserPOST() {
    Intent i = new Intent();
    // MUST instantiate android browser, otherwise it won't work (it won't find an activity to satisfy intent)
    i.setComponent(new ComponentName("com.android.browser", "com.android.browser.BrowserActivity"));
    i.setAction(Intent.ACTION_VIEW);
    String html = readTrimRawTextFile(this, R.raw.htmlfile);

    // Replace params (if any replacement needed)

    // May work without url encoding, but I think is advisable
    // URLEncoder.encode replace space with "+", must replace again with %20
    String dataUri = "data:text/html," + URLEncoder.encode(html).replaceAll("\\+","%20");
    i.setData(Uri.parse(dataUri));
    startActivity(i);
}

private static String readTrimRawTextFile(Context ctx, int resId) {
    InputStream inputStream = ctx.getResources().openRawResource(resId);

    InputStreamReader inputreader = new InputStreamReader(inputStream);
    BufferedReader buffreader = new BufferedReader(inputreader);
    String line;
    StringBuilder text = new StringBuilder();
    try {
        while ((line = buffreader.readLine()) != null) {
            text.append(line.trim());
        }
    }
    catch (IOException e) {
        return null;
    }
    return text.toString();
}