0

I need to log in and post user Id and pass word into the web view. Can anyone help me, please!!! thanks in advance.

How to show needed content in a webview. I need to get required contents in a webview to post login data

 public class MainActivity extends AppCompatActivity {
EditText login_user,login_password;
Button login_button;
String uName,uPwd,data;
WebView webView;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    login_user=(EditText)findViewById(R.id.userName);
    login_password=(EditText)findViewById(R.id.passWord);
    login_button=(Button)findViewById(R.id.loginButton);

    webView=(WebView)findViewById(R.id.webView);

    login_button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            try{
                getText();

            }catch(Exception e){
                e.printStackTrace();
            }
        }
    });
}

private void getText() {
    uName = login_user.getText().toString();
    uPwd = login_password.getText().toString();

    WebView webview = new WebView(this);
    setContentView(webview);
    String url = "website url";
    String postData = null;
    try {
        postData = "username=" + URLEncoder.encode(uName, "UTF-8") + "&password=" + URLEncoder.encode(uPwd, "UTF-8");
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    }
    webview.postUrl(url,postData.getBytes());
}

}

Manjunath
  • 52
  • 1
  • 10
  • I don't think you can post data with WebView. You may want to post it using some HTTP library, and load the response HTML into WebView – Vladyslav Matviienko Jan 29 '19 at 12:39
  • @VladyslavMatviienko yes we tried that too, Can you suggest me how to post – Manjunath Jan 29 '19 at 12:42
  • what exactly you tried, and why it didn't work? – Vladyslav Matviienko Jan 29 '19 at 12:42
  • @VladyslavMatviienko i tried to follow this tutorial https://androidexample.com/How_To_Make_HTTP_POST_Request_To_Server_-_Android_Example/index.php?view=article_discription&aid=64 – Manjunath Jan 29 '19 at 12:45
  • if you want to just post data on the server then you can use retrofit. – Uday Nayak Jan 29 '19 at 13:30
  • Hello @UdayNayak bro, I need to post data using webview, no issue with the retrofit or AsyncTask.. Can you tell how to post login data into the server using webview – Manjunath Jan 30 '19 at 05:26
  • @Manjunath you have a webpage of login is on server or local? if you have use login button then you can follow this link:- https://stackoverflow.com/questions/20917235/webviews-html-button-click-detection-in-activityjava-code or something like that. – Uday Nayak Jan 30 '19 at 12:40
  • @UdayNayak thanks for your reply.. Bro, I need to post login data of EditTexts(username and password) in the login Activity into the server of which website login Account URL. please tell me what to do and I tried that code in your recent post(it shows webview directly – Manjunath Jan 30 '19 at 13:22

1 Answers1

0

you can try this:-

String postData = "username=abc&password=236";

webview.postUrl(
    "website url/exampleURL",
    EncodingUtils.getBytes(postData, "BASE64"));

and also check this links:- 1)How to post data for WebView android

2)How to make post requests with webview?

Uday Nayak
  • 429
  • 4
  • 20