How can I open android browser with specified POST

2020-01-24 09:40发布

I my application, I need to open a link in Android Browser. This page can receive some data just via POST. Could I add these parameters (data) to the intent which start the browser?

Do you know if this is possible? If it is, could you give my a hint?

4条回答
放荡不羁爱自由
2楼-- · 2020-01-24 10:10

I believe that there is a little misconception in the question. What is missing is the purpose that you need of the POST instead of GET.

If you admit I will make few assumptions that could be common in this context:

  • You need to hide the actual variables from history
  • You Need some interaction with server before user gets control
  • You cannot control the server itself and it uses (on purpose) POST requests

Any of those options or requirements implies some additional processing that is distinct from usual browser use case (which is to give full control over the processing and interaction). It seems that you are actually asking for Machine to Machine (M2M) communication with eventual HTML output.

If that renders to be true, then using some OKHttp, HTTPURLConnection, Apache HTTP Client, etc. is the right choice. Rather then invoking the browser via Intent, that has close to zero messaging capabilities (just fire and forget - in case of http:...). It actually requires some analysis of the data flow (sequence diagram might help) and then engineering of that process into M2M or assisted M2M interaction.

If the server you are using to interact with is your own, then why you do not create some REST/JSON/SOAP or other M2M API to make remote method calls (RPC/RMI/...). It is not that complex as it might look (e.g.: http://coreymaynard.com/blog/creating-a-restful-api-with-php/ or https://docs.phalconphp.com/pt/latest/reference/tutorial-rest.html)

Alternative would be to make your M2M interaction rather on your APP server, because then the eventual changes to the BE server data flow could be reflected without app change. By this you would be actually shifting the M2M communication partially to server side.

Note: Using application to interact with 3rd party servers might have some legal implications. In fact those server might not allow other use than through browser (human detection = captcha, User-Agent detection). In such case you have to negotiate with the server owner.

查看更多
时光不老,我们不散
3楼-- · 2020-01-24 10:11

Intents sent to the browser can contain more than just a URL. In older versions of android it was possible to package extra POST data in the intent, in newer versions that capability is gone but one can send extra header data for a GET (which can be just about anything representable as a string) in the intent delivered to the browser.

查看更多
男人必须洒脱
4楼-- · 2020-01-24 10:11
try{
        String finalUrl = "javascript:" +
                "var to = 'http://the_link_you_want_to_open';" +
                "var p = {param1:'"+your_param+"',param2:'"+your_param+"'};" +
                "var myForm = document.createElement('form');" +
                "myForm.method='post' ;" +
                "myForm.action = to;" +
                "for (var k in p) {" +
                "var myInput = document.createElement('input') ;" +
                "myInput.setAttribute('type', 'text');" +
                "myInput.setAttribute('name', k) ;" +
                "myInput.setAttribute('value', p[k]);" +
                "myForm.appendChild(myInput) ;" +
                "}" +
                "document.body.appendChild(myForm) ;" +
                "myForm.submit() ;" +
                "document.body.removeChild(myForm) ;";


        Uri uriUrl = Uri.parse(finalUrl);
        Intent browserIntent = new Intent(Intent.ACTION_VIEW);
        PackageManager packageManager = this.getPackageManager();
        browserIntent.setData(uriUrl);
        List<ResolveInfo> list = packageManager.queryIntentActivities(browserIntent, 0);
        for (ResolveInfo resolveInfo : list) {
            String activityName = resolveInfo.activityInfo.name;
            if (activityName.contains("BrowserActivity")) {
                browserIntent =
                        packageManager.getLaunchIntentForPackage(resolveInfo.activityInfo.packageName);
                ComponentName comp =
                        new ComponentName(resolveInfo.activityInfo.packageName, resolveInfo.activityInfo.name);
                browserIntent.setAction(Intent.ACTION_VIEW);
                browserIntent.addCategory(Intent.CATEGORY_BROWSABLE);
                browserIntent.setComponent(comp);
                browserIntent.setData(uriUrl);
            }
        }

        this.startActivity(browserIntent);

    }catch (Exception e){
        e.printStackTrace();
        txtHeader.setText(e.toString());
    }
查看更多
劫难
5楼-- · 2020-01-24 10:15

Use a webview:

WebView webview = new WebView(this);
setContentView(webview);
byte[] post = EncodingUtils.getBytes("postvariable=value&nextvar=value2", "BASE64");
webview.postUrl("http://www.geenie.nl/AnHeli/mobile/ranking/demo/index.php", post);
查看更多
登录 后发表回答