модифицированный метод get param - PullRequest
0 голосов
/ 27 ноября 2018
 public class RetrofitClient {

public static String BASE_URL = "https://android-full-time-task.firebaseio.com";
private static Retrofit retrofit;
public static Retrofit getRetrofit(){
    if(retrofit==null){
        retrofit = new Retrofit.Builder()
                .baseUrl(BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
    }
    return retrofit;
}

}

public interface ApiInterfaceService {
@GET("/data.json")
Call<List<FoodItem>> getFoodItems();}

public class RetrofitApi {

public static void getFoodItemsList(final ApiListener<List<FoodItem>> listener) {
    ApiInterfaceService service = RetrofitClient.getRetrofit().create(ApiInterfaceService.class);
    Call<List<FoodItem>> callFood = service.getFoodItems();
    callFood.enqueue(new Callback<List<FoodItem>>() {
        @Override
        public void onResponse(Call<List<FoodItem>> call, final Response<List<FoodItem>> response) {
            if(response!=null) {
                List<FoodItem> foodItems = response.body();
                listener.onSuccess(foodItems);
            }
        }

        @Override
        public void onFailure(Call<List<FoodItem>> call, Throwable t) {
            Log.d("Retrofit Failure",t.getLocalizedMessage());
        }
    });
}

}

Вот как использовать метод @GET?как отобразить этот URL https://android -full-time-task.firebaseio.com / data.json Я новичок в Retrofit, не знаю, как использовать метод get ..

1 Ответ

0 голосов
/ 28 ноября 2018

@GET - это HTTP-аннотация, в которой указывается тип запроса и относительный URL-адрес . Возвращаемое значение переносит ответ в объект Call с типом ожидаемого результата.

Коды в вашем вопросе - полный пример:

Call<List<FoodItem>> callFood = service.getFoodItems();
//this line calls getFoodItems method defined in interface via @GET annoation, 
//which will send a get request using baseUrl + relative url,

callFood.enqueue(new Callback<List<FoodItem>>() {
    @Override
    public void onResponse(Call<List<FoodItem>> call, final Response<List<FoodItem>> response) {
        if(response!=null) {//the response result will be wrapped in a Call object
            List<FoodItem> foodItems = response.body();
            listener.onSuccess(foodItems);
        }
    }

    @Override
    public void onFailure(Call<List<FoodItem>> call, Throwable t) {
        Log.d("Retrofit Failure",t.getLocalizedMessage());
    }
});
...