OnResponse или метод onFailure Retrofit не вызывается - PullRequest
0 голосов
/ 25 марта 2020

Я пытался отладить это. Не уверен, что я делаю здесь не так.

Ниже приведено JSON, которое я пытаюсь проанализировать с помощью Retrofit и OkHttp, поэтому отлаживаем веб-ответ.

JSON

{
    "results": [
        {
            "id": 955591,
            "title": "Baby Kale Breakfast Salad with Quinoa & Strawberries",
            "image": "https://spoonacular.com/recipeImages/955591-312x231.jpg",
            "imageType": "jpg"
        },
        {
            "id": 557456,
            "title": "Oatmeal Berry Breakfast Cake ",
            "image": "https://spoonacular.com/recipeImages/557456-312x231.jpg",
            "imageType": "jpg"
        },
        {
            "id": 500461,
            "title": "Creamy Chocolate Breakfast Shake",
            "image": "https://spoonacular.com/recipeImages/500461-312x231.jpg",
            "imageType": "jpg"
        },
        {
            "id": 607078,
            "title": "Strawberry Infused Quinoa Breakfast Bowl",
            "image": "https://spoonacular.com/recipeImages/607078-312x231.jpg",
            "imageType": "jpg"
        },
        {
            "id": 474360,
            "title": "Easy Breakfast Bruschetta with Tomato and Avocado – Nigella Lawson – 50 Women Game Changers In Food",
            "image": "https://spoonacular.com/recipeImages/474360-312x231.jpg",
            "imageType": "jpg"
        },
        {
            "id": 505103,
            "title": "Strawberry & Peach Quinoa Breakfast",
            "image": "https://spoonacular.com/recipeImages/505103-312x231.jpg",
            "imageType": "jpg"
        },
        {
            "id": 495020,
            "title": "Pumpkin Spice Breakfast Shake",
            "image": "https://spoonacular.com/recipeImages/495020-312x231.jpg",
            "imageType": "jpg"
        },
        {
            "id": 916852,
            "title": "Strawberry Infused Quinoa Breakfast Bowl",
            "image": "https://spoonacular.com/recipeImages/916852-312x231.jpg",
            "imageType": "jpg"
        },
        {
            "id": 694990,
            "title": "Quick Breakfast Taco",
            "image": "https://spoonacular.com/recipeImages/694990-312x231.jpg",
            "imageType": "jpg"
        },
        {
            "id": 850397,
            "title": "Plant Protein Power Breakfast Bowls",
            "image": "https://spoonacular.com/recipeImages/850397-312x231.jpg",
            "imageType": "jpg"
        }
    ],
    "offset": 0,
    "number": 10,
    "totalResults": 4874
}

Позвольте мне также опубликовать классы моделей, которые я создал для JSON выше. Для этого есть два класса: JSON

1) Root Java class

public class Root {
    @SerializedName("results")
    private List<Results> results;

    @SerializedName("offset")
    private int offset;

    @SerializedName("number")
    private int number;


    @SerializedName("totalResults")
    private int totalResults;

    public void setResults(List<Results> results){
        this.results = results;
    }
    public List<Results> getResults(){
        return this.results;
    }
    public void setOffset(int offset){
        this.offset = offset;
    }
    public int getOffset(){
        return this.offset;
    }
    public void setNumber(int number){
        this.number = number;
    }
    public int getNumber(){
        return this.number;
    }
    public void setTotalResults(int totalResults){
        this.totalResults = totalResults;
    }
    public int getTotalResults(){
        return this.totalResults;
    }
}

И результат Java class

public class Results {

    @SerializedName("id")
    private int id;

    private String title;

    private String image;

    private String imageType;

    public void setId(int id){
        this.id = id;
    }
    public int getId(){
        return this.id;
    }
    public void setTitle(String title){
        this.title = title;
    }
    public String getTitle(){
        return this.title;
    }
    public void setImage(String image){
        this.image = image;
    }
    public String getImage(){
        return this.image;
    }
    public void setImageType(String imageType){
        this.imageType = imageType;
    }
    public String getImageType(){
        return this.imageType;
    }
}

Я настроил свой модифицированный клиент и интерфейс для метода get. Ниже приведен журнал из журналов OkHttp:

2020-03-25 14:49:58.787 7230-7640/com.gahlot.foodbeingmade D/OkHttp: --> GET https://api.spoonacular.com/recipes/complexSearch?apiKey=4f5842b38a684f47b841dd3518073f75&type=Breakfast
2020-03-25 14:49:58.788 7230-7640/com.gahlot.foodbeingmade D/OkHttp: --> END GET
2020-03-25 14:49:59.844 7230-7640/com.gahlot.foodbeingmade D/OkHttp: <-- 200 https://api.spoonacular.com/recipes/complexSearch?apiKey=4f5842b38a684f47b841dd3518073f75&type=Breakfast (1056ms)
2020-03-25 14:49:59.844 7230-7640/com.gahlot.foodbeingmade D/OkHttp: date: Wed, 25 Mar 2020 09:20:00 GMT
2020-03-25 14:49:59.844 7230-7640/com.gahlot.foodbeingmade D/OkHttp: content-type: application/json
2020-03-25 14:49:59.844 7230-7640/com.gahlot.foodbeingmade D/OkHttp: set-cookie: __cfduid=d6a7d5539bf764a2fd40ed2fcb691c7d51585127999; expires=Fri, 24-Apr-20 09:19:59 GMT; path=/; domain=.spoonacular.com; HttpOnly; SameSite=Lax
2020-03-25 14:49:59.844 7230-7640/com.gahlot.foodbeingmade D/OkHttp: allow-control-allow-origin: *
2020-03-25 14:49:59.845 7230-7640/com.gahlot.foodbeingmade D/OkHttp: access-control-allow-origin: *
2020-03-25 14:49:59.845 7230-7640/com.gahlot.foodbeingmade D/OkHttp: access-control-allow-headers: Authorization, UserId, Hash, Name, Password, Accept, Accept-Language, Content-Language, Content-Type, Cache-Control, Origin, X-Requested-With
2020-03-25 14:49:59.845 7230-7640/com.gahlot.foodbeingmade D/OkHttp: access-control-allow-methods: GET, HEAD, POST, OPTIONS, DELETE, PUT
2020-03-25 14:49:59.845 7230-7640/com.gahlot.foodbeingmade D/OkHttp: allow-control-allow-methods: GET, HEAD, POST, OPTIONS, DELETE, PUT
2020-03-25 14:49:59.845 7230-7640/com.gahlot.foodbeingmade D/OkHttp: x-api-quota-request: 1.1
2020-03-25 14:49:59.845 7230-7640/com.gahlot.foodbeingmade D/OkHttp: x-api-quota-used: 9.899999999999999
2020-03-25 14:49:59.845 7230-7640/com.gahlot.foodbeingmade D/OkHttp: cf-cache-status: DYNAMIC
2020-03-25 14:49:59.845 7230-7640/com.gahlot.foodbeingmade D/OkHttp: expect-ct: max-age=604800, report-uri="https://report-uri.cloudflare.com/cdn-cgi/beacon/expect-ct"
2020-03-25 14:49:59.845 7230-7640/com.gahlot.foodbeingmade D/OkHttp: server: cloudflare
2020-03-25 14:49:59.845 7230-7640/com.gahlot.foodbeingmade D/OkHttp: cf-ray: 57978dad5adfdd1e-SIN
2020-03-25 14:49:59.848 7230-7640/com.gahlot.foodbeingmade D/OkHttp: {"results":[{"id":955591,"title":"Baby Kale Breakfast Salad with Quinoa & Strawberries","image":"https://spoonacular.com/recipeImages/955591-312x231.jpg","imageType":"jpg"},{"id":557456,"title":"Oatmeal Berry Breakfast Cake ","image":"https://spoonacular.com/recipeImages/557456-312x231.jpg","imageType":"jpg"},{"id":500461,"title":"Creamy Chocolate Breakfast Shake","image":"https://spoonacular.com/recipeImages/500461-312x231.jpg","imageType":"jpg"},{"id":607078,"title":"Strawberry Infused Quinoa Breakfast Bowl","image":"https://spoonacular.com/recipeImages/607078-312x231.jpg","imageType":"jpg"},{"id":474360,"title":"Easy Breakfast Bruschetta with Tomato and Avocado – Nigella Lawson – 50 Women Game Changers In Food","image":"https://spoonacular.com/recipeImages/474360-312x231.jpg","imageType":"jpg"},{"id":505103,"title":"Strawberry & Peach Quinoa Breakfast","image":"https://spoonacular.com/recipeImages/505103-312x231.jpg","imageType":"jpg"},{"id":495020,"title":"Pumpkin Spice Breakfast Shake","image":"https://spoonacular.com/recipeImages/495020-312x231.jpg","imageType":"jpg"},{"id":916852,"title":"Strawberry Infused Quinoa Breakfast Bowl","image":"https://spoonacular.com/recipeImages/916852-312x231.jpg","imageType":"jpg"},{"id":694990,"title":"Quick Breakfast Taco","image":"https://spoonacular.com/recipeImages/694990-312x231.jpg","imageType":"jpg"},{"id":850397,"title":"Plant Protein Power Breakfast Bowls","image":"https://spoonacular.com/recipeImages/850397-312x231.jpg","imageType":"jpg"}],"offset":0,"number":10,"totalResults":4875}
2020-03-25 14:49:59.848 7230-7640/com.gahlot.foodbeingmade D/OkHttp: <-- END HTTP (1561-byte body)

После этого нет журнала ни из методов модернизации onResonse и OnFailure. Вот вызовы Retrofit, которые я делаю с помощью метода enqueue

public LiveData<List<Results>> getData() {
        final MutableLiveData<List<Results>> data = new MutableLiveData<>();
        Call<Root> call = foodBeingMadeApi.getRoot();
        call.enqueue(new Callback<Root> () {
            @Override
            public void onResponse(Call<Root>  call, Response<Root>  response) {
                    Log.i(TAG, "onResponse: " + response.code());
                    data.setValue(response.body().getResults());
            }

            @Override
            public void onFailure(Call<Root> call, Throwable t) {
                t.printStackTrace();
            }
        });
            try {
                Thread.sleep(5000);
                Log.e(TAG, "getRoot: Thread is sleeping");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        return data;
    }
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...