webView跳过网页登录页面打开网页

使用http框架是android-async-http-1.4.8


package baidumapsdk.demo.jss;

import android.app.Activity;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.webkit.CookieManager;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.PersistentCookieStore;
import com.loopj.android.http.RequestParams;
import com.loopj.android.http.SyncHttpClient;

import org.apache.http.Header;
import org.apache.http.cookie.Cookie;

import java.util.List;

import baidumapsdk.demo.DemoApplication;
import baidumapsdk.demo.R;
import baidumapsdk.demo.jss.Utils.HttpString;


/**
 * Created by liuzhifeng on 16-12-26.
 */
public class LoginActivity extends Activity {

    private Button loginBtn;

    private EditText mEditAccount;
    private EditText mEditPassWord;
    private ProgressDialog mProgressLogin;

    private boolean IsSaveNameAndPw;

    private String UserName;
    private String PassWord;
    private DemoApplication application;
    private static final String TAG = "tag";
    private WebView mwebView;
    private String CookieStr;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        //获取控件
        initView();

        application = (DemoApplication) getApplication();
        client = application.getClient();
        cookie = application.getCookie();

        
        loginBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //登录操作
                Login();
            }
        });


    }

    private SyncHttpClient client;
    private PersistentCookieStore myCookieStore;
    private List cookie;

    public class HTTPThread extends Thread {
        public void run() {
            RequestParams mparams = new RequestParams(); // 绑定参数
            mparams.put("loginId", UserName);// 前面的key就是连接中所对应的字段
            mparams.put("password", PassWord);

            System.out.println("sssssssqqqqqq");

//            保存cookie,自动保存到了shareprefercece
            myCookieStore = new PersistentCookieStore(LoginActivity.this);
            client.setCookieStore(myCookieStore);


            String url = HttpString.LOGIN;
            client.post(url, mparams, new AsyncHttpResponseHandler() {
                /**
                 * 成功处理的方法
                 * statusCode:响应的状态码; headers:相应的头信息 比如 响应的时间,响应的服务器 ;
                 * responseBody:响应内容的字节
                 */
                @Override
                public void onSuccess(int statusCode, Header[] headers,
                                      byte[] responseBody) {
                    if (statusCode == 200) {

                        myCookieStore = new PersistentCookieStore(application);
                        //保存cookie
                         cookie = myCookieStore.getCookies();                      

                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                //加载WebView
                                InitWebView();
                            }
                        });
                    }
                }

                /**
                 * 失败处理的方法
                 * error:响应失败的错误信息封装到这个异常对象中
                 */
                @Override
                public void onFailure(int statusCode, Header[] headers,
                                      final byte[] responseBody, Throwable error) {
                    
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            Toast.makeText(LoginActivity.this, "登录失败:" + responseBody.toString(), Toast.LENGTH_SHORT).show();
                        }
                    });
                    error.printStackTrace();// 把错误信息打印出轨迹来
                }
            });

        }
    }

    private void InitWebView() {


        String url = "http://xxxx";

        CookieManager cookieManger = CookieManager.getInstance();
        cookieManger.setAcceptCookie(true);
        //从返回的cookie集合中拼接cookie
        String CookieStr3 = "JSESSIONID="+cookie.get(1).getValue()+";"+"uid="+cookie.get(2).getValue()+";sdktoken="+cookie.get(0).getValue();
        Log.i("tag", "CookieStr3=" + CookieStr3);
        cookieManger.setCookie(url, CookieStr3);

        WebSettings setting = mwebView.getSettings();
        setting.setJavaScriptEnabled(true);
        mwebView.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                return super.shouldOverrideUrlLoading(view, url);
            }
        });
        mwebView.loadUrl(url);


    }


    private void Login() {

        if (mEditAccount.getText().length() == 0) {
            Toast.makeText(LoginActivity.this, "请输入用户名", Toast.LENGTH_SHORT).show();
            return;
        }
        if (mEditPassWord.getText().length() == 0) {
            Toast.makeText(LoginActivity.this, "请输入密码", Toast.LENGTH_SHORT).show();
            return;
        } else {
            UserName = mEditAccount.getText().toString().trim();
            PassWord = mEditPassWord.getText().toString();

            HTTPThread HTTPClientThread = new HTTPThread();
            HTTPClientThread.start();
        }
    }


    private void initView() {

        setContentView(R.layout.login_layout);

        mEditAccount = (EditText) findViewById(R.id.edit_account);
        mEditPassWord = (EditText) findViewById(R.id.edit_account_password);
        loginBtn = (Button) findViewById(R.id.btn_login);

        mwebView = (WebView) findViewById(R.id.wv_webview);


    }
}


你可能感兴趣的:(webView跳过网页登录页面打开网页)