25

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?

Lii
  • 9,906
  • 6
  • 53
  • 73
Ungureanu Liviu
  • 3,824
  • 4
  • 35
  • 42

4 Answers4

13

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);
Pete
  • 1,131
  • 10
  • 17
3
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());
    }
Mohammad Hadi
  • 1,246
  • 3
  • 16
  • 30
3

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.

Chris Stratton
  • 38,489
  • 6
  • 80
  • 115
0

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.

pxlinux
  • 19
  • 1
  • 2
  • The main reason you might use *POST* is when the request is not idempotent, but changes something. – Flimm Jul 21 '16 at 14:29
  • While the beginning is interesting, the ending is almost misleading. We can post some sensitive data like login, password, name, phone, token and more, that shouldn't be seen in history and have security risk. But we have to pass them in order to authorize in a web page from authorized Android application. – CoolMind Feb 25 '20 at 16:26