Retrofit的簡單使用(入門篇)-GET請求
阿新 • • 發佈:2019-01-01
關於android的網路請求,使用過不少 有HttpURLConnection,volley,xUtil3,OkHttp等,而大多數數時候,從網路請求拿到資料之後,一般是json資料,都還需要解析,個人用的最多的還是Gson (結合 GsonFormat使用,簡直好用極了!) 最近又出現了一個吊爆的東東-Retrofit,什麼鬼(反正官方文件看不太懂!) 經過檢視網路資料,大致有所瞭解Retrofit與okhttp共同出自於Square公司,retrofit就是對okhttp做了一層封裝。把網路請求都交給給了Okhttp,我們只需要通過簡單的配置就能使用retrofit來進行網路請求了。
言歸正傳,下面來介紹retrofit的簡單使用 以get請求為例,開始了!(AS)
使用之前當然需要匯入依賴庫
compile 'com.squareup.okhttp3:okhttp:3.2.0'
compile 'com.squareup.retrofit2:retrofit:2.0.0-beta4'
compile 'com.squareup.retrofit2:converter-gson:2.0.0-beta3'
其中
http://112.124.22.238:8081 是網站地址
/course_api/wares/hot 是資料獲取介面(查詢資料是通過訪問servlet連結獲取)
?pageSize=28&curPage=1 通過? & 連線查詢條件 (也就是鍵值對)
經典的GET請求
返回的json資料格式
{
"currentPage": 1,
"list": [
{
"id": 1,
"imgUrl": "http://7mno4h.com2.z0.glb.qiniucdn.com/s_recommend_55c1e8f7N4b99de71.jpg",
"name": "聯想(Lenovo)拯救者14.0英寸遊戲本(i7-4720HQ 4G 1T硬碟 GTX960M 2G獨顯 FHD IPS屏 背光鍵盤)黑",
"price": 5979.0 ,
"sale": 8654
}
],
"pageSize": 1,
"totalCount": 28,
"totalPage": 28
}
需要先建立一個Bean類,可以手寫,也可以使用GsonFormat生成,此類就是json轉換成的java類,叫ShopBean吧
不懂json轉java類的童鞋,請自行百度
public class ShopBean{
/**
* totalCount : 28
* currentPage : 1
* totalPage : 28
* pageSize : 1
* list : [{"id":1,"name":"聯想(Lenovo)拯救者14.0英寸遊戲本(i7-4720HQ 4G 1T硬碟 GTX960M 2G獨顯 FHD IPS屏 背光鍵盤)黑","imgUrl":"http://7mno4h.com2.z0.glb.qiniucdn.com/s_recommend_55c1e8f7N4b99de71.jpg","description":null,"price":5979,"sale":8654}]
*/
private int totalCount;
private int currentPage;
private int totalPage;
private int pageSize;
/**
* id : 1
* name : 聯想(Lenovo)拯救者14.0英寸遊戲本(i7-4720HQ 4G 1T硬碟 GTX960M 2G獨顯 FHD IPS屏 背光鍵盤)黑
* imgUrl : http://7mno4h.com2.z0.glb.qiniucdn.com/s_recommend_55c1e8f7N4b99de71.jpg
* description : null
* price : 5979.0
* sale : 8654
*/
private List<ListBean> list;
public int getTotalCount() {
return totalCount;
}
public void setTotalCount(int totalCount) {
this.totalCount = totalCount;
}
public int getCurrentPage() {
return currentPage;
}
public void setCurrentPage(int currentPage) {
this.currentPage = currentPage;
}
public int getTotalPage() {
return totalPage;
}
public void setTotalPage(int totalPage) {
this.totalPage = totalPage;
}
public int getPageSize() {
return pageSize;
}
public void setPageSize(int pageSize) {
this.pageSize = pageSize;
}
public List<ListBean> getList() {
return list;
}
public void setList(List<ListBean> list) {
this.list = list;
}
public static class ListBean {
private int id;
private String name;
private String imgUrl;
private Object description;
private double price;
private int sale;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getImgUrl() {
return imgUrl;
}
public void setImgUrl(String imgUrl) {
this.imgUrl = imgUrl;
}
public Object getDescription() {
return description;
}
public void setDescription(Object description) {
this.description = description;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public int getSale() {
return sale;
}
public void setSale(int sale) {
this.sale = sale;
}
}
}
上面的程式碼好像蠻多的,其實就是一個json轉java類,大可不必細看,略過即可!
下面開始,簡述retrofit的使用嘍,此使用沒有新增任何複雜邏輯,只是把官方使用加了一個小例子,方便理解。至於封裝,大家看懂之後,自行新增!
首先需要定義一個介面
public interface ShopService {
@GET("/course_api/wares/hot")
Call<ShopBean> getShop(@Query("pageSize") int pageSize,
@Query("curPage") int curPage
}
先不接受這個介面,往下看。。。
在Actitivity中,或者其他你需要使用的地方,當然這裡只是個例子,其實retrofit 應該單例會比較好
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("http://112.124.22.238:8081/")
.addConverterFactory(GsonConverterFactory.create())
.build();
ShopService service = retrofit.create(ShopService.class);
//pageSize = 10,curPage=1
Call<ShopBean> callShops = service.getShop(10,1);
callShops.enqueue(new Callback<ShopBean>() {
@Override
public void onResponse(Call<ShopBean> call, Response<ShopBean> response) {
if (response.isSuccess()) {
ShopBean result = response.body();
}
}
@Override
public void onFailure(Call<ShopBean> call, Throwable t) {
}
});