我正在教自己如何在Android中使用REST。作为一个整体,我对非常陌生,这将是我第二次使用REST。我试着学习一些关于YouTube的教程,但是我仍然有一些问题,我觉得解决方案将非常简单,我会感到非常愚蠢,特别是因为我正在使用RetroFit.我只想看到列表视图中显示的Pokemon名称(至少现在是这样)。
这是我的代码文件
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;
}
}任何帮助都是非常感谢的!谢谢您:)
发布于 2017-12-31 07:36:38
您必须根据json response.So编写模型类,在本例中,您应该将"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();
}
});
}}
发布于 2017-12-31 06:52:00
您的GET请求为您提供了一个JSON对象作为响应,而不是数组或List。现在的反应如下:
{
"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类。在本例中,我使用http://www.jsonschema2pojo.org/创建了如下所示的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
)
);
}https://stackoverflow.com/questions/48040269
复制相似问题