編輯:關於Android編程
最近兩天一直想用安卓模擬登陸,利用新的WebView顯示登陸後可以訪問的頁面,但是不管怎麼訪問需要登陸後才能訪問的頁面,還是跳回到登陸頁,後來網上看了下是cookie沒有設置,找了半天才到到合適的設置方法:
登陸方法:
private Cookie cookie; public static HttpContext localContext; public static Listcookies;//此為保存的cookie public String cookieStr; // public static Cookie cookie = null; /** * 登陸時保存cookie * @param url * @param data * @return */ public String login(String url,String data[]){ HttpClient client = null; String html = null; try { client = new DefaultHttpClient(); // 創建cookie store的本地實例 CookieStore cookieStore = new BasicCookieStore(); localContext = new BasicHttpContext(); // 綁定定制的cookie store到本地內容中 localContext.setAttribute(ClientContext.COOKIE_STORE, cookieStore); HttpPost post = new HttpPost(url); Listparameters = new ArrayList (); parameters.add(new BasicNameValuePair("username", data[0])); parameters.add(new BasicNameValuePair("passwd", data[1])); parameters.add(new BasicNameValuePair("login", "%B5%C7%A1%A1%C2%BC")); UrlEncodedFormEntity entity = new UrlEncodedFormEntity(parameters,"utf-8"); post.setEntity(entity); HttpResponse response = client.execute(post,localContext); if(response.getStatusLine().getStatusCode() == 200){ InputStream content = response.getEntity().getContent(); BufferedReader buffer = new BufferedReader(new InputStreamReader(content,"gbk")); String line = null; while((line=buffer.readLine())!=null){ html+=line; } cookies = cookieStore.getCookies(); System.out.println("cookies.size():"+cookies.size()); if (!cookies.isEmpty()) { for(int i=0;i
Activity中的oncreate()方法:
private WebView wvTempShow; private String receiveUrl; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_temp_web_view); wvTempShow = (WebView) findViewById(R.id.wv_tempShow); Intent intent = this.getIntent(); receiveUrl = intent.getStringExtra("openUrl"); //獲得cookie管理者 CookieManager cookieManager = CookieManager.getInstance(); //獲取登陸時的cookie String oldCookie = UtilsLogin.cookies.get(0).getName()+"="+UtilsLogin.cookies.get(0).getValue()+";"+ UtilsLogin.cookies.get(1).getName()+"="+UtilsLogin.cookies.get(1).getValue()+";" ; System.out.println("oldCookie:"+oldCookie); cookieManager.setCookie(receiveUrl, oldCookie); wvTempShow.getSettings().setDefaultTextEncodingName("gbk"); wvTempShow.loadUrl(receiveUrl); wvTempShow.setWebViewClient(new MyWebViewClient()); } class MyWebViewClient extends WebViewClient{ @Override public void onPageStarted(WebView view, String url, Bitmap favicon) { super.onPageStarted(view, url, favicon); } @Override public void onPageFinished(WebView view, String url) { super.onPageFinished(view, url); } }
需要注意的是:要看訪問的頁面需要什麼樣的cookie字符串可以用以下方法:CookieManager cookieManager = CookieManager.getInstance(); String CookieStr = cookieManager.getCookie("http://bkjw.guet.edu.cn/student/public/menu.asp?menu=mnall.asp"); System.out.println("TempWebViewonPageFinished = " + CookieStr);
然後自己像以上String oldCookie中一樣自己拼好,再在cookieManager.setCookie(url,cookieString);中設置
引言 簡單介紹一下Cordova的來歷,Cordova的前身叫PhoneGap,自被Adobe收購後交由Apache管理,並將其核心功能開源改名為Cordova。它能讓你
好吧,終於要開始講解Activity的啟動流程了,Activity的啟動流程相對復雜一下,涉及到了Activity中的生命周期方法,涉及到了Android體系的CS模式,
今天繼續說一下安卓的菜單,之前介紹了:相信大家對於Metro風格並不陌生,下面就在安卓平台上實現一下這個效果,如圖: 實現思路: 利用動畫來實現移動的效果,使
最近在玩3g體育門戶客戶端的時候,看到這樣個效果: 輕觸賽事圖標,會有一個圖標變大浮出的效果.,蠻有意思的.於是就把仿照它做了一個. 這個是原圖: