Retrofit簡單使用

2021-08-16 20:17:55 字數 906 閱讀 8920

//新增retrofit依賴

compile 'com.squareup.retrofit2:retrofit:2.3.0'

//新增gson轉換器的依賴

compile 'com.squareup.retrofit2:converter-gson:2.3.0'

配置服務介面

public inte***ce apiservice /repos") //請求路徑上的佔位符

callgetblog(@path("user") string user);

// 訪問的api是:

// 在發起請求時, 會被替換為方法的第乙個引數 user(被@path註解作用)

}

categarybean 為返回資料的實體類

// unable to create converter轉換器 for class com.dash.a05_retrofit.categarybean

//不能夠為categarybean建立乙個轉換器---response.body().string()---json字串--gson->categarybean

//json xml jackson --->對應著不同的轉換器

public void

getcategory(view view)

}@override

public void

onfailure(callcall, throwable t)

});} public void

getcategory_02(view view)

@override

public void

onfailure(callcall, throwable t)

});}}

retrofit的簡單使用

有引數的寫入引數,如 get user login calllogin query mobile string mobile,query password string password 沒有引數的不寫引數 get product getcatagory 建立方法 callgouwu 二 建立類寫入...

retrofit的簡單使用 請求網路資料

倒入依賴 implementation com.squareup.retrofit2 converter gson 2.0.0 beta4 寫乙個網路資料的bean類 寫乙個所有介面都一樣的介面頭 方便呼叫 public class constant寫乙個介面 繼續拼接往介面頭後面拼接的介面 pub...

retrofit簡單的網路請求

目前retrofit速度最快,接下來是okhttp,最後是volley。定義乙個介面 public inte ce service 現在是模擬get請求,get後面帶的引數是網頁的子目錄,比如,www.baidu.com api cook list,那get後面就帶 api cook list 而b...