我正在尝试自学如何在Android Studio中使用REST API。我对整个Android开发都很陌生,这只是我第二次使用REST API。我曾试图在YouTube上关注一些教程,但我仍然遇到问题,我觉得解决方案非常简单,我觉得非常愚蠢,特别是因为我正在使用RetroFit ......我只是想看看列表视图中显示的口袋妖怪名称(至少目前为止)。
以下是我的代码文件
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final ListView listView = (ListView) findViewById(R.id.listView);
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(Pokeapi.URL)
.addConverterFactory(GsonConverterFactory.create())
.build();
Pokeapi pokeapi = retrofit.create(Pokeapi.class);
Call<List<Pokemon>> call = pokeapi.getPokemonNameAndPic();
call.enqueue(new Callback<List<Pokemon>>() {
@Override
public void onResponse(Call<List<Pokemon>> call, Response<List<Pokemon>> response) {
List<Pokemon> pokemon = response.body();
String[] pokemonNames = new String[pokemon.size()];
for (int i = 0; i < pokemon.size(); i++) {
pokemonNames[i] = pokemon.get(i).getName();
}
listView.setAdapter(
new ArrayAdapter<String>(
getApplicationContext(),
android.R.layout.simple_list_item_1,
pokemonNames
)
);
}
@Override
public void onFailure(Call<List<Pokemon>> call, Throwable t) {
Toast.makeText(getApplicationContext(), t.getMessage(), Toast.LENGTH_SHORT).show();
}
});
}
}
_
public interface Pokeapi {
String URL = "https://pokeapi.co/api/v2/";
@GET("pokemon")
Call<List<Pokemon>> getPokemonNameAndPic();
}
_
public class Pokemon {
private String url;
private String name;
public Pokemon(String url, String name) {
this.url = url;
this.name = name;
}
public String getUrl() {
return url;
}
public String getName() {
return name;
}
}
非常感谢任何帮助! 谢谢:))
答案 0 :(得分:1)
你必须根据你的json响应编写模型类。所以在这种情况下你应该将你的“Pokemon”类改为:
public class Data {
@SerializedName("count")
private Integer count;
@SerializedName("previous")
private Object previous;
@SerializedName("results")
private List<Pokemon> results = null;
@SerializedName("next")
private String next;
public Integer getCount() {
return count;
}
public void setCount(Integer count) {
this.count = count;
}
public Object getPrevious() {
return previous;
}
public void setPrevious(Object previous) {
this.previous = previous;
}
public List<Pokemon> getResults() {
return results;
}
public void setResults(List<Pokemon> results) {
this.results = results;
}
public String getNext() {
return next;
}
public void setNext(String next) {
this.next = next;
}
public class Pokemon {
@SerializedName("url")
private String url;
@SerializedName("name")
private String name;
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
}
你的api界面如下:
public interface Pokeapi {
String URL = "https://pokeapi.co/api/v2/";
@GET("pokemon")
Call<Data> getPokemonNameAndPic();
}
将mainActivity更新为:
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final ListView listView = (ListView) findViewById(R.id.listView);
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(Pokeapi.URL)
.addConverterFactory(GsonConverterFactory.create())
.build();
Pokeapi pokeapi = retrofit.create(Pokeapi.class);
Call<Data> call = pokeapi.getPokemonNameAndPic();
call.enqueue(new Callback<Data>() {
@Override
public void onResponse(Call<Data> call, Response<Data> response) {
Log.d("response", response.body().toString());
Data data = response.body();
String[] pokemonNames = new String[data.getResults().size()];
for (int i = 0; i < data.getResults().size(); i++) {
pokemonNames[i] = data.getResults().get(i).getName();
}
for (String item : pokemonNames){
Log.d("item", item);
}
listView.setAdapter(
new ArrayAdapter<String>(
getApplicationContext(),
android.R.layout.simple_list_item_1,
pokemonNames
)
);
}
@Override
public void onFailure(Call<Data> call, Throwable t) {
Toast.makeText(getApplicationContext(), t.getMessage(), Toast.LENGTH_SHORT).show();
}
});
}
}
答案 1 :(得分:0)
您的 GET 请求为您提供了一个JSON对象作为响应,而不是数组,也不是列表。现在响应看起来像这样:
{
"count": 949,
"previous": null,
"results": [
{
"url": "https://pokeapi.co/api/v2/pokemon/1/",
"name": "bulbasaur"
},
{
"url": "https://pokeapi.co/api/v2/pokemon/13/",
"name": "weedle"
},
{
"url": "https://pokeapi.co/api/v2/pokemon/20/",
"name": "raticate"
}
],
"next": "https://pokeapi.co/api/v2/pokemon/?limit=20&offset=20"
}
您无法直接获得List<Pokemon>
作为回复,因为您的所需列表位于回复的"results"
标记下。所以你必须为此创建一个不同的POJO类。使用JSON到POJO转换器从JSON创建pojo类。在这种情况下,我使用Support Link来制作看起来像这样的POJO类。
-----------------------------------com.example.Example.java-----------------------------------
package com.example;
import java.util.List;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
public class Example {
@SerializedName("count")
@Expose
private Integer count;
@SerializedName("previous")
@Expose
private Object previous;
@SerializedName("results")
@Expose
private List<Result> results = null;
@SerializedName("next")
@Expose
private String next;
public Integer getCount() {
return count;
}
public void setCount(Integer count) {
this.count = count;
}
public Object getPrevious() {
return previous;
}
public void setPrevious(Object previous) {
this.previous = previous;
}
public List<Result> getResults() {
return results;
}
public void setResults(List<Result> results) {
this.results = results;
}
public String getNext() {
return next;
}
public void setNext(String next) {
this.next = next;
}
}
-----------------------------------com.example.Result.java-----------------------------------
package com.example;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
public class Result {
@SerializedName("url")
@Expose
private String url;
@SerializedName("name")
@Expose
private String name;
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
所以你的回答应该像这样处理:
call.enqueue(new Callback<Example>() {
@Override
public void onResponse(Call<Example> call, Response<Example> response) {
List<Result > pokemon = response.body().getResults();
String[] pokemonNames = new String[pokemon.size()];
for (int i = 0; i < pokemon.size(); i++) {
pokemonNames[i] = pokemon.get(i).getName();
}
listView.setAdapter(
new ArrayAdapter<String>(
getApplicationContext(),
android.R.layout.simple_list_item_1,
pokemonNames
)
);
}