最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Android網(wǎng)絡(luò)請(qǐng)求框架Retrofit詳解

 更新時(shí)間:2017年08月07日 10:09:23   作者:xing-java  
這篇文章主要為大家詳細(xì)介紹了Android網(wǎng)絡(luò)請(qǐng)求框架Retrofit,使用Retrofit2.0.0版本進(jìn)行實(shí)例演示,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

介紹:

Retrofit 是Square公司開(kāi)發(fā)的一款針對(duì)Android網(wǎng)絡(luò)請(qǐng)求的框架,Retrofit2底層基于OkHttp實(shí)現(xiàn)的,OkHttp現(xiàn)在已經(jīng)得到Google官方認(rèn)可,大量的app都采用OkHttp做網(wǎng)絡(luò)請(qǐng)求。本文使用Retrofit2.0.0版本進(jìn)行實(shí)例演示。

使用Retrofit可以進(jìn)行GET,POST,PUT,DELETE等請(qǐng)求方式。

同步請(qǐng)求:需要在子線程中完成,會(huì)阻塞主線程。

Response response = call.execute().body();

異步請(qǐng)求:請(qǐng)求結(jié)果在主線程中回調(diào),可以在onResponse()回調(diào)方法進(jìn)行更新UI。

call.enqueue(Callback callback)

使用步驟:

(1) 創(chuàng)建工程,添加jar:

compile 'com.squareup.retrofit2:retrofit:2.0.0'
compile 'com.squareup.retrofit2:converter-gson:2.0.0' //這兩個(gè)jar版本要一致,否則會(huì)有沖突

(2) 創(chuàng)建業(yè)務(wù)請(qǐng)求接口,具體代碼如下

/**
 * 創(chuàng)建業(yè)務(wù)請(qǐng)求接口
 */

public interface IUserService {
 /**
  * GET請(qǐng)求
  */
 @GET("Servlet/UserServlet")
 Call<User> getUser(@Query("email") String email);

 /**
  * POST請(qǐng)求
  */
 @FormUrlEncoded
 @POST("UserServlet")
 Call<User> postUser(@Field("name") String name, @Field("email") String email);
}


解釋說(shuō)明:

@GET注解表示GET請(qǐng)求,@Query表示請(qǐng)求參數(shù),將會(huì)以key=value(@Query注解參數(shù)名稱為key,調(diào)用傳進(jìn)來(lái)的值為value)的方式拼接在url后面.

@POST注解表示POST請(qǐng)求,@FormUrlEncoded將會(huì)自動(dòng)將請(qǐng)求參數(shù)的類型設(shè)置為application/x-www-form-urlencoded,@FormUrlEncoded注解不能用于Get請(qǐng)求。@Field注解將每一個(gè)請(qǐng)求參數(shù)都存放至請(qǐng)求體中,還可以添加encoded參數(shù),該參數(shù)為boolean型,具體的用法為:
@Field(value = "password", encoded = true) String pwd
encoded參數(shù)為true的話,key-value-pair將會(huì)被編碼,即將中文和特殊字符進(jìn)行編碼轉(zhuǎn)換.

(3)創(chuàng)建Retrofit對(duì)象

Retrofit retrofit = new Retrofit.Builder()
    .baseUrl(Constant.BASE_URL)
    .addConverterFactory(GsonConverterFactory.create())
    .build();
IUserService iUserService = retrofit.create(IUserService.class);


解釋說(shuō)明:

baseUrl()方法制定網(wǎng)絡(luò)請(qǐng)求的固定絕對(duì)地址,一般包括請(qǐng)求協(xié)議(如Http)、域名或IP地址、端口號(hào)。
創(chuàng)建Retrofit實(shí)例時(shí),若沒(méi)有配置addConverterFactory(GsonConverterFactory.create())將會(huì)回調(diào)出JSON字符串,配置了將會(huì)回調(diào)實(shí)體對(duì)象。

支持的JSON解析庫(kù):

Gson: compile ‘com.squareup.retrofit2:converter-gson:2.0.1'
Jackson: compile ‘com.squareup.retrofit2:converter-jackson:2.0.1'
Moshi: compile ‘com.squareup.retrofit2:converter-moshi:2.0.1'
Protobuf: compile ‘com.squareup.retrofit2:converter-protobuf:2.0.1'
Wire: compile ‘com.squareup.retrofit2:converter-wire:2.0.1'
Simple XML: compile ‘com.squareup.retrofit2:converter-simplexml:2.0.1'
Scalars (primitives, boxed, and String): compile ‘com.squareup.retrofit2:converter-scalars:2.0.1'

(4) 調(diào)用請(qǐng)求方法,并得到Call實(shí)例

Call<ResponseBody> call = iUserService.getUser(xing-java@foxmail.com);

(5) 使用Call實(shí)例完成同步或異步請(qǐng)求

/**
  * 發(fā)送GET請(qǐng)求
  */
 private void getRequest() {
  Retrofit retrofit = new Retrofit.Builder()
    .baseUrl(Constant.BASE_URL)
    .addConverterFactory(GsonConverterFactory.create())
    .build();
  IUserService iUserService = retrofit.create(IUserService.class);
  Call<User> call = iUserService.getUser("xing-java@foxmail.com");
  call.enqueue(new Callback<User>() {
   @Override
   public void onResponse(Call<User> call, Response<User> response) {

    Log.i("MainActivity", "response = " + response);
    User user = response.body();
    resTxtView.setText(user.toString());
   }

   @Override
   public void onFailure(Call<User> call, Throwable t) {

   }
  });
 }


請(qǐng)求方式:

(1)GET 請(qǐng)求:

GET 請(qǐng)求返回 JSON 字符串:

這里寫圖片描述

GET 請(qǐng)求返回實(shí)體對(duì)象:

這里寫圖片描述

(2) POST發(fā)送表單:

 /**
  * 發(fā)送POST請(qǐng)求
  */
 private void postRequest() {
  Retrofit retrofit = new Retrofit.Builder()
    .baseUrl(Constant.BASE_URL)
    .addConverterFactory(GsonConverterFactory.create())
    .build();
  IUserService iUserService = retrofit.create(IUserService.class);
  Call<User> call = iUserService.postUser("star.tao", "xing-java@foxmail.com");
  call.enqueue(new Callback<User>() {
   @Override
   public void onResponse(Call<User> call, Response<User> response) {
   }

   @Override
   public void onFailure(Call<User> call, Throwable throwable) {

   }
  });


服務(wù)端接收到的結(jié)果:

這里寫圖片描述

(3)文件上傳:

private void uploadFile() {
  Retrofit retrofit = new Retrofit.Builder()
    .addConverterFactory(GsonConverterFactory.create())
    .baseUrl(Constant.BASE_URL)
    .build();
  IUserService iUserService = retrofit.create(IUserService.class);
  File file = new File("/sdcard/s.png");
  RequestBody fileRequestBody = RequestBody.create(MediaType.parse("multipart/form-data"), file);
  MultipartBody.Part multipartBody = MultipartBody.Part.createFormData("upload_file", file.getName(), fileRequestBody);
  String desc = "this is file description";
  RequestBody descRequestBody = RequestBody.create(MediaType.parse("multipart/form-data"), desc);
  Call<ResponseBody> call = iUserService.uploadFile(descRequestBody, multipartBody);
  call.enqueue(new Callback<ResponseBody>() {
   @Override
   public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
    Log.i("debug", "upload success");
   }

   @Override
   public void onFailure(Call<ResponseBody> call, Throwable t) {

   }
  });

 }


這里寫圖片描述

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論

乡宁县| 通化市| 志丹县| 阿克| 四会市| 柏乡县| 苏尼特右旗| 子洲县| 理塘县| 铁力市| 巩留县| 察雅县| 武安市| 富顺县| 铁力市| 资中县| 五指山市| 凭祥市| 泽库县| 栖霞市| 西昌市| 旬阳县| 天祝| 新乐市| 雷波县| 玉门市| 穆棱市| 全南县| 泾川县| 丹凤县| 剑川县| 离岛区| 林西县| 阿荣旗| 新疆| 天门市| 靖州| 多伦县| 建水县| 宁明县| 家居|