Hello Stackoverflowers!
I have written a relatively simple application that consists of a login text field, a password text field and a login button.
My goal is that when the user enters the login information and touches the login button, the application will log the user into the website I have specified and open it up in a different intent or WebView. My current implementation opens a new activity with a WebView and passes in the login information. My code is as follows for the new activity:
setContentView(R.layout.web);
try {
//add the users login information(username/pw) to a List
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("email", "random@gmail.com"));
nameValuePairs.add(new BasicNameValuePair("password", "password1"));
//declare a new HttpClient
HttpClient httpclient = new DefaultHttpClient();
//set the HttpPost to the desire URL
HttpPost httppost = new HttpPost(URL_STRING);
//set the entity to a new UrlEncodedForm with the login info and type
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs, HTTP.UTF_8));
//store the response
HttpResponse response = httpclient.execute(httppost);
//get the data from the response
String data = new BasicResponseHandler().handleResponse(response);
//get the webview from the xml
WebView webview = (WebView)findViewById(R.id.webView);
webview.setWebViewClient(new WebViewClient() {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url)
{
view.loadUrl(url);
return true;
}
});
//load the return website from the server
webview.loadDataWithBaseURL(httppost.getURI().toString(), data, "text/html", HTTP.UTF_8, null);
This successfully logs me in to the URL (an https site) and opens the page, however if you try to click on any of the buttons on the website it takes you back to the login page in the WebView, and it does not display many of the attributes on the website (charts/graphs).
Could this be a cookie thing?
So is there a way to send the login info via a new intent? Or is there a solution to my WebView implementation?
(here is a relatively similar(ish) question that never got a definitive answer Android SSO (Single sign-on) for app)
Thank you for your time! I really appreciate you taking a look at my question.
EDIT: So giseks's solution worked for me to get the webpage to stay within the WebView, however the charts/graphs/etc on the page still did not display, the solution for that was as simple as enabling javascript for the WebSettings
webview.getSettings().setJavaScriptEnabled(true);
Here's Google's WebSettings Android API for reference: WebSettings
This helped me, I hope it helps you!
My guess is that your application doesn't handle cookies right. Take a look at this question, it may help.
WebView and Cookies on Android
EDIT
In your code you seem to pass only the html retrieved from request to the WebView. Cookies seem to get lost somewhere. I'd suggest you another approach.