Android 我无法将JSON解析为列表视图(Pokeapi)
我试图教自己如何在Android Studio中使用RESTAPI。作为一个整体,我对Android开发非常陌生,这将是我第二次使用RESTAPI。我曾尝试在YouTube上学习一些教程,但我仍然有一些问题,我觉得解决方案会非常简单,我会感到非常愚蠢,特别是因为我正在使用改型。。。我现在只想看看列表视图中显示的口袋妖怪名称 这是我的代码文件Android 我无法将JSON解析为列表视图(Pokeapi),android,json,retrofit,retrofit2,Android,Json,Retrofit,Retrofit2,我试图教自己如何在Android Studio中使用RESTAPI。作为一个整体,我对Android开发非常陌生,这将是我第二次使用RESTAPI。我曾尝试在YouTube上学习一些教程,但我仍然有一些问题,我觉得解决方案会非常简单,我会感到非常愚蠢,特别是因为我正在使用改型。。。我现在只想看看列表视图中显示的口袋妖怪名称 这是我的代码文件 public class MainActivity extends AppCompatActivity { @Override prot
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();
}
});
}
}
非常感谢您的帮助!
谢谢各位:
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"
}
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
)
);
}
您无法直接将列表作为响应获取,因为所需列表位于响应的results标记下。因此,您必须为此创建不同的POJO类。使用JSON到POJO转换器从JSON创建POJO类。在本例中,我使用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;
}
}
所以你的回答应该这样处理:
{
"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"
}
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
)
);
}
您必须根据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();
}
});
}
}您是否能够获得json响应,如果可以,请发布json响应response@SonuSanjeev-如果您想亲自查看,我已将链接添加到我的回购协议中?:请发布您遇到的任何错误。您是否在eneque回调中收到任何响应/错误?