国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 系統(tǒng) > Android > 正文

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

2019-10-22 18:30:37
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

介紹:

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 字符串:

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

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

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

(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é)果:

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

(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) {   }  }); }

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

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


注:相關(guān)教程知識(shí)閱讀請(qǐng)移步到Android開(kāi)發(fā)頻道。
發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 铜川市| 衡东县| 民权县| 富顺县| 临沭县| 郓城县| 迭部县| 宜昌市| 营口市| 忻城县| 卢湾区| 灵台县| 桦南县| 东兰县| 军事| 中阳县| 武宁县| 武胜县| 丹棱县| 离岛区| 辽宁省| 高雄市| 泰安市| 恭城| 惠水县| 万年县| 明水县| 丁青县| 宜良县| 东台市| 广河县| 鸡西市| 乐都县| 竹山县| 蓝田县| 长寿区| 长丰县| 额尔古纳市| 青龙| 沧源| 蒙阴县|