Reputation: 11
Good morning,
I am currently working on a project in android studio in Java on the PokeApi Api. Unfortunately, I can't get a response from the server even though the link is accessible from a browser. If you have an idea, I'm interested. Thanks.
My class :
public class PokemonDetailActivity extends AppCompatActivity {
private int idPokemon;
private String imagePokemon;
private String urlPokemonDetails1 = "https://pokeapi.co/api/v2/pokemon-species/";
private String urlPokemonDetails2 = "https://pokeapi.co/api/v2/pokemon/";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_pokemon_detail);
idPokemon = getIntent().getIntExtra("POKEMON_SELECTION_ID", -1);
imagePokemon = getIntent().getStringExtra("IMAGE_POKEMON");
urlPokemonDetails1 += idPokemon + "/";
urlPokemonDetails2 += idPokemon + "/";
RequestQueue queue = Volley.newRequestQueue(getApplicationContext());
TextView nomPokemon = findViewById(R.id.nom_pokmeon);
//on associe la postion du pokemon au textView
TextView positionPokemon = findViewById(R.id.posture_pokemon);
// on associe la taille du pokemon au textView
TextView taillePokemon = findViewById(R.id.taille_pokemon);
// on associe l'habitat du pokemon au textView
TextView habitatPokemon = findViewById(R.id.habitat_pokemon);
// on associe les stats du pokemon aux textView
TextView hpPokemon = findViewById(R.id.hp);
TextView attaquePokemon = findViewById(R.id.attaque);
TextView defensePokemon = findViewById(R.id.defense);
TextView attaqueSpePokemon = findViewById(R.id.attaque_speciale);
TextView defenseSpePokemon = findViewById(R.id.defense_speciale);
TextView vitessePokemon = findViewById(R.id.vitesse);
// on associe l'image du pokemon à l'imageView
Picasso.get().load(imagePokemon).into((ImageView) findViewById(R.id.imageview_pokemon));
final String[] response = {""};
try {
StringRequest request = new StringRequest(Request.Method.GET, urlPokemonDetails1,
new Response.Listener<String>() {
@Override
public void onResponse(String rep) {
response[0] += rep;
JSONObject jsonObj = null;
try {
jsonObj = new JSONObject(response[0]);
//get the name of the pokemon
String namePokemon = jsonObj.getString("name");
// get the living area of the pokemon
JSONObject habitat = jsonObj.getJSONObject("habitat");
String habitatPok = habitat.getString("name");
// get the shape of the pokemon
JSONObject shape = jsonObj.getJSONObject("shape");
String shapePokemon = shape.getString("name");
nomPokemon.setText(namePokemon);
habitatPokemon.setText(habitatPok);
positionPokemon.setText(shapePokemon);
} catch (JSONException e) {
throw new RuntimeException(e);
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.d("Erreur de téléchargement : ", error.getMessage());
}
});
queue.add(request);
} catch (Exception e) {
e.printStackTrace();
}
try {
StringRequest request = new StringRequest(Request.Method.GET, urlPokemonDetails2,
new Response.Listener<String>() {
@Override
public void onResponse(String rep) {
response[1] += rep;
JSONObject jsonObj = null;
try {
jsonObj = new JSONObject(response[1]);
// get the height of the pokemon
int heightPokemon = jsonObj.getInt("height");
// get the stats of the pokemon
JSONArray stats = jsonObj.getJSONArray("stats");
JSONObject stat = stats.getJSONObject(0);
JSONObject stat1 = stats.getJSONObject(1);
JSONObject stat2 = stats.getJSONObject(2);
JSONObject stat3 = stats.getJSONObject(3);
JSONObject stat4 = stats.getJSONObject(4);
JSONObject stat5 = stats.getJSONObject(5);
int hp = stat.getInt("base_stat");
int attack = stat1.getInt("base_stat");
int defense = stat2.getInt("base_stat");
int specialAttack = stat3.getInt("base_stat");
int specialDefense = stat4.getInt("base_stat");
int speed = stat5.getInt("base_stat");
taillePokemon.setText(heightPokemon);
hpPokemon.setText(hp);
attaquePokemon.setText(attack);
defensePokemon.setText(defense);
attaqueSpePokemon.setText(specialAttack);
defenseSpePokemon.setText(specialDefense);
vitessePokemon.setText(speed);
} catch (JSONException e) {
throw new RuntimeException(e);
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.d("Erreur de téléchargement : ", error.getMessage());
}
});
queue.add(request);
} catch (Exception e) {
e.printStackTrace();
}
}
}
Do you think it may be caused by the response too long returned by the server maybe ?
I tried to implement library Volley to get the "response" by the server but that didn't solved the problem and to make one class for this.
Update : Why my String tab 'result' is empty (and above all why the server doesn't return me anything)..
Upvotes: 0
Views: 183
Reputation: 2000
Your response
array has only one element (at index 0):
final String[] response = {""};
this makes the string array to conceptually look like this: [""]
. This way there is no problem in accessing the first element of the array like this: response[0]
.
Trying to access the second element in the array like this: response[1]
throws an ArrayIndexOutOfBoundsException
since there is no element at index 1 for an array of size 1 (with an element only at the index 0).
You probably want to initialize your array so it looks like this: ["", ""]
this way:
final String[] response = {"", ""};
You also shouldn't worry about the problem being
caused by the response too long returned by the server
Upvotes: 0