OK, tôi có một ứng dụng Android có biểu mẫu trong đó, hai EditText, một spinner và nút đăng nhập. Người dùng chọn dịch vụ từ spinner, các loại trong tên người dùng và mật khẩu của họ, và nhấp chuột đăng nhập. Dữ liệu được gửi qua POST, phản hồi được trả về, nó được xử lý, một WebView mới được khởi chạy, chuỗi html được tạo ra từ phản hồi được tải và tôi có trang chủ của bất kỳ dịch vụ nào mà người dùng đã chọn.Duy trì phiên cookie trong Android
Đó là tất cả tốt và tốt. Bây giờ, khi người dùng nhấp vào liên kết, bạn không thể tìm thấy thông tin đăng nhập và trang yêu cầu người dùng đăng nhập lại. Phiên đăng nhập của tôi đang bị xóa ở đâu đó và tôi không chắc chắn cách chuyển thông tin từ lớp kiểm soát phần chính của ứng dụng của tôi đến lớp chỉ khởi chạy hoạt động xem trên web.
Các onClick handler từ nút mẫu đăng nhập:
private class FormOnClickListener implements View.OnClickListener {
public void onClick(View v) {
String actionURL, user, pwd, user_field, pwd_field;
actionURL = "thePageURL";
user_field = "username"; //this changes based on selections in a spinner
pwd_field = "password"; //this changes based on selections in a spinner
user = "theUserLogin";
pwd = "theUserPassword";
List<NameValuePair> myList = new ArrayList<NameValuePair>();
myList.add(new BasicNameValuePair(user_field, user));
myList.add(new BasicNameValuePair(pwd_field, pwd));
HttpParams params = new BasicHttpParams();
DefaultHttpClient client = new DefaultHttpClient(params);
HttpPost post = new HttpPost(actionURL);
HttpResponse response = null;
BasicResponseHandler myHandler = new BasicResponseHandler();
String endResult = null;
try { post.setEntity(new UrlEncodedFormEntity(myList)); }
catch (UnsupportedEncodingException e) { e.printStackTrace(); }
try { response = client.execute(post); }
catch (ClientProtocolException e) { e.printStackTrace(); }
catch (IOException e) { e.printStackTrace(); }
try { endResult = myHandler.handleResponse(response); }
catch (HttpResponseException e) { e.printStackTrace(); }
catch (IOException e) { e.printStackTrace(); }
List<Cookie> cookies = client.getCookieStore().getCookies();
if (!cookies.isEmpty()) {
for (int i = 0; i < cookies.size(); i++) {
cookie = cookies.get(i);
}
}
Intent myWebViewIntent = new Intent(MsidePortal.this, MyWebView.class);
myWebViewIntent.putExtra("htmlString", endResult);
myWebViewIntent.putExtra("actionURL", actionURL);
startActivity(myWebViewIntent);
}
}
Và đây là lớp WebView để xử lý việc hiển thị câu trả lời:
public class MyWebView extends android.app.Activity {
private class MyWebViewClient extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.web);
MyWebViewClient myClient = new MyWebViewClient();
WebView webview = (WebView)findViewById(R.id.mainwebview);
webview.getSettings().setBuiltInZoomControls(true);
webview.getSettings().setJavaScriptEnabled(true);
webview.setWebViewClient(myClient);
Bundle extras = getIntent().getExtras();
if(extras != null)
{
// Get endResult
String htmlString = extras.getString("htmlString");
String actionURL = extras.getString("actionURL");
Cookie sessionCookie = MsidePortal.cookie;
CookieSyncManager.createInstance(this);
CookieManager cookieManager = CookieManager.getInstance();
if (sessionCookie != null) {
cookieManager.removeSessionCookie();
String cookieString = sessionCookie.getName()
+ "=" + sessionCookie.getValue()
+ "; domain=" + sessionCookie.getDomain();
cookieManager.setCookie(actionURL, cookieString);
CookieSyncManager.getInstance().sync();
}
webview.loadDataWithBaseURL(actionURL, htmlString, "text/html", "utf-8", actionURL);}
}
}
}
Tôi đã thành công hỗn hợp thực hiện rằng giải pháp cookie. Dường như làm việc cho một dịch vụ mà tôi đăng nhập vào đó tôi biết giữ các cookie trên máy chủ (cũ, cổ, nhưng nó hoạt động và họ không muốn thay đổi nó.) Dịch vụ tôi đang cố yêu cầu người dùng giữ cookie trên máy cục bộ của họ và nó không hoạt động với thiết lập này.
Mọi đề xuất?
Chắc chắn, nhưng đó là một sự lãng phí bộ nhớ để lưu trữ toàn bộ 'DefaultHttpClient' khi bạn chỉ cần cookie. – rds