본문 바로가기

IT

Intent 를 이용하여 POST 방식의 주소에 파라메터를 함께 전달하는 방법

반응형

ACTION_VIEW 를 이용해서 프로그램에서 외부 웹브라우저를 이용하여 웹을 연동할 때 바로 POST 방식으로 파라메터를 던지는 경우 사용할 수 있는 방법이다.


땡규~ Ziteng Chen


The Android Browser support 'viewing' javascript, for example the following code can launch the Browser app to show an alert dialog:

    String finalUrl = "javascript:alert('hello')";
    Intent browserIntent = new Intent(android.content.Intent.ACTION_VIEW,
                            Uri.parse(finalUrl));
    startActivity(browserIntent);

A common trick to do post operation by javascript is that you create a form by javascript and then submit it. So in theory code like below should work (part of the code is copied from this post):

    //String finalUrl = "http://localhost:7001/display/result.jsp?param=12345";
    String finalUrl = "javascript:" + 
        "var to = 'http://localhost:7001/display/result.jsp';" +
        "var p = {param:'12345',param2:'blablabla',param3:'whatever'};"+
        "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) ;";
    Intent browserIntent = new Intent(android.content.Intent.ACTION_VIEW,
                            Uri.parse(finalUrl));
    startActivity(browserIntent);

참조 링크

1. http://stackoverflow.com/questions/12890361/android-open-browser-with-a-url-with-request-headers

2. http://lsit81.tistory.com/77

반응형