Android WebView请求,保存cookie,判断登录状态

TestActivity.java


import org.apache.http.client.CookieStore;
import org.apache.http.cookie.Cookie;
import org.apache.http.impl.client.BasicCookieStore;
import org.apache.http.impl.cookie.BasicClientCookie;

import com.ydzy.machine.bean.DataCenter;

import net.tsz.afinal.FinalHttp;
import net.tsz.afinal.http.AjaxCallBack;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.CookieManager;
import android.webkit.CookieSyncManager;
import android.webkit.WebView;
import android.webkit.WebViewClient;

public class TestActivity extends BaseActivity implements OnClickListener{
	
	WebView webView;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.test);
		webView=(WebView) findViewById(R.id.web);
		webView.getSettings().setJavaScriptEnabled(true);
//		webView.addJavascriptInterface(this, "data");
		webView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);//允许js弹出窗口
		webView.getSettings().setUseWideViewPort(true);// 设置此属性,可任意比例缩放。
		webView.loadUrl("http://cangku.card-books.com/app_dev.php/login");
		
		webView.setWebViewClient(new WebViewClient(){
			@Override
			public boolean shouldOverrideUrlLoading(WebView view, String url) {
				// TODO Auto-generated method stub
				view.loadUrl(url);
				return false;
			}
			@Override
			public void onPageStarted(WebView view, String url, Bitmap favicon) {
				// TODO Auto-generated method stub
				
				super.onPageStarted(view, url, favicon);
			}
			
			@Override
			public void onPageFinished(WebView view, String url) {
				// TODO Auto-generated method stub
				super.onPageFinished(view, url);
//				IWLog.d(TAG, "onPageFinished() url is:"+url);  
	            /* 将cookie保存起来*/  
	            String c = CookieManager.getInstance().getCookie(url);  
	            System.out.println(c+"------c--->");
	            DataCenter.setCookie(c);  
	            CookieSyncManager.getInstance().sync();  

			}
			
		});
			
		
		
	}
	@Override
	public void onClick(View v) {
		if(v.getId()==R.id.get_bt){
			System.out.println("--------->");
			FinalHttp fb=new FinalHttp();
			CookieStore cookieJar= new BasicCookieStore();  
			String _cookie = DataCenter.getCookie();  
            if(_cookie !=null && !_cookie.equals("")){  
                String[] cookies = _cookie.split(";");  
                for(int i=0; i< cookies.length; i++){  
                    String[] nvp = cookies[i].split("=");  
                    System.out.println("-----nvp:"+nvp[0]+"---->"+nvp[1]);
                    BasicClientCookie c = new BasicClientCookie(nvp[0], nvp[1]);  
                    c.setVersion(0);   
                    c.setDomain(".card-books.com");//这里是自己的主机地址   
                    cookieJar.addCookie(c);  
                }  
           }  
           
//            BasicClientCookie c = new BasicClientCookie("PHPSESSID","43ntd1pulgbo9s9ji5rdm3mve3");
//            cookieJar.addCookie(c);
			
			fb.configCookieStore(cookieJar);
			fb.get("http://cangku.card-books.com/app_dev.php/user/retrieve", new AjaxCallBack<Object>(){
				@Override
				public void onStart() {
					// TODO Auto-generated method stub
					super.onStart();
				}
				@Override
				public void onFailure(Throwable t, int errorNo, String strMsg) {
					// TODO Auto-generated method stub
					super.onFailure(t, errorNo, strMsg);
					showToast(TestActivity.this, t.toString()+"--fail->");
					System.out.println(t.toString()+"--fail->");
				}
				
				@Override
				public void onSuccess(Object t) {
					// TODO Auto-generated method stub
					super.onSuccess(t);
					showToast(TestActivity.this, t.toString()+"--succ->");
					System.out.println(t.toString()+"--succ->");
				}
			});
		}
	}
	
}

Sentence.java

public class Sentence
{
	private int id;
	private String title;
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getTitle() {
		return title;
	}
	public void setTitle(String title) {
		this.title = title;
	}
	public Sentence(int id,String title)
	{
		setId(id);
		setTitle(title);
	}
}

test.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    <Button 
         android:layout_width="match_parent"
    	android:layout_height="wrap_content"
    	android:text="get"
        android:id="@+id/get_bt"
        android:onClick="onClick"
        />
    
    
    <WebView 
         android:layout_width="match_parent"
    	android:layout_height="match_parent"
    	android:id="@+id/web"
        
        />
    

</LinearLayout>



阅读更多
个人分类: 安卓开发
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭
关闭