How can I open Android browser with specified POST parameters?
Asked Answered
S

4

29

In my Android application, I need to open a link in 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?

Sorel answered 7/11, 2010 at 21:36 Comment(5)
> Could I add these parameters(data) to the intent which start the browser? POST? No. GET? Yes.Nathalie
Thank you. So is no way to send some data via POST to a page and open it in Android Browser?Even if I use WebView?Sorel
Correct. Just as you cannot "some data via POST to a page" anywhere else using a plain URL (e.g., hyperlink in Web page). This isn't an Android thing -- it's an Internet thing.Nathalie
You're right! I will search for a other solution. Thank you!Sorel
@UngureanuLiviu, Hi there i would like to talk to you regarding adding parameters to a POST request in androidChurchlike
M
14

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);
Massproduce answered 4/4, 2011 at 18:29 Comment(1)
how would i send a json payload?Chidester
C
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.

Cottonseed answered 8/11, 2010 at 6:25 Comment(1)
Of course, what is supported ultimately depends on which Browser app is receiving the Intent.Cottonseed
H
2
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());
    }
Husserl answered 16/9, 2016 at 17:19 Comment(1)
I have tried this and it doesnt work. Chrome opens up but nothing happens.Repudiation
T
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.

Triumphal answered 12/6, 2015 at 14:20 Comment(2)
The main reason you might use POST is when the request is not idempotent, but changes something.Tical
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.Adamik

© 2022 - 2024 — McMap. All rights reserved.