云炬Android开发笔记 5-3,4Restful请求的处理
2.Restful請(qǐng)求的處理-框架
【說(shuō)明】首先要考慮網(wǎng)絡(luò)請(qǐng)求的參數(shù)(url傳入的值、文件、回調(diào)、及l(fā)oder加載圈)
【說(shuō)明】使用建造者模式,將建造者類和宿主類分開(kāi);
2.1【新建建造者類】
?
2.2【restClient類的參數(shù)的定義】
【restClient類的參數(shù)的定義】restClient類在每次Builder的時(shí)候會(huì)生成全新的實(shí)例,而里面的參數(shù)一次更改完畢,不允許二次更改;
?
?
2.3 【回調(diào)類】
【回調(diào)類】在網(wǎng)路請(qǐng)求之后,會(huì)存在網(wǎng)絡(luò)請(qǐng)求之后的回調(diào),比如:請(qǐng)求失敗、請(qǐng)求異常、請(qǐng)求成功等;
[新建CallBack包,書(shū)寫需要調(diào)用的接口]
?
?
2.4 完善RestClient
【完善com.flj.latte.net.RestClient】以Builder的形式構(gòu)造出來(lái)了;
?
2.5?RestClientBuilder 對(duì)數(shù)據(jù)的設(shè)置
【說(shuō)明】主要完成的數(shù)據(jù)的傳遞
?
?
?
2.6?RestClient的調(diào)用
?
2.7?RestClientBuilder的改進(jìn)
?2.7.1【改進(jìn)1】【mParams】參數(shù)每次都會(huì)構(gòu)建,比較繁瑣;
?
【優(yōu)化1】
【優(yōu)化方法2】
【client修改】
?
2.8 requset請(qǐng)求
【新建callBack類】新建類并實(shí)現(xiàn)實(shí)現(xiàn)接口, 復(fù)寫方法;
【部分源碼】com.flj.latte.net.callback.RequestCallbacks
1 package com.flj.latte.net.callback;2 3 import android.os.Handler;4 5 import com.flj.latte.app.ConfigKeys;6 import com.flj.latte.app.Latte;7 import com.flj.latte.net.RestCreator;8 import com.flj.latte.ui.loader.LatteLoader;9 import com.flj.latte.ui.loader.LoaderStyle; 10 11 import retrofit2.Call; 12 import retrofit2.Callback; 13 import retrofit2.Response; 18 19 public final class RequestCallbacks implements Callback<String> { 20 21 private final IRequest REQUEST; 22 private final ISuccess SUCCESS; 23 private final IFailure FAILURE; 24 private final IError ERROR; 25 private final LoaderStyle LOADER_STYLE; 26 private static final Handler HANDLER = Latte.getHandler(); 27 28 public RequestCallbacks(IRequest request, ISuccess success, IFailure failure, IError error, LoaderStyle style) { 29 this.REQUEST = request; 30 this.SUCCESS = success; 31 this.FAILURE = failure; 32 this.ERROR = error; 33 this.LOADER_STYLE = style; 34 } 35 36 @Override 37 public void onResponse(Call<String> call, Response<String> response) { 38 if (response.isSuccessful()) { 39 if (call.isExecuted()) { 40 if (SUCCESS != null) { 41 SUCCESS.onSuccess(response.body()); 42 } 43 } 44 } else { 45 if (ERROR != null) { 46 ERROR.onError(response.code(), response.message()); 47 } 48 } 49 50 onRequestFinish(); 51 } 52 53 @Override 54 public void onFailure(Call<String> call, Throwable t) { 55 if (FAILURE != null) { 56 FAILURE.onFailure(); 57 } 58 if (REQUEST != null) { 59 REQUEST.onRequestEnd(); 60 } 61 62 onRequestFinish(); 63 }?【完善RestClient】?
?
?
2.9 使用方法
【測(cè)試】
【增加權(quán)限】
【測(cè)試】
【效果】通過(guò)get請(qǐng)求返回了數(shù)據(jù)
?
總結(jié)
以上是生活随笔為你收集整理的云炬Android开发笔记 5-3,4Restful请求的处理的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: Python爬虫 senlenium爬取
- 下一篇: 云炬Android开发笔记 5-5,6