Retrofit:是Square公司開(kāi)發(fā)的一款針對(duì)Android網(wǎng)絡(luò)請(qǐng)求的框架羊苟,Retrofit2底層基于OkHttp實(shí)現(xiàn)的
1.使用前準(zhǔn)備
dependencies {
compile 'com.squareup.retrofit2:retrofit:2.1.0'
compile 'com.squareup.retrofit2:converter-gson:2.1.0'
compile 'com.squareup.retrofit2:converter-scalars:2.1.0'//ConverterFactory的String依賴(lài)包
}
當(dāng)然還有網(wǎng)絡(luò)權(quán)限尿招。
2.使用示例:
淘寶查詢(xún)IP地址的接口:/service/getIpInfo.PHP?ip=[ip地址字串]
返回的是(json格式的)國(guó)家 边翁、省(自治區(qū)或直轄市)、市(縣)、運(yùn)營(yíng)商。
{
“code”: 0,
”data”: {
“ip”: ”210.75.225.254”,
”country”: ”\u4e2d\u56fd”,
”area”: ”\u534e\u5317”,
“region”: ”\u5317\u4eac\u5e02”,
”city”: ”\u5317\u4eac\u5e02”,
”county”: ”“,
”isp”: ”\u7535\u4fe1”,
“country_id”: ”86”,
”area_id”: ”100000”,
”region_id”: ”110000”,
”city_id”: ”110000”,
“county_id”: ”-1”,
”isp_id”: ”100017”
}
編寫(xiě)實(shí)體類(lèi)垦巴,IpModel ?(包含響應(yīng)碼和IpData數(shù)據(jù)實(shí)體類(lèi))和IpData實(shí)體類(lèi),代碼就不放了铭段。
網(wǎng)絡(luò)請(qǐng)求接口:
public interface IpService{? ??
@GET("getIpInfo.php")? ??
Call getIpMsg(@Query("ip")String ip);
}
Retrofit提供的請(qǐng)求方式注解有@GET和@POST等骤宣,分別代表GET請(qǐng)求和POST請(qǐng)求,我們?cè)谶@里訪問(wèn)的界面是“getIpInfo.php”稠项。參數(shù)注解有@PATH和@Query等涯雅,@Query就是我們的請(qǐng)求的鍵值對(duì)的設(shè)置,在這里@Query(“ip”)代表鍵展运,“String ip”則代表值活逆。
創(chuàng)建Retrofit:
String url = "http://ip.taobao.com/service/";
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(url)
//增加返回值為String的支持
.addConverterFactory(ScalarsConverterFactory.create())
.addConverterFactory(GsonConverterFactory.create())
.build();
這里的baseUrl加上之前@GET(“getIpInfo.php”)定義的參數(shù)形成完整的請(qǐng)求地址;addConverterFactory用于指定返回的參數(shù)數(shù)據(jù)類(lèi)型拗胜,這里我們支持String和Gson類(lèi)型
用Retrofit創(chuàng)建接口文件:
IpService ipService = retrofit.create(IpService.class);?
Call call=ipService.getIpMsg(ip);
用retrofit創(chuàng)建我們之前定義的IpService接口對(duì)象蔗候,并調(diào)用該接口定義的getIpMsg方法得到Call對(duì)象。拿到Call對(duì)象接下來(lái)就可以像OkHttp那樣訪問(wèn)網(wǎng)絡(luò)了埂软。前面兩篇文章有講過(guò)锈遥。
我們主要探索下retrofit的請(qǐng)求參數(shù)本身。
請(qǐng)求方法
請(qǐng)求方法除了上文講到的@GET勘畔,還有@POST所灸、@PUT、@DELETE炫七、@HEAD爬立、@OPTIONS、@PATCH万哪、@HTTP侠驯。其中@HTTP用來(lái)替換以上7個(gè)抡秆,其他的分別對(duì)應(yīng)著不同的Http的請(qǐng)求方法。
@Query:?jiǎn)蝹€(gè)查詢(xún)參數(shù)
public interface IpService{? ?
?@GET("getIpInfo.php")? ??
Call getIpMsg(@Query("ip")String ip);
}
@QueryMap:多個(gè)查詢(xún)參數(shù)
public interface BlueService {
?@GET("book/search")? ??
Call getSearchBooks(@QueryMap Mapoptions);
}
@Path:動(dòng)態(tài)的替換訪問(wèn)路徑
public interface ApiStores {? ?
?@GET("adat/sk/{cityId}.html")? ?
?Call getWeather(@Path("cityId") String cityId);
}
接口傳過(guò)來(lái)的參數(shù)會(huì)替換請(qǐng)求接口中{cityId}的值吟策,做到動(dòng)態(tài)的改變請(qǐng)求接口儒士。
@Body與@POST:一起使用,提供查詢(xún)主體內(nèi)容檩坚,其中ApiInfo是一個(gè)bean類(lèi)
public interface ApiStores {? ? ? ??
@POST("client/shipper/getCarType")? ? ? ??
Call getCarType(@Body ApiInfo apiInfo);
}
@Headers:設(shè)置網(wǎng)絡(luò)請(qǐng)求頭
interface SomeService {?
@GET("some/endpoint")?
@Headers("Accept-Encoding: application/json")?
Call getCarType();
}
這種是設(shè)置固定的請(qǐng)求頭着撩,當(dāng)然我們也可以設(shè)置動(dòng)態(tài)的,如下:
interface SomeService {?
@GET("some/endpoint")?
Call someEndpoint(@Header("Location") String location);
}
@Multipart:用來(lái)上傳文件
public interface FileUploadService {? ? ??
@Multipart? ??
@POST("upload")? ??
Call upload(@Part("description") RequestBody description,@Part MultipartBody.Part file);
}
大體的用法介紹完畢效床。