我想通过我的网页浏览中的帖子登录网站。这工作正常,但如果我点击这个网站上的按钮,我会被重定向回登录页面。如何保存登录信息,以便我再次登录?我应该使用cookies来解决问题吗?我已经尝试过使用cookies,但这并没有解决问题。
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
CookieSyncManager.createInstance(MainActivity.this);
CookieSyncManager.getInstance().sync();
}
private class LoadWebPageASYNC extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... urls) {
String result = "";
// Create a new HttpClient and Post Header
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://example/index.php?s=login");
try {
CookieManager cookieManager = CookieManager.getInstance();
cookieManager.setAcceptCookie(true);
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(3);
nameValuePairs.add(new BasicNameValuePair("id", "id"));
nameValuePairs.add(new BasicNameValuePair("pass", "pass"));
nameValuePairs.add(new BasicNameValuePair("submit", "login"));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
// Execute HTTP Post Request
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
result = EntityUtils.toString(entity, "UTF-8");
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
} catch (IOException e) {
// TODO Auto-generated catch block
}
return result;
}
@Override
protected void onPostExecute(String result) {
WebView webView = (WebView) findViewById(R.id.webView);
webView.setWebViewClient(new WebViewClient());
webView.getSettings().setJavaScriptEnabled(true);
CookieManager.setCookie();
webView.loadDataWithBaseURL("http://example/index.php?s=login", result, "text/html", "BASE64", null);
}
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
}
public void dummyFunc(View view){
Toast.makeText(MainActivity.this, "Button works", Toast.LENGTH_SHORT).show();
}
public void readWebpage(View view) {
LoadWebPageASYNC task = new LoadWebPageASYNC();
task.execute(new String[] { "http://example/index.php?s=login" });
}
}
答案 0 :(得分:0)
是的,你必须使用cookie。