Android學(xué)習(xí)中網(wǎng)絡(luò)是必不可少的一部分,甚至是重中之重。開發(fā)中可能我們會使用一些很成熟優(yōu)秀的網(wǎng)絡(luò)框架刑然,如:OkHttp,Retrofit暇务,Volley等泼掠。圖片庫:Fresco,Glide垦细,Picasso择镇,ImageLoader等。為了深入的學(xué)習(xí)一起來打造一個屬于自己的網(wǎng)絡(luò)庫吧括改。
HttpURLConnetion
HttpURLConnetion
是Java
的一個類腻豌,當(dāng)然Java
的JDK
中的也是有HttpURLConnetion
類的,Android SDK
中HttpURLConnection
進(jìn)行了很多修改嘱能,所以很多Android
中和Java
同名的類是不一樣的吝梅。
我們可以用HttpURLConnetion
來實現(xiàn)Android
的網(wǎng)絡(luò)請求。HttpURLConnetion
類中設(shè)置了7種請求方法:OPTIONS
惹骂,GET
苏携,HEAD
,POST
对粪,PUT
右冻,DELETE
,TRACE
著拭。
-
OPTIONS
返回服務(wù)器針對特定資源所支持的HTTP請求方法
纱扭。 -
HEAD
向服務(wù)器索要與GET
請求相一致的響應(yīng),只不過響應(yīng)體(body)
將不會被返回茫死。這一方法可以在不必傳輸整個響應(yīng)內(nèi)容的情況下跪但,就可以獲取包含在響應(yīng)消息頭中的元信息。 -
GET
向特定的資源發(fā)出請求。注意:GET
方法不應(yīng)當(dāng)被用于產(chǎn)生副作用
的操作中屡久。其中一個原因是GET
可能會被網(wǎng)絡(luò)蜘蛛等隨意訪問忆首。 -
POST
向指定資源提交數(shù)據(jù)進(jìn)行處理請求(例如提交表單或者上傳文件)。數(shù)據(jù)被包含在請求體中
被环,POST
請求可能會導(dǎo)致新的資源的建立或已有資源的修改糙及。 -
PUT
向指定資源位置上傳其最新內(nèi)容。 -
DELETE
請求服務(wù)器刪除Request-URI
所標(biāo)識的資源筛欢。 -
TRACE
回顯服務(wù)器收到的請求浸锨,主要用于測試或診斷。
雖然有這么多的請求方式版姑,但是常用的就只有GET
和POST
柱搜,圖片
(網(wǎng)絡(luò)上的一個文件地址)的請求方式也正好是GET
。
經(jīng)過HttpURLConnetion
簡單的進(jìn)行封裝后得到
public class Net {
static Net instance;
private Net(){};
public static Net getInstance(){
if(instance==null){
instance=new Net();
}
return instance;
}
public interface CallBack{
void onCallBack(String result);
}
public interface BitmapCallBack{
void onBitmapCallBack(Bitmap bitmap);
}
//get請求:這里的請求參數(shù)必須寫在string url里面剥险,通過URL realurl=new URL(url);來訪問地址
public void netGet(final String url ,final Map<String,String> params,final CallBack callBack){
final Handler handler=new Handler();
new Thread(new Runnable() {
@Override
public void run() {
try {
String requestData="";
if(params==null){
requestData=url;
}
if(params!=null){
requestData=url+"?";
for(Entry<String,String> entry:params.entrySet()){
requestData+=URLEncoder.encode(entry.getKey(),"UTF-8")+
"="+URLEncoder.encode(entry.getValue(),"UTF-8")+"&";
}
}
URL realurl=new URL(requestData);
HttpURLConnection connection= (HttpURLConnection) realurl.openConnection();
//貌似默認(rèn)的是get請求
connection.setRequestMethod("GET");
//獲取輸入流(inputstream和outputstream是其他字節(jié)流的父類(抽象類)是不能直接new的
// 必須繼承他聪蘸,實例化它的子類)
InputStream in=connection.getInputStream();
ByteArrayOutputStream out=new ByteArrayOutputStream();
int a=0;
byte[] bytes=new byte[1024*20];
while ((a=in.read(bytes))!=-1){
out.write(bytes,0,a);
}
in.close();
out.flush();
out.close();
//toByteArray:返回這個流的當(dāng)前內(nèi)容作為一個字節(jié)數(shù)組。
final String result=new String(out.toByteArray());
handler.post(new Runnable() {
@Override
public void run() {
callBack.onCallBack(result);
}
});
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}).start();
}
//post請求表制,這里的請求參數(shù)需要方法對應(yīng)的輸出流里面去請求網(wǎng)絡(luò)(httpurlconnection)對應(yīng)的outputstream
public void netPost(final String url, final Map<String,String> params, final CallBack callBack){
final Handler handler=new Handler();
new Thread(new Runnable() {
@Override
public void run() {
try{
InputStream in = null;
URL realurl = null;
String result = null;
String requestData="";
HttpURLConnection conn = null;
realurl = new URL(url);
conn = (HttpURLConnection)realurl.openConnection();
//post請求必須設(shè)置為true這下面的兩個
conn.setDoOutput(true);
conn.setDoInput(true);
conn.setRequestMethod("POST");
if(params!=null){
for(Entry<String,String> entry:params.entrySet()){
requestData+= "&"+
URLEncoder.encode(entry.getKey(),"UTF-8")
+"="
+ URLEncoder.encode(entry.getValue(),"UTF-8");
}
}
//取出網(wǎng)絡(luò)對應(yīng)的輸出流健爬,這里轉(zhuǎn)化為了字符流
PrintWriter pw = new PrintWriter(conn.getOutputStream());
pw.print(requestData);
pw.flush();
pw.close();
// //這里也可以不轉(zhuǎn)化為字符流,直接寫出字節(jié)流
// OutputStream out=conn.getOutputStream();
// out.write(requestData.getBytes());
//獲取對應(yīng)的輸入流么介,然后取到返回的數(shù)據(jù)
in = conn.getInputStream();
BufferedReader bin = new BufferedReader(
new InputStreamReader(in));
String line;
while ((line = bin.readLine()) != null) {
result += line;
}
in.close();
final String finalResult = result;
handler.post(new Runnable() {
@Override
public void run() {
//這里通過回調(diào)去處理返回的數(shù)據(jù)
callBack.onCallBack(finalResult);
}
});
}catch(Exception eio){
eio.printStackTrace();
}
}
}).start();
}
public void netBitmap(final String url, final BitmapCallBack callBack){
final Handler handler=new Handler();
new Thread(new Runnable() {
@Override
public void run() {
URL realurl = null;
try {
realurl = new URL(url);
HttpURLConnection connection = (HttpURLConnection) realurl.openConnection();
//貌似默認(rèn)的是get請求
connection.setRequestMethod("GET");
connection.setDoInput(true);
connection.connect();
//獲取輸入流(inputstream和outputstream是其他字節(jié)流的父類(抽象類)是不能直接new的
// 必須繼承他娜遵,實例化它的子類)
InputStream in = connection.getInputStream();
final Bitmap bitmap = BitmapFactory.decodeStream(in);
in.close();
handler.post(new Runnable() {
@Override
public void run() {
callBack.onBitmapCallBack(bitmap);
}
});
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (ProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}).start();
}
}
這樣一個簡單的GET,POST壤短,圖片加載就做好了设拟,調(diào)用方式
Net.getInstance().netGet(url,null,new Net.CallBack() {
@Override
public void onCallBack(String result) {
Gson gson=new Gson();
Data datas=gson.fromJson(result,Data.class);
if(datas!=null){
for(int i=0;i<datas.data.length;i++){
ResponseData responseData=datas.data[i];
responseDataList.add(responseData);
}
}
}
});
Android
中網(wǎng)絡(luò)不能在UI線程
請求數(shù)據(jù),所以在進(jìn)行網(wǎng)絡(luò)數(shù)據(jù)請求的時候必須要開啟新的線程鸽扁∷庹溃考慮到APP
可能有大量的網(wǎng)絡(luò)數(shù)據(jù)請求,過多的數(shù)據(jù)請求桶现,線程的不斷創(chuàng)建都是對手機(jī)電量和流量的考驗,這樣的情況下我們需要考慮緩存鼎姊,對于網(wǎng)絡(luò)請求使用服務(wù)器端的緩存
骡和,對于圖片則使用內(nèi)存緩存
和硬盤緩存
,即節(jié)省了流量和電量相寇,還提高的加載速度慰于,能有效避免OOM
的發(fā)生。
針對上面的問題我們需要有請求隊列
唤衫,緩存隊列
婆赠,線程池
,內(nèi)存緩存(LruCache)
佳励,硬盤緩存(DiskLruCache)
等休里。
網(wǎng)絡(luò)數(shù)據(jù)請求
流程圖
重要的類
-
HttpRequest
:網(wǎng)絡(luò)請求的入口類蛆挫,通過它發(fā)出請求,然后判斷是否有緩存妙黍,如果有緩存加入緩存隊列悴侵,如果沒有緩存加入 請求隊列。 -
ServerCache
:數(shù)據(jù)緩存類拭嫁,存儲和讀取網(wǎng)絡(luò)數(shù)據(jù)可免。 -
ServerCacheDispatcher
:緩存調(diào)度類,維持了緩存隊列做粤,負(fù)責(zé)緩存任務(wù)浇借。 -
RequestDispatcher
:內(nèi)部維持了請求隊列,負(fù)責(zé)網(wǎng)絡(luò)請求任務(wù)怕品。 -
RestHttpRequest
:通過動態(tài)代理的方式模仿了Retrofit
逮刨。
圖片的加載
流程圖
重要的類
-
HttpRequestImage
:圖片請求的入口類。 -
MemoryCache
:內(nèi)存緩存類堵泽,負(fù)責(zé)圖片在內(nèi)存中的讀取和存儲修己。 -
DiskCahce
:硬盤緩存類,負(fù)責(zé)圖片在硬盤中的讀取和存儲迎罗。 -
CacheDispatcher
:緩存調(diào)度類睬愤,內(nèi)存維持了緩存隊列。
源碼
由于項目代碼量不少纹安,不方便挨著貼代碼出來講解尤辱,下面是項目地址,歡迎start
后clone
下來看源碼進(jìn)行學(xué)習(xí)厢岂。
**項目地址 https://github.com/llxdaxia/RestHttp **光督,歡迎大家一起學(xué)習(xí),探討塔粒,后期架構(gòu)和新需求持續(xù)更新结借。。卒茬。船老。