Double requête ?

Fermé
P53ud0 Messages postés 24 Date d'inscription dimanche 30 octobre 2016 Statut Membre Dernière intervention 14 avril 2018 - Modifié par jordane45 le 6/02/2017 à 15:37
jordane45 Messages postés 38181 Date d'inscription mercredi 22 octobre 2003 Statut Modérateur Dernière intervention 20 mai 2024 - 6 févr. 2017 à 15:38
Bonjour, je travaille sur une application pour une utilisation personnel/amis.

Je voudrais savoir si il était possible de faire requête dans une requête (API)
Je m'explique, j'ai un " public void checkGame " et avec les résultât JSON que je récupère dans une boucle for, je voulais savoir si je pouvais directement dans la boucle relancer une requête qui utilise ses données dans un "public void checkIdChampion" ?

Voici le code:
(ps: Je ne sais pas comment faire pour mettre en évidence le code... si quelque"un peu m'expliquer)

public void checkGame(final String plrID, final String region, final CheckGameCallBack clllBack) {


String regionv2;
        switch (region) {
            case "NA": regionv2 = "NA1";
                    break;
            case "EUW": regionv2 = "EUW1";
                    break;
            case "BR": regionv2 = "BR1";
                    break;
            case "EUNE": regionv2 = "EUN1";
                    break;
            default: regionv2 = "ERROR";
        }

String url = "https://"+ region +".api.pvp.net/observer-mode/rest/consumer/getSpectatorGameInfo/"+ regionv2 +"/"+ plrID +"?api_key=" + API_KEY;

final JsonObjectRequest requete = new JsonObjectRequest(Request.Method.GET, url, new Response.Listener<JSONObject>() {
                    @Override
                    public void onResponse(JSONObject response) {

Log.d("APP", response.toString());
                        try {

List<Gamer> listGamers = new ArrayList<>();

String gameMode = response.getString("gameQueueConfigId");


JSONArray nameList = response.getJSONArray("participants");

for (int i = 0; i < nameList.length(); i++) {
                                JSONObject jsonobject = nameList.getJSONObject(i);


final Gamer gamer = new Gamer(jsonobject.getString("summonerName"), jsonobject.getLong("teamId"), jsonobject.getLong("championId"));
                                listGamers.add(gamer);
(C'EST ICI QUE JE VOUDRAIS FAIRE APPEL A MON CHECKIDCHAMPION PUIS CE QU IL Y A DEJA UNE BOUCLE FOR QUI RECUPERE LES DONNES UN PAR UN)

}
                            clllBack.onSucces(gameMode.toString(), listGamers);

} catch (JSONException e) {
                            Log.d("APP", "EXCEPTION = " + e);
                            e.printStackTrace();
                        }
                    }



}, new Response.ErrorListener() {
                        @Override
                        public void onErrorResponse(VolleyError error) {

if(error instanceof NetworkError) {
                                clllBack.onError("Impossible to connect");
                            }
                            else if(error instanceof ServerError){
                                clllBack.onError("Game not found");
                            }
                            Log.d("APP", "ERROR = " + error);

}
                });
                            queue.add(requete);
    }





VOICI LE DEUXIEME CODE QUI EST SUR LA MEME CLASS JAVA ( ApiRequest.java )

public void checkIdChampion(final String champId, final String region, final CheckIdChampionCallBack callBack) {

String url = "https://global.api.pvp.net/api/lol/static-data/" + region + "/v1.2/champion/" + champId + "?champData=image&api_key=" + API_KEY;

JsonObjectRequest requete = new JsonObjectRequest(Request.Method.GET, url , new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject response) {
                try {

String gameMode = response.getString("image");


callBack.onSucces("SALUT");

} catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

if(error instanceof NetworkError) {
                    callBack.onError("Impossible to connect");
                }
                else if(error instanceof ServerError){
                    callBack.onError("Game not found");
                }
                Log.d("APP", "ERROR = " + error);

}
        });
        queue.add(requete);
    }


EDIT : Ajout des balises de code

A voir également:

1 réponse

jordane45 Messages postés 38181 Date d'inscription mercredi 22 octobre 2003 Statut Modérateur Dernière intervention 20 mai 2024 4 670
6 févr. 2017 à 15:38
Bonjour,

1 - Pour les balises de code : https://codes-sources.commentcamarche.net/faq/10686-le-nouveau-codes-sources-comment-ca-marche#balises-code


2 - Les données sont issues de la même BDD ?
Si oui.. il y a surement moyen de ne pas avoir à boucler sur le résultat de ta première requête .... mais directement faire une SEULE requête (avec JOINTURE) qui retourne TOUTES les infos que tu veux ( en une seule fois...)
Pour t'aider.. il nous faudrait la structure de tes tables.

0