早安,
我目前正在 PokeApi Api 上使用 Java 在 android studio 中开发一个项目。不幸的是,即使可以从浏览器访问该链接,我也无法从服务器获得响应。如果你有想法,我很感兴趣。 谢谢。
我的班级:
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();
}
}
}
您认为这可能是服务器返回的响应太长造成的吗?
我尝试实现库 Volley 以获取服务器的“响应”,但这并没有解决问题并为此创建一个类。
你的
response
数组只有一个元素(在索引 0 处):
final String[] response = {""};
这使得字符串数组在概念上看起来像这样:
[""]
。这样访问数组的第一个元素就没有问题了:response[0]
.
尝试像这样访问数组中的第二个元素:
response[1]
抛出一个ArrayIndexOutOfBoundsException
,因为对于大小为 1 的数组,索引 1 处没有元素(只有索引 0 处有一个元素)。
你可能想要初始化你的数组,所以它看起来像这样:
["", ""]
这样:
final String[] response = {"", ""};
你也不用担心这个问题
服务器返回的response过长导致