this is my very first time making an Android
app, and maybe the 5th or so time that I have touched Java
, so bear with me.. My goal is to make a mobile app that will log a user in to wordpres
s (a site we use at my school, so they aren't admins, just valid users), among other things, but it is the logging in where I hit a snag.
Also throughout I have borrowed code from previous SO questions I have found, and will link throughout, if I can find them again.
The problem: My approach (thanks to google, and many posts on this site) consists of using HttpClient to make the http Post request and to log in to the site, then my goal was to transfer the cookies to the Webview so that my users won't have to log in manually.
Global vars: myWebView, and the httpClient
For testing purposes I overrode the network on os main thread exception/error
Most of my code comes from here: (Sharing session between httpClient and webView issue in android 4.4+)
Some code pertaining to the above:
AppSettings class (from SO post) to manipulate cookies
public class AppSettings extends Application
{
private static final DefaultHttpClient client = createClient();
@Override
public void onCreate()
{
}
public static DefaultHttpClient getClient()
{
return client;
}
private static DefaultHttpClient createClient()
{
BasicHttpParams params = new BasicHttpParams();
SchemeRegistry schemeRegistry = new SchemeRegistry();
schemeRegistry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
final SSLSocketFactory sslSocketFactory = SSLSocketFactory.getSocketFactory();
schemeRegistry.register(new Scheme("https", sslSocketFactory, 443));
ClientConnectionManager cm = new ThreadSafeClientConnManager(params, schemeRegistry);
DefaultHttpClient httpclient = new DefaultHttpClient(cm, params);
httpclient.getCookieStore().getCookies();
return httpclient;
}
}
Now in the File that utilizes the WebView, I put this, which is supposed to do the bulk of cookie manipulation
myWebView.setWebViewClient(new WebViewClient()
{
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon)
{
super.onPageStarted(view, url, null);
Log.w(null, "onPage Started url is" + url);
Cookie sessionInfo;
List<Cookie> cookies = httpClient.getCookieStore().getCookies();
if (!cookies.isEmpty())
{
CookieSyncManager.createInstance(DisplayMessageActivity.this);
CookieManager cookieManager = CookieManager.getInstance();
for (Cookie cookie : cookies)
{
sessionInfo = cookie;
String cookieString = sessionInfo.getName() + "=" + sessionInfo.getValue()
+ "; domain=" + sessionInfo.getDomain();
Log.w(null, "cookie string is " + cookieString);
cookieManager.setCookie("http://www.corpsofcadets.net", cookieString);
CookieSyncManager.getInstance().sync();
Log.w(null, "cookie loop " + cookie.getName());
}
}
else
{
Log.w(null, "no cookies");
}
}
});
And here is where I post the data. As a quick note, this seems to be working, or at least improved, because before I set the headers manually, wordpress was sending back an HTTP 406 error. Now the responseBody appears to be equivalent to the HTML of the login page (http://pastebin.com/smLJrziz), but I also see that I am getting assigned wordpress' "test_cookie" so something must be happening, right?!
public void postData() throws IOException, ClientProtocolException {
httpClient = AppSettings.getClient();
HttpPost request = new HttpPost("http://corpsofcadets.net/wp-login.php");
request.setHeader("Accept", "*/*");
request.setHeader("Content-Type", "application/x-www-form-urlencoded");
request.setHeader("User-Agent", "Mozilla/5.0");
request.setHeader("Accept-Encoding", "gzip/deflate");
List<NameValuePair> postParameters = new ArrayList<NameValuePair>();
postParameters.add(new BasicNameValuePair("user_login", "obscured"));
postParameters.add(new BasicNameValuePair("user_pass", "obscured"));
try {
UrlEncodedFormEntity entity = new UrlEncodedFormEntity(postParameters);
request.setEntity(entity);
HttpResponse response= httpClient.execute(request);
if (response != null)
{
String responseBody = EntityUtils.toString(response.getEntity());
Log.w(null, "responseBody " + responseBody);
}
else
{
Log.w(null, "FAIL");
}
} catch (Exception e)
{
Log.w(null, "Error sending data");
e.printStackTrace();
}
This may be considered an opinion question, and therefore off topic, but I just stumbled upon this SO question (Pass cookies from HttpURLConnection (java.net.CookieManager) to WebView (android.webkit.CookieManager)), could this, in conjunction with httpurlconnection, be a simpler solution? And lastly, could I use XML-RPC in any way?
Thanks a million
Methods that did not work: Webview.loadData, Webview.posturl, submitting the form with javascript with Webview.loadurl, this current setup and similar iterations