taycaldwell / riot-api-java

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

GetGameDuration in Android Studio #143

Closed rafalm1 closed 4 years ago

rafalm1 commented 5 years ago

Im trying to get game duration by using getGameDuration method from Match class by i don't know what im doing wrong :( So this is my code:

public class MatchListActivity extends MainActivity {

private EditText inputText;
private TextView numOfGamesText;

ListView listView;

ArrayList<String> arrayList = new ArrayList<String>();

int matchLimitCounter;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_match_list);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);

    matchLimitCounter = 0;

    inputText = findViewById(R.id.editText);
    numOfGamesText = findViewById(R.id.txtNumOfGames);
    Button searchButton = findViewById(R.id.button);

    //listView with adapter
    listView = findViewById(R.id.listview);
    ArrayAdapter adapter = new ArrayAdapter(MatchListActivity.this, android.R.layout.simple_list_item_1, arrayList);
    listView.setAdapter(adapter);

    searchButton.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            FetchMatchListTask summonerTask = new FetchMatchListTask();
            String inputName = inputText.getText().toString();
            summonerTask.execute(inputName);
        }
    });
}

public class Wrapper
{
    public Summoner summoner;
    public MatchList matchList;
    public Match myMatch;
    RiotApi api;

}

public class FetchMatchListTask extends AsyncTask<String, Void, Wrapper> {

    @Override
    protected Wrapper doInBackground(String... params) {

        ApiConfig config = new ApiConfig().setKey("apiKey");
        //RiotApi api = new RiotApi(config);

        Wrapper w = new Wrapper();

        w.api = new RiotApi(config);

        try {
            //Summoner summoner = api.getSummonerByName(Platform.EUNE, params[0]);
            w.summoner = w.api.getSummonerByName(Platform.EUNE, params[0]);
            //MatchList matchList = api.getMatchListByAccountId(Platform.EUNE, summoner.getAccountId());
            w.matchList = w.api.getMatchListByAccountId(Platform.EUNE, w.summoner.getAccountId());
            return w;
        } catch (RiotApiException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    protected void onPostExecute(Wrapper w) {
        super.onPostExecute(w);
        if(w != null) {
            numOfGamesText.setText(String.valueOf(w.matchList.getTotalGames()));
        }

        if (w.matchList.getMatches() != null) {
            for (MatchReference match : w.matchList.getMatches()) {
                if(matchLimitCounter == 5) break;
                else{
                    matchLimitCounter++;
                    arrayList.add("Season: " + match.getSeason() + ", Lane: "+ match.getLane() +
                     ", Champion: " + match.getChampion() + ", Queue: " + match.getQueue() + " Summ lev: "+w.summoner.getSummonerLevel());

                    long gameID = match.getGameId();

                    try {
                        w.myMatch = w.api.getMatch(Platform.EUNE, gameID );
                        arrayList.add(" Time: "+ w.myMatch.getGameDuration());
                    } catch (RiotApiException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }
}

}

VinciGP commented 5 years ago

This API works in Android?

Jorege98 commented 5 years ago

I have this question too, I'm trying to use it on Android Studio, but it isn't working fine, I'm not sure if I need something more to make it work

rafalm1 commented 4 years ago

Everything is working.