早上好
我目前正在用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来获得服务器的“响应”,但这并没有解决问题,并为此创建了一个类。
更新:为什么我的String标签'result'是空的(最重要的是为什么服务器不返回任何东西)。.
1条答案
按热度按时间7cjasjjr1#
response
数组只有一个元素(索引为0):这使得字符串数组在概念上看起来像这样:
[""]
。这样就可以像这样访问数组的第一个元素:response[0]
。尝试像这样访问数组中的第二个元素:
response[1]
抛出ArrayIndexOutOfBoundsException
,因为对于大小为1的数组,索引1处没有元素(只有索引0处的元素)。你可能想初始化你的数组,使它看起来像这样:
["", ""]
这样:你也不用担心
由于服务器返回的响应过长而导致