taycaldwell / riot-api-java

Riot Games API Java Library
http://taycaldwell.github.io/riot-api-java/
Apache License 2.0
192 stars 73 forks source link

Encounter a ClassCastException when call method getLeagueBySummonerId #100

Closed sugls closed 7 years ago

sugls commented 7 years ago

hello,I got a error when run the following java code

        ApiConfig config = new ApiConfig();
        config.setKey(key);
        RiotApi api = new RiotApi(config);
        try {
            Map<String, List<LeagueList>> map = api.getLeagueBySummonerId(Platform.KR, 27971322);

            for (Map.Entry<String, List<LeagueList>> entry : map.entrySet()) {
                List<LeagueList> list = entry.getValue();

                System.out.println("key--> " + entry.getKey());
                System.out.println("value--> ");
                for (LeagueList leagueList : list) {
                    System.out.println(leagueList);
                }
            }
        } catch (RiotApiException e) {
            e.printStackTrace();
        }

the console output as following show

Exception in thread "main" java.lang.ClassCastException: java.util.ArrayList cannot be cast to java.util.Map
    at net.rithms.riot.api.RiotApi.getLeagueBySummonerId(RiotApi.java:1183)
    at test.t5(test.java:216)
    at test.main(test.java:238)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147)

In RiotApi.java source file , i found the method

public Map<String, List<LeagueList>> getLeagueBySummonerId(Platform platform, long summonerId) throws RiotApiException {
        Objects.requireNonNull(platform);
        ApiMethod method = new GetLeagueBySummonerId(getConfig(), platform, summonerId);
        return endpointManager.callMethodAndReturnDto(method);
    }

and the ApiMethod GetLeagueBySummonerId set the ReturnType is Gson's TypeToken<List<LeagueList>>, not the method return type Map<String, List<LeagueList>>

public GetLeagueBySummonerId(ApiConfig config, Platform platform, long summonerId) {
        super(config);
        setPlatform(platform);
        setReturnType(new TypeToken<List<LeagueList>>() {
        }.getType());
        setUrlBase(platform.getHost() + "/lol/league/v3/leagues/by-summoner/" + summonerId);
        addApiKeyParameter();
    }

btw , in riot api document the api returned a Set image

so could you fix it.

Linnun commented 7 years ago

This bug was fixed a few days ago in c47fc0a786aaaf31a2e2d3cd9d4b60b672b42af2

Using the current master branch and slightly modifying your code, this runs perfectly fine for me:

        ApiConfig config = new ApiConfig();
        config.setKey(key);
        RiotApi api = new RiotApi(config);
        try {
            List<LeagueList> list = api.getLeagueBySummonerId(Platform.KR, 27971322);

            for (LeagueList leagueList : list) {
                System.out.println(leagueList);
            }
        } catch (RiotApiException e) {
            e.printStackTrace();
        }

We might change List to Set in a future update to fully comply with the Riot Api -- but for now this works just fine :)

sugls commented 7 years ago

thanks for your answer,it works well now