編輯:關於Android編程
package com.glodon.cas.model; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.net.HttpURLConnection; import java.net.URL; import java.net.URLEncoder; import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpGet; import org.apache.http.client.methods.HttpPost; import org.apache.http.impl.client.DefaultHttpClient; public class GlodonCas { private static DefaultHttpClient httpClient = new DefaultHttpClient(); private static boolean casLogined = false; public final static String CAS_DOMAIN = "http://cas.grandsoft.com.cn"; public String appCallback = null; private boolean isLogined = false; public GlodonCas(String appCallback) { this.appCallback = appCallback; } public boolean login(String username, String password) throws Exception { String service = appCallback; String lt = getLt(); String loginApi = CAS_DOMAIN + "/login"; String params = "username=" + URLEncoder.encode(username) + "&password=" + URLEncoder.encode(password) + "<=" + URLEncoder.encode(lt) + "&service=" + service; byte[] entitydata = params.getBytes(); HttpURLConnection c = (HttpURLConnection) new URL(loginApi) .openConnection(); c.setInstanceFollowRedirects(false); c.setRequestMethod("POST"); c.setDoOutput(true); c.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); c.setRequestProperty("Content-Length", String.valueOf(entitydata.length)); OutputStream outStream = c.getOutputStream(); outStream.write(entitydata); outStream.flush(); outStream.close(); int responeCode = -1; try { responeCode = c.getResponseCode(); } catch (java.io.IOException e) { if (e.getMessage().contains("authentication challenge")) { responeCode = 401; } else { throw e; } } if (responeCode == 303) { get(c.getHeaderField("Location")); isLogined = true; casLogined = true; return true; } else { isLogined = false; casLogined = true; return false; } } public boolean isLogined() { return isLogined; } public static boolean casLogined() { return casLogined; } public static DefaultHttpClient getHttpClient() { return httpClient; } private static String getLt() throws IOException { String httpUrl = CAS_DOMAIN + "/loginTicket"; HttpPost httpPost = new HttpPost(httpUrl); HttpResponse httpRespone = httpClient.execute(httpPost); HttpEntity httpEntity = httpRespone.getEntity(); InputStream inputStream = httpEntity.getContent(); BufferedReader reader = new BufferedReader(new InputStreamReader( inputStream)); String str = ""; String line = null; while ((line = reader.readLine()) != null) { str += line; } return str; } private static String get(String httpUrl) throws Exception { HttpGet httpGet = new HttpGet(httpUrl); InputStream inputStream = null; String bodyStr = ""; HttpResponse httpRespone = httpClient.execute(httpGet); HttpEntity httpEntity = httpRespone.getEntity(); inputStream = httpEntity.getContent(); BufferedReader reader = new BufferedReader(new InputStreamReader( inputStream)); String line = ""; while ((line = reader.readLine()) != null) { bodyStr += line; } return bodyStr; } }
package com.glodon.cas.activities; import java.io.BufferedReader; import java.io.InputStream; import java.io.InputStreamReader; import java.util.List; import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpGet; import org.apache.http.cookie.Cookie; import android.app.Activity; import android.content.Intent; import android.os.AsyncTask; import android.os.Bundle; import android.view.View; import android.webkit.CookieManager; import android.webkit.CookieSyncManager; import android.webkit.WebView; import android.webkit.WebViewClient; import android.widget.Button; import android.widget.TextView; import android.widget.Toast; import com.glodon.cas.model.GlodonCas; public class MainActivity extends Activity { private Button btnGetThinkList; private static int LOGIN_REQUEST_CODE = 1; private TextView tvThinkList; private WebView webView; private Button btnRefresh; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); btnGetThinkList = (Button) findViewById(R.id.btnGetThinkList); tvThinkList = (TextView) findViewById(R.id.tvThinkList); webView = (WebView) findViewById(R.id.webView); btnGetThinkList.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if (!GlodonCas.casLogined()) { startActivityForResult(new Intent(MainActivity.this, LoginActivity.class), LOGIN_REQUEST_CODE); return; } new ThinkListTask().execute(); } }); webView.getSettings().setJavaScriptEnabled(true); webView.setWebViewClient(new WebViewClient() { public boolean shouldOverrideUrlLoading(WebView view, String url) { view.loadUrl(url); return false; } }); webView.loadUrl("http://think.grandsoft.com.cn"); btnRefresh = (Button) findViewById(R.id.btnRefresh); btnRefresh.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Listcookies = GlodonCas.getHttpClient() .getCookieStore().getCookies(); if (!cookies.isEmpty()) { Cookie sessionCookie = null; for (int i = 0; i < cookies.size(); i++) { sessionCookie = cookies.get(i); } CookieSyncManager.createInstance(MainActivity.this); CookieManager cookieManager = CookieManager.getInstance(); if (sessionCookie != null) { cookieManager.removeSessionCookie(); String cookieString = sessionCookie.getName() + "=" + sessionCookie.getValue() + "; domain=" + sessionCookie.getDomain(); cookieManager.setCookie("think.grandsoft.com.cn", cookieString); CookieSyncManager.getInstance().sync(); } System.out.println("finish"); webView.loadUrl("http://think.grandsoft.com.cn"); } } }); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { if (requestCode == LOGIN_REQUEST_CODE) { Toast.makeText(this, R.string.login_success, Toast.LENGTH_LONG) .show(); } } class ThinkListTask extends AsyncTask { @Override protected String doInBackground(Void... params) { try { HttpGet httpGet = new HttpGet( "http://think.grandsoft.com.cn/api/weekly_journals"); HttpClient httpClient = GlodonCas.getHttpClient(); HttpResponse httpRespone = httpClient.execute(httpGet); StringBuilder sb = new StringBuilder(); HttpEntity httpEntity = httpRespone.getEntity(); InputStream inputStream = httpEntity.getContent(); BufferedReader reader = new BufferedReader( new InputStreamReader(inputStream)); String line = ""; while ((line = reader.readLine()) != null) { sb.append(line); } return sb.toString(); } catch (Exception e) { e.printStackTrace(); return null; } } @Override protected void onPostExecute(String thinkList) { tvThinkList.setText(thinkList); } } }
package com.glodon.cas.activities; import java.io.BufferedReader; import java.io.InputStream; import java.io.InputStreamReader; import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpGet; import android.app.Activity; import android.content.Intent; import android.os.AsyncTask; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.Toast; import com.glodon.cas.model.GlodonCas; public class LoginActivity extends Activity { private static Button btnLogin; private static EditText etUsername; private static EditText etPassword; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_login); btnLogin = (Button) findViewById(R.id.btnLogin); etUsername = (EditText) findViewById(R.id.etUsername); etPassword = (EditText) findViewById(R.id.etPassword); btnLogin.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { new LoginTask().execute(etUsername.getText().toString(), etPassword.getText().toString()); } }); } public class LoginTask extends AsyncTask{ @Override protected Boolean doInBackground(String... args) { try { return new GlodonCas("http://think.grandsoft.com.cn") .login(args[0], args[1]); } catch (Exception e) { e.printStackTrace(); } return null; } @Override protected void onPostExecute(Boolean login_success) { if (login_success == null) { Toast.makeText(LoginActivity.this, R.string.network_error_tip, Toast.LENGTH_LONG).show(); return; } if (login_success) { Intent intent = new Intent(); LoginActivity.this.setResult(RESULT_OK, intent); LoginActivity.this.finish(); } else { Toast.makeText(LoginActivity.this, R.string.login_error_tip, Toast.LENGTH_LONG).show(); } } } }
本教程為大家分享了Android日歷庫的使用方法,供大家參考,具體內容如下MainActivity.java代碼:package siso.weekv;import an
在Android中實現異步任務機制有兩種方式,Handler和AsyncTask。Handler模式需要為每一個任務創建一個新的線程,任務完成後通過Handler實例向U
【一】常見用法最原始的用法,耦合度低,但是不能統一管理。我們需要在每一個控制器都寫以下代碼,很繁瑣,以後項目修改起來更繁瑣,得一個控制器一個控制器的去定位、修改。1.1
前言默認情況下,Android Studio設置新的項目並且部署到模擬器或者真機設備上,只需要點擊幾下。使用即時運行,你並不需要構建一個新的APK即可將改變後的方法和現有