首先得到的API,並獲得通過提前REST客戶端的JSON輸出,是一款Chrome擴展。現在把這個輸出到JSON to POJO converter ,你會得到你的POJO類。將它們粘貼到您的項目中。做一個interface`
/**
* Get Data
*
* @param body Holds the JSON payloads
* @return Formatted data
*/
@POST("JobSpotAPI/getUserInterviewSchedule")
Call<POJOClass> getData(@Body JsonObject body);
和安裝客戶端
public class RetrofitClient {
private static Retrofit retrofit = null;
public static Retrofit getClient(String baseUrl) {
if (retrofit==null) {
retrofit = new Retrofit.Builder()
.baseUrl(baseUrl)
.addCallAdapterFactory(RxJavaCallAdapterFactory.create())
.addConverterFactory(GsonConverterFactory.create())
.build();
}
return retrofit;
}
創建回收站視圖設置它的適配器和所有。只需添加一些新的方法,如 -
public void updateAnswers(List<Item> items) {
mItems = items;
notifyDataSetChanged();
}
private Item getItem(int adapterPosition) {
return mItems.get(adapterPosition);
}
Setup Utility類來調用接口。
public class ApiUtils {
public static final String BASE_URL = "https://base_url/";
public static Interface_name methodName() {
return RetrofitClient.getClient(BASE_URL).create(Interface_name.class);
}}
聲明接口在活動
Interface_name obj = ApiUtils.methodName();
如果你有一些有效載荷然後附上他們給你提出要求。在此之前,你需要一個JSON字符串傳遞給API請求。製作JSON有效內容。
private JsonObject makeJsonObjectPayload() {
JsonObject requestBean = new JsonObject();
requestBean.addProperty("key", value);
requestBean.addProperty("key", value);
requestBean.addProperty("key", value);
requestBean.addProperty("key", value);
return requestBean;
根據JSON響應http://www.jsonschema2pojo.org/傳遞API請求
使用本站生成JSON的類 –