保持Android的cookie中的会话Android、cookie

2023-09-12 08:37:38 作者:习惯用你的名字去拒绝别人

好吧,我有一个Android应用程序中有一个形式,二的EditText,旋转器,和一个登录按钮。用户从微调选择服务,他们的用户名和密码,点击登录类型。该数据通过POST发送,返回响应,它的处理,新的web视图被启动,从响应产生的HTML字符串被加载,并且我有任何服务的用户选择的主页。

Okay, I have an android application that has a form in it, two EditText, a spinner, and a login button. The user selects the service from the spinner, types in their user name and password, and clicks login. The data is sent via POST, a response is returned, it's handled, a new webview is launched, the html string generated from the response is loaded, and I have the home page of whatever service the user selected.

这是一切都很好。现在,当用户点击一个链接,登录信息不能被发现,并在页面要求用户重新登录。我的登录会话被丢弃的地方,而我不能确定如何通过从控制我的应用程序,只是启动的WebView活动类的主要部分的类信息。

That's all well and good. Now, when the user clicks on a link, the login info can't be found, and the page asks the user to login again. My login session is being dropped somewhere, and I'm not certain how to pass the info from the class that controls the main part of my app to the class that just launches the webview activity.

上单击处理程序从表单中登录按钮:

The on click handler from the form login button:

 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);
    }
}

这里是处理响应显示的WebView类:

And here is the webview class that handles the response display:

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);}

 }
}

我有混杂的成功实施该cookie的解决方案。它似乎工作一个服务,我登录,我知道保持饼干的服务器上(旧的,过时的,但它的作品,他们不想改变它。)我试图现在的服务需要用户不断其本地计算机上的cookie,它不与此设置工作。

I've had mixed success implementing that cookie solution. It seems to work for one service I log into that I know keeps the cookies on the server (old, archaic, but it works and they don't want to change it.) The service I'm attempting now requires the user to keep cookies on their local machine, and it does not work with this setup.

有什么建议?

推荐答案

您需要保持的cookie从一个电话到另一个。而不是创建一个新的DefaultHttpClient的,用这个生成器:

You need to keep the cookie from one call to another. Instead of creating a new DefaultHttpClient, use this builder:

private Object mLock = new Object();
private CookieStore mCookie = null;
/**
 * Builds a new HttpClient with the same CookieStore than the previous one.
 * This allows to follow the http session, without keeping in memory the
 * full DefaultHttpClient.
 * @author Régis Décamps <decamps@users.sf.net>
 */
private HttpClient getHttpClient() {
        final DefaultHttpClient httpClient = new DefaultHttpClient();
        synchronized (mLock) {
                if (mCookie == null) {
                        mCookie = httpClient.getCookieStore();
                } else {
                        httpClient.setCookieStore(mCookie);
                }
        }
        return httpClient;
}

和保持生成器类作为应用程序的一个领域。

And keep the Builder class as a field of your application.