0
我想通過在我的webview中發帖登錄到網站。這工作正常,但如果我點擊這個網站上的按鈕,我會重定向回登錄頁面。我怎樣才能保存登錄,所以我需要再次登錄?我應該使用Cookie來解決問題嗎?我已經嘗試過使用Cookie,但沒有解決問題。在Android中發送POST數據並更改網站
@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" });
}
}
okey,但我已經嘗試解決cookie的問題,但它不工作太... – user3129870