Day05 數據存儲及多線程斷點續傳
1.數據提交到服務器兩種方式的優缺點
* GET請求
優勢:使用很是方便,只須要在url後面組拼數據。
缺點:數據在url的後面組拼,不安全。有數據長度限制。
* POST請求
優勢:安全,數據不是在url後面組拼而是經過流的方式寫給服務器。數據長度不受限制
缺點:編寫麻煩。android
2.數據提交
* GET請求
1. 須要在url後面組拼提交的數據
* POST請求
1. 不須要組拼任何的數據
2. 必須指定請求的數據類型,是一個通過url編碼的表單數據。Content-Type
3. 以流的方式把數據寫給服務器,因此必須指定提交數據的長度。Content-Length
重要方法:設置給服務器寫的數據的長度:conn.setRequestProperty("Content-Length", String.valueOf(data.length()));
記得指定要給服務器寫數據:conn.setDoOutput(true);web
3.中英文亂碼問題的解決及細節
Tomcat默認碼錶iso-8859-1
Tomcat若是發現字符串不識別,就默認採用本地碼錶
byte[] bytes = string.getBytes(String charsetName)
將字符串按指定的編碼轉化爲byte數組,默認採用本地碼錶
new String(byte[] bytes, String charsetName)
將byte數組按指定的編碼轉化爲字符串
//提交的數據中含有中文時,將字符串qq按照編碼UTF-8進行編碼
客戶端
URLEncoder.encode(qq, "UTF-8");
服務端
String qq = request.getParameter("qq");//tomcat採用的編碼是iso-8859-1
System.out.println("qq:"+new String(qq.getBytes("iso-8859-1"),"utf-8"));數組
4.httpclient
get請求
//1.打開瀏覽器
HttpClient client = new DefaultHttpClient();
//2.輸入地址或者數據
HttpGet httpGet = new HttpGet(path);
//3.敲回車
HttpResponse response = client.execute(httpGet);
//獲取狀態碼
int code = response.getStatusLine().getStatusCode();
post方式
//1.打開瀏覽器
HttpClient client = new DefaultHttpClient();
//2.輸入地址或者數據
HttpPost httpPost = new HttpPost(path);
List<NameValuePair> parameters = new ArrayList<NameValuePair>();
parameters.add(new BasicNameValuePair("qq", qq));
parameters.add(new BasicNameValuePair("pwd", pwd));
httpPost.setEntity(new UrlEncodedFormEntity(parameters, "utf-8"));
//3.敲回車
HttpResponse response = client.execute(httpPost);
//獲取狀態碼
int code = response.getStatusLine().getStatusCode();
5.async-http開源框架
GET請求
String path = "http://192.168.1.103:8080/web/LoginServlet?qq="+URLEncoder.encode(qq)+"&pwd="+URLEncoder.encode(pwd);
AsyncHttpClient client = new AsyncHttpClient();
client.get(path, new AsyncHttpResponseHandler()
POST請求
String path = "http://192.168.1.103:8080/web/LoginServlet";
AsyncHttpClient client = new AsyncHttpClient();
RequestParams params = new RequestParams();
params.put("qq", qq);
params.put("pwd", pwd);
client.post(path, params, new AsyncHttpResponseHandler()
請求成功調用onsuccess()方法 請求失敗調用onFailure()方法
文件上傳的時候
put添加的是一個file對象瀏覽器
6.多線程下載用到的重要的API
//1.在客戶端本地建立一個空白文件,文件的大小跟服務器的如出一轍
RandomAccessFile raf = new RandomAccessFile(getFileName(path), "rw");
raf.setLength(length);
raf.close();緩存
//3.第i個線程只取服務器中的某一段數據
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestProperty("Range", "bytes="+startIndex+"-"+endIndex);
InputStream is = conn.getInputStream();tomcat
//4.第i個線程取回數據後,從某個位置開始寫入數據
RandomAccessFile raf = new RandomAccessFile(getFileName(path), "rw");
raf.seek(startIndex);//每一個線程寫文件的開始位置都是不同的.安全
保證同步使用synchronize代碼塊服務器
使用FileOutputStream數據不必定每一次都寫到存儲設備裏,有可能寫到硬盤的緩存裏,使用RandomAccessFile將模式設置爲rwd,能夠保證每次都將數據寫到磁盤裏
HttpUtils http = new HttpUtils();
//第一個參數:服務器地址
//第二個參數:要下載到哪裏
//是否斷點續傳
//下載的一些回調函數
http.download(path, "/mnt/sdcard/xxx.exe", true, new RequestCallBack<File>()
//下載成功的回調:onSuccess(ResponseInfo<File> arg0)
//進度更新的回調:onLoading(long total, long current, boolean isUploading)
//下載失敗的回調:onFailure(HttpException arg0, String arg1)數據結構
day06activity多線程
一、Android中開發一個界面的過程
界面在Android中對應的類爲Activity
建立一個類繼承Activity,在onCreate方法中經過setcontentView(R.layout.xxx)方法設置該界面的佈局文件
在清單文件AndroidManifest.xml的application節點中配置該Activity
<activity
android:name="com.itheima.mutliuiapp.MainActivity" //配置類的全類名,必須配置
android:label="@string/app_name" > //標題欄上顯示的文字,能夠不配置
//應用程序的啓動頁面必須配置以下intent-filter節點,action和category缺一不可
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
2.如何設置Activity沒有標題欄
在清單文件中配置單個activity節點的主題
<activity
android:theme="@android:style/Theme.Light.NoTitleBar"
android:name="com.itheima.rpcalc.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
在清單文件中配置整個application節點的主題,在此配置後,該應用全部界面都沒有標題欄
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
//1.找到主題對應的style文件,按住ctrl鍵用鼠標左鍵點擊便可
android:theme="@style/AppTheme" >
...
...
</application>
//2.在對應的style文件中添加對應的條目
<style name="AppTheme" parent="AppBaseTheme">
<item name="android:windowNoTitle">true</item>
</style>
在Activity的onCreate中設置
requestWindowFeature(Window.FEATURE_NO_TITLE);
3.如何進行界面的跳轉?
顯示意圖
Intent intent = new Intent();//意圖對象
intent.setClass(this, SecondActivity.class); //SecondActivity.class 要跳轉到的界面
startActivity(intent);
如何關閉一個Activity
調用finish()方法
不要在主線程(UI線程)中使用Thread.sleep()方法,會讓界面卡住
4.Activity 跳轉時如何傳遞數據
第一個界面,傳遞數據
Intent intent = new Intent(this, ResultActivity.class);
//添加要傳遞的數據,可傳遞的數據類型有:
//1. 8個基本數據類型及其數組:byte、short、int、long、float、double、boolean、char及他們的數組
//2. 字符串及其數組:CharSequence及其數組、String及其數組
//3. 集合:CharSequence的ArrayList、String的ArrayList、Integer的ArrayList
//4. 對象:Serializable、Parcelable及其數組、繼承自Parcelable的ArrayList
intent.putExtra(key,value);
startActivity(intent);
第二個界面,接收數據
//獲取傳遞過來的數據
Intent intent = getIntent();
intent.getXXXExtra();
5.顯式意圖&隱式意圖
顯式意圖,必需要指定被開啓的Activity的類名或者路徑名。激活本身應用程序內部的組件,推薦使用顯式意圖,效率高
Intent intent = new Intent();
intent.setClassName("com.itheima.twointent", "com.itheima.twointent.Activity01");
startActivity(intent);
隱式意圖,只須要指定action(動做) 和 data (數據)就能夠了。通常用來激活別的應用程序的界面,或者是本身應用程序的某個界面須要暴露給別的應用程序調用,效率低.
Intent intent = new Intent();
intent.setAction("com.itheima.twointent.OPEN02");
intent.addCategory("android.intent.category.DEFAULT");
startActivity(intent);
Activity若想被隱式意圖激活,須要配置意圖過濾器
<activity android:name="com.itheima.twointent.Activity02">
<intent-filter >
<action android:name="com.itheima.twointent.OPEN02"/>
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
</activity>
6.隱式意圖的詳細參數
Android系統會根據隱式意圖中設置的動做(action)、類別(category)、數據(URI和數據類型)找到最合適的組件來處理這個意圖。
setData()和setType()要一塊兒使用時,要使用setDataAndType()方法
7.打開系統應用某個界面的思路
找到該應用源碼
在logcat中查看該界面是哪一個Activity
在源碼的清單文件中查找該Activity,查看其隱式意圖
利用隱式意圖激活該Activity
8.ListView條目的點擊事件
lv.setOnItemClickListener(new OnItemClickListener() {
//當listview的條目被點擊的時候調用的方法
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//parent:該ListView
//view:該行的View
//position:點擊的是第幾行,從0開始
}
});
9.開啓另外一個Activity獲取數據的步驟
前提:A開啓B,想從B獲取數據
A調用startActivityForResult
Intent intent = new Intent(this, ContactListActivity.class);
startActivityForResult(intent, 2);
B界面設置數據
//把當前界面的數據,返回給開啓個人界面.
Intent data = new Intent();
data.putExtra("message", message);
setResult(0, data);
//把當前界面關閉
finish();
返回A界面,系統會調用onActivityResult()方法
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 1) {
System.out.println("咱們開啓的新的選擇短信的界面被關閉了,結果數據返回到這裏");
if (data != null) {
String message = data.getStringExtra("message");
et_message.setText(message);
}
}else if(requestCode == 2) {
System.out.println("咱們開啓的新的選擇聯繫人界面被關閉了,結果數據返回到這裏");
}
super.onActivityResult(requestCode, resultCode, data);
}
10.請求碼與結果碼
請求碼:若是A中屢次調用startActivityForResult(),從其餘界面獲取數據,可使用請求碼進行區分
結果碼:B中能夠設置結果碼,來通知A返回結果是否成功或其餘信息
11.Activity的生命週期
對象從建立到被垃圾回收過程當中必定會執行的方法就叫作生命週期方法.
onCreate 被建立
onDestroy 被銷燬
onStart 可見
onStop 不可見
onResume 獲取焦點
onPause 失去焦點
onRestart 界面不可見以後,再次回到該界面
12.生命週期的重要概念
entire lifetime
完整生命週期 oncreate--onstart--onresume--onpause--onstop--ondestroy
visible lifetime
可視生命週期 onstart--->onresume---onpause----ondestroy
foreground lifetime
前臺生命週期 onresume ---> onpause
13.如何處理橫豎屏切換
指定屏幕朝向
在清單文件對應的Activity中配置android:screenOrientation=」landscape」(橫屏,portrait是豎屏);
設置屏幕旋轉時不從新建立Activity
在清單文件對應的Activity中配置android:configChanges="keyboardHidden|orientation|screenSize",最好這三個都配置,不然不能適配全部機型或sdk版本。
橫豎屏切換時會走Activity的onConfigurationChanged()方法
@Override
public void onConfigurationChanged(Configuration newConfig) {
// 當新設置中,屏幕布局模式爲橫排時
if(newConfig.orientation == ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE){
//TODO 某些操做
}else{
//TODO 某些操做
}
super.onConfigurationChanged(newConfig);
14.任務棧
task stack
task: 一個應用程序通常包括多個Activity,每一個Activity都是用來處理用戶交互的一個任務
stack: 棧,是一總特殊的數據結構(後進先出)。隊列要是一種特殊的數據結構(先進先出)
任務棧就是用來記錄用戶的操做的,記錄的是Activity打開的前後順序,後打開的界面先關閉,若是整個任務棧裏面打開的Activity都被關閉了,就是應用程序被退出了.
一個應用程序通常只有一個任務棧,可是也可能對應有多個任務棧
15.Activity的啓動模式1.standard:標準的啓動模式默認應用場景 2.singleTop:單一頂部模式若是Activity已經被開啓,而且處於任務棧的棧頂,就不會建立新的Activity,而是複用這個已經開啓的Activity。爲了防止出現一些奇怪的用戶體驗,推薦使用單一頂部模式,整個任務棧能夠有多個實例存在.應用場景:短信發送界面3.singletask:單一任務棧在整個任務棧裏面只容許有一個當前Activity的實例存在若是要開啓的Activity在任務棧中已經存在,直接複用這個已經存在的Activity,而且把這個Activity上面的全部的其餘Activity給清空應用場景:若是一個Activity很是消耗內存和cpu資源,建議把這個Activity作成singletask的模式。瀏覽器的browserActivity 4.singleinstance:單一實例.整個手機操做系統只有一個實例存在,而且是運行在本身單獨的任務棧裏面.應用場景:通話界面的Activity