如何对此响应使用改造调用?

问题描述 投票:0回答:1

具有Get调用的API的响应,但是如何从翻新的Api调用中获得这些值,在该过程中,调用在OnFailure函数中进行,并且无法在Response Viz getresponse()函数之后给出的代码段中得到喘息。此函数提供了一个异常,并且不返回Global,Country和主要模型类的值**

The response of the API showing the object with key and having the values, how should one use Values of the key as it is not achievable by my java code snippet

                     {
                      "Global": {
                        "New": 118196,
                        "Total": 5898252,
                        "NewD": 4785,
                        "TotalD": 367273,
                        "NewR": 65964,
                        "TotalR": 2415401
                      },
                      "Countries": [
                        {
                          "Country": "Afghanistan",
                          "CountryCode": "AF",
                          "Slug": "afghanistan",
                          "Date": "2020-05-29T08:39:18Z"
                        },
                        {
                          "Country": "Belarus",
                          "CountryCode": "BY",
                          "Slug": "belarus",
                          "Date": "2020-05-29T08:39:18Z"
                        },
                        {
                          "Country": "Brazil",
                          "CountryCode": "BR",
                          "Slug": "brazil",
                          "Date": "2020-05-29T08:39:18Z"
                        }
                        ],
                      "Date": "2020-05-29T08:39:18Z"
                    }


                After making 3 models for this response namely Main, Global, Countries, how to get response values from Global i.e-("New": 118196, "Total": 5898252, "NewD": 4785,"TotalD": 367273,"NewR": 65964,"TotalR": 2415401), Countries({"Country": "Afghanistan", "CountryCode": "AF", "Slug":"afghanistan","Date": "2020-05-29T08:39:18Z"
                        }), and Main class(Countries,GLobal and Date).


                    Here is the java function which is-

                    private void getresponse() {
                            Gson gson = new GsonBuilder()
                                    .setLenient()
                                    .create();
                            Retrofit retrofit = new Retrofit.Builder()
                                    .baseUrl(endpointApi.BASE_URL)
                                    .addConverterFactory(GsonConverterFactory.create(gson))                                    
                                    .build();
                            endpointApi api = retrofit.create(endpointApi.class);
                            Call<List<Main>> call = api.getResult();
                            call.enqueue(new Callback<List<Main>>() {
                                @Override
                                public void onResponse(Call<List<Main>> call, Response<List<Main>> response) {
                                    List<Main> heroList = response.body();
                                    for (int i = 0; i < heroList.size(); i++) {
                                        Integer heroes = heroList.get(i).getCountries().size();
                                        Log.i("test111","test111--"+heroes);
                                    }
                                }
                                @Override
                                public void onFailure(Call<List<Example>> call, Throwable t) {
                                    Toast.makeText(getApplicationContext(), t.getMessage(), Toast.LENGTH_SHORT).show();
                                    t.printStackTrace();
                                }
                            });
                        }

上面的Java代码是用于从API获取响应的改进,如何在此代码段中从Global对象中获取TotalD,从Countrys对象中获取Slung。上面的代码未显示任何响应,并转到onFailure()函数,并且无法通过API获得响应,如[]

        Here is the Interface code snippet-
        public interface endpointApi{
            String BASE_URL = "https://api.something.com/";
            @GET("summary")
            Call<List<Main>> getResult();
        }

            **How to achieve all values in Global i.e, Countries, and Main model class values?** 

[具有Get调用的API的响应,但是如何从翻新的Api调用获得这些值,在该过程中,调用在OnFailure函数中进行,而在...之后给出的代码段中无法重生]]

android json gson retrofit retrofit2
1个回答
0
投票

如果您只想从国家/地区购买totalD' from Globalsandslug`,请使用此选项>>

public class MyResponse {
    private Global Global;
    private List<Country> Countries;

    //your getter methods here... 
} 

public class Global {
    private String TotalD;
    //your getter here
} 

public class Country {
    private String Slug
    //your getter
} 

然后在您的Call中按如下方式使用它:

© www.soinside.com 2019 - 2024. All rights reserved.