日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

android网络请求框架汇总

發(fā)布時(shí)間:2023/12/29 编程问答 36 豆豆
生活随笔 收集整理的這篇文章主要介紹了 android网络请求框架汇总 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

網(wǎng)絡(luò)

使用網(wǎng)絡(luò)庫不要忘記添加網(wǎng)絡(luò)權(quán)限

2.1網(wǎng)絡(luò)_Volley

·?簡介:
Volley的中文翻譯為齊射、并發(fā),是在2013年的Google大會上發(fā)布的一款Android平臺網(wǎng)絡(luò)通信庫,具有網(wǎng)絡(luò)請求的處理、小圖片的異步加載和緩存等功能,能夠幫助Android APP 更方便地執(zhí)行網(wǎng)絡(luò)操作,而且更快速高效。

Google IO的演講上,其配圖是一幅發(fā)射火弓箭的圖,有點(diǎn)類似流星。這表示,Volley特別適合數(shù)據(jù)量不大但是通信頻繁的場景。見下圖:

https://android.googlesource.com/platform/frameworks/volley/
https://github.com/mcxiaoke/android-volley

網(wǎng)絡(luò)資料參考:http://www.dengzhr.com/others/mobile/android/762

·?特點(diǎn):

o?通信更快,更簡單

o?支持網(wǎng)絡(luò)請求的排序,優(yōu)先級處理

o?支持網(wǎng)絡(luò)請求的緩存

o?多級別的取消請求

o?擴(kuò)展性強(qiáng)(可以自己進(jìn)行相關(guān)的封裝,比如說請求失敗重試機(jī)制等)

·?配置依賴:

0.?compile'com.mcxiaoke.volley:library:1.0.19'

?

·?使用步驟:

1.?創(chuàng)建RequestQueue

1.??requestQueue=?Volley.newRequestQueue(this);

2.?創(chuàng)建Request對象

1.?//StringRequest響應(yīng)的主體為字符串

2.?//JsonArrayRequest發(fā)送和接收JSON數(shù)組

3.?//JsonObjectRequest發(fā)送和接收JSON對象

4.?//ImageRequest發(fā)送和接收Image

5.?//StringRequest

6.?//這里有兩個(gè)構(gòu)造方法,一個(gè)是有method參數(shù),一個(gè)是沒有method參數(shù)的,那么沒有method參數(shù)的默認(rèn)為GET method

7.?StringRequest(int?method,?String?url,?Listener<String>?listener,?ErrorListener?errorListener)???

8.?StringRequest(String?url,?Listener<String>?listener,?ErrorListener?errorListener)

?

3.?添加RequestRequestQueue

·?注意事項(xiàng):

o?如果自己編譯Volley的話,compileSdkVersion需要<=22,這是因?yàn)樵?/span>Android6.0Google移除了httpClient相關(guān)的API

o?Volley僅適合用于通信頻繁數(shù)據(jù)量小的網(wǎng)絡(luò)操作

o?大數(shù)據(jù)量的網(wǎng)絡(luò)操作并不適合Volley

·?工作原理圖

·?使用步驟:

0.?創(chuàng)建RequestQueue

1.?創(chuàng)建Request對象

2.?添加Request對象到RequestQueue

2.2網(wǎng)絡(luò)_Okhttp

·?主頁:?https://github.com/square/okhttp

·?OkHttp是一個(gè)高效的Http客戶端,有如下的特點(diǎn)::

o?支持HTTP/2 SPDY

o?默認(rèn)支持 GZIP 降低傳輸內(nèi)容的大小

o?支持網(wǎng)絡(luò)請求的緩存

o?當(dāng)網(wǎng)絡(luò)出現(xiàn)問題時(shí),自動重試一個(gè)主機(jī)的多個(gè)IP 地址

·?配置: 添加依賴 compile 'com.squareup.okhttp3:okhttp:3.2.0'

·?使用步驟:

0.?創(chuàng)建OkHttpClient對象

1.?創(chuàng)建Request對象

2.?添加Request對象到OkHttpClient對象中并執(zhí)行請求.示例代碼:

?

1.??OkHttpClient?okHttpClient=?new?OkHttpClient();

?

-------Get 請求---------

2.?//Get請求方式 ?

3.?//默認(rèn)情況下,Request就是是使用Get請求

4.?Request?requestGet=new?Request.Builder()

5.?????????????????.url(URL_GET)

6.?????????????????.build();

?

7.??//默認(rèn)情況下,Request就是是使用Get請求方式

8.?????????Request?requestGet=new?Request.Builder()

9.?????????????????.url(URL_GET)

10.?????????????????.build();

11.?????????//生成Call進(jìn)行同步或者異步的調(diào)用

12.?????????Call?call=?okHttpClient.newCall(requestGet);

13.?????????//同步調(diào)用

14.?// ???????Response response = call.execute();

15.?????????//異步調(diào)用

16.?????????call.enqueue(new?Callback()?{

17.?????????????@Override

18.?????????????public?void?onFailure(Call?call,?IOException?e)?{

19.?????????????????//請求失敗的時(shí)候執(zhí)行

20.?????????????}

21.?

22.?????????????@Override

23.?????????????public?void?onResponse(Call?call,?Response?response)?throws?IOException?{

24.?????????????????//網(wǎng)絡(luò)請求成功響應(yīng)

25.?????????????}

26.?????????});

?

------Post 請求---------

27.?????RequestBody?body=?new?FormBody.Builder()//請求體

28.?????????????.add("phone",?"13812345678")//構(gòu)造請求的參數(shù)

29.?????????????.add("key",?"daf8fa858c330b22e342c882bcbac622")//構(gòu)造請求的參數(shù)

30.?????????????.build();

31.?

32.?????Request?post_request=?new?Request.Builder()

33.?????????????.url(URL_POST)//指定請求的地址

34.?????????????.post(body)//指定請求的方式為POST

35.?????????????.build();

36.?????client.newCall(post_request).enqueue(new?Callback()?{

37.?????????@Override

38.?????????public?void?onFailure(Call?call,?IOException?e)?{

39.?????????????//請求失敗的處理

40.?????????}

41.?

42.?????????@Override

43.?????????public?void?onResponse(Call?call,?Response?response)?throws?IOException?{???//請求成功的處理

44.?????????????ResponseBody?body=?response.body();

45.?????????????String?string=?body.string();//把返回的結(jié)果轉(zhuǎn)換為String類型

46.?????????????// body.bytes();//把返回的結(jié)果轉(zhuǎn)換為byte數(shù)組

47.?????????????// body.byteStream();//把返回的結(jié)果轉(zhuǎn)換為流

48.?????????}

49.?????});

?

·?因?yàn)樵?/span>OkHttp的使用比較復(fù)雜,有一個(gè)包裝過的工具項(xiàng)目okhttp-utils使用非常簡單

o?添加依賴:?

1.?compile'com.zhy:okhttputils:2.3.8'

?

o?工具類簡介:https://github.com/hongyangAndroid/okhttp-utils
代碼寫起來很簡單,如下:

2.??OkHttpUtils

3.?????????????????.get()//如果是Post請求,用.post()

4.?????????????????.url(URL)

5.?????????????????.addParams("key","value")

6.?????????????????.build()

7.?????????????????.execute(new?StringCallback()?{

8.?????????????????????@Override

9.?????????????????????public?void?onError(Call?call,?Exception?e)?{

10.?????????????????????????//可以直接在這里進(jìn)行UI的操作

11.?????????????????????????//網(wǎng)絡(luò)訪問錯(cuò)誤

12.?????????????????????}

13.?

14.?????????????????????@Override

15.?????????????????????public?void?onResponse(String?response)?{

16.?????????????????????????//可以直接在這里進(jìn)行UI的操作

17.?????????????????????????//網(wǎng)絡(luò)正常邏輯

18.?????????????????????}

19.?????????????????});

?

2.3網(wǎng)絡(luò)_Retrofit

·?主頁:?https://github.com/square/retrofit

·?功能:

o?效率非常高

o?可以直接將結(jié)果轉(zhuǎn)換稱Java

o?可以配合RxJava一起使用

·?配置:

o?添加Retrofit依賴: compile 'com.squareup.retrofit2:retrofit:2.0.2'

o?添加數(shù)據(jù)解析依賴,根據(jù)實(shí)際情況進(jìn)行選擇

§?Gson : com.squareup.retrofit2:converter-gson:2.0.2

§?Jackson : com.squareup.retrofit2:converter-jackson:2.0.2

§?Moshi : com.squareup.retrofit2:converter-moshi:2.0.2

§?Protobuf : com.squareup.retrofit2:converter-protobuf:2.0.2

§?Wire : com.squareup.retrofit2:converter-wire:2.0.2

§?Simple XML : com.squareup.retrofit2:converter-simplexml:2.0.2

·?使用步驟:

0.?創(chuàng)建JsonBean(可以用工具生成)

1.?Http Api 轉(zhuǎn)化為 java interface.

0.??/**

1.??????*對應(yīng)Http接口的Java Interface

2.??????*/

3.?????interface?HttpApi?{

4.?

5.?????????//網(wǎng)絡(luò)請求方式,請求Url

6.?????????@GET("home")

7.?????????Call<HomeResponse>?getHomeData();

8.?

9.?????????/*GET有參數(shù)請求:

10.?????????????1、可以在url后面直接進(jìn)行參數(shù)拼接,比如 ???@GET("home?key=value&key2=value2")

11.?????????????2、可以用@Query進(jìn)行參數(shù)設(shè)置,可以有多個(gè)

12.?????????????3、可以用@QueryMap進(jìn)行參數(shù)設(shè)置用Map集合進(jìn)行參數(shù)

13.?????????????*/

14.?????????@GET("home")

15.?????????Call<HomeResponse>?getHomeData(@Query("key1")?String?value1,?@Query("key2")?String?value2);

16.?

17.?????????@GET("home")

18.?????????Call<HomeResponse>?getHomeData(@QueryMap?Map<String,?String>?params);

19.?

20.?

21.?????????/*POST有參數(shù)請求:

22.??????????1、可以用@Field進(jìn)行參數(shù)設(shè)置,可以有多個(gè)

23.??????????2、可以用@FieldMap進(jìn)行參數(shù)設(shè)置用Map集合進(jìn)行參數(shù)

24.??????????*/

25.?????????@FormUrlEncoded

26.?????????@POST("search")

27.?????????Call<SearchResponse>?search(

28.?????????????????@Field("keyword")?String?keyword,

29.?????????????????@Field("page")?String?page,

30.?????????????????@Field("pageNum")?String?pageNum,

31.?????????????????@Field("orderby")?String?orderby

32.?????????);

33.?

34.?????????@FormUrlEncoded

35.?????????@POST("search")

36.?????????Call<SearchResponse>?search(

37.?????????????????@FieldMap?Map<String,?String>?params

38.?????????);

39.?

40.?

41.?????}

?

?

§?

0.?創(chuàng)建Retrofit對象,并發(fā)起請求.示例代碼:

0.???//3.1創(chuàng)建Retrofit實(shí)例,進(jìn)行接口實(shí)現(xiàn)

1.?????????retrofit=?new?Retrofit

2.?????????????????.Builder()

3.?????????????????.baseUrl(baseUrl)

4.?????????????????.addConverterFactory(GsonConverterFactory.create())

5.?????????????????.build();

6.?????????//3.2實(shí)例化接口類

7.?????????HomeApi?homeApi=?retrofit.create(HomeApi.class);

8.?????????//3.3調(diào)用接口的方法得到Call

9.?????????Call<HomeResponse>?homeResponseCall=?homeApi.getHomeData();

10.?????????//3.4執(zhí)行數(shù)據(jù)的請求操作,可以用同步或者是異步的操作

11.?????????//3.4.1同步操作,耗時(shí)操作需要在線程中執(zhí)行

12.?????????/*try {

13.?????????????Response<HomeResponse> homeResponseResponse = homeResponseCall.execute();

14.?????????????HomeResponse homeResponse = homeResponseResponse.body();

15.?

16.?????????} catch (IOException e) {

17.?????????????e.printStackTrace();

18.?????????}*/

19.?

20.?????????//3.4.2異步的操作,okhttp的異步執(zhí)行不同的是,可以在回掉的方法中進(jìn)行UI的控制操作

21.?????????homeResponseCall.enqueue(new?Callback<HomeResponse>()?{

22.?????????????@Override

23.?????????????public?void?onResponse(Call<HomeResponse>?call,?Response<HomeResponse>?response)?{

24.?????????????????HomeResponse?homeResponse=?response.body();

25.?????????????????textView.setText(homeResponse.toString());

26.?????????????????textView.setTextColor(Color.GREEN);

27.?????????????}

28.?

29.?????????????@Override

30.?????????????public?void?onFailure(Call<HomeResponse>?call,?Throwable?t)?{

31.?????????????????textView.setText(t.getMessage());

32.?????????????????textView.setTextColor(Color.RED);

33.?????????????}

34.?????????});

?

·?常用注解:

o?請求方法:@GET / @POST / @PUT / @DELETE / @HEAD

o?URL處理

1、拼接注意,建議baseUrl“/”結(jié)尾,接口中url不用"/"開頭

例子:baseUrl=http://10.0.2.2:8080/market/? ??? ? url=home

錯(cuò)誤案例

0.?案例一:

1.?baseUrl=http://10.0.2.2:8080/market ???????url=home

2.?--->http://10.0.2.2:8080/home

3.?分析:默認(rèn)用最后一個(gè)斜線去拼接

4.?

5.?案例二:

6.?baseUrl=http://10.0.2.2:8080/market ???????url=/home

7.?--->http://10.0.2.2:8080/home

8.?分析:url中開始的斜線代表主機(jī)地址http://10.0.2.2:8080

?

2、注解:

§?@Path - 替換參數(shù)

1.?@GET("{path1}/{path2}")//注意:如果路徑用“/”分割,就需要使用多個(gè)參數(shù)表示

2.?public?Call<ResponseData>?groupList(@Path("path1")?String?path1,@Path("path2")?String?path2);

§?@Query - 添加查詢參數(shù),在GET請求中使用

3.??@GET("home")

4.??Call<HomeResponse>?getHomeData(@Query("key1")?String?value1,?@Query("key2")?String?value2);

?

§?@QueryMap - 如果有多個(gè)查詢參數(shù),把它們放在Map中,在GET請求中使用

5.??@GET("home")

6.??Call<HomeResponse>?getHomeData(@QueryMap?Map<String,?String>?params);

?

o?

§?@Field單個(gè)字段,@FieldMap多個(gè)字段,把它們放在Map中,在Post請求中使用

@FormUrlEncoded 編碼控制 與Post請求結(jié)合使用,不能缺少。

7.??@FormUrlEncoded

8.??@POST("search")

9.??Call<SearchResponse>?search(

10.??????????@Field("keyword")?String?keyword,

11.??????????@Field("page")?String?page,

12.??????????@Field("pageNum")?String?pageNum,

13.??????????@Field("orderby")?String?orderby

14.??);

15.??@FormUrlEncoded

16.??@POST("search")

17.??Call<SearchResponse>?search(

18.??????????@FieldMap?Map<String,?String>?params

19.??);

?

拓展:Retrofitokhttp進(jìn)化而來的,那么也是支持返回ResponseBody類型的,我們也可以直接使用返回?cái)?shù)據(jù)類型ResponseBody來進(jìn)行格式化字符串的獲取。

20.?//接口中的方法

21.?@GET("home")

22.?Call<ResponseBody>?getHomeDataStr();

?

23.?

24.???//直接獲取json字符串

25.?????????retrofit.create(HttpApi.class)

26.?????????????????.getHomeDataStr()

27.?????????????????.enqueue(new?Callback<ResponseBody>()?{

28.?????????????????????@Override

29.?????????????????????public?void?onResponse(Call<ResponseBody>?call,?Response<ResponseBody>?response)?{

30.?????????????????????????String?resultStr=null;

31.?????????????????????????if?(response.isSuccessful())?{

32.?????????????????????????????ResponseBody?body=?response.body();

33.?????????????????????????????try?{

34.?????????????????????????????????resultStr=?body.string();

35.?????????????????????????????}?catch?(IOException?e)?{

36.?????????????????????????????????e.printStackTrace();

37.?????????????????????????????}

38.?????????????????????????}

39.?????????????????????}

40.?

41.?????????????????????@Override

42.?????????????????????public?void?onFailure(Call<ResponseBody>?call,?Throwable?t)?{

43.?

44.?????????????????????}

45.?????????????????});

?

?

·?優(yōu)點(diǎn):?

·?

?

總結(jié)

以上是生活随笔為你收集整理的android网络请求框架汇总的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。