我尝试使用retorfit2解析https://favqs.com/api/。首先,我希望至少从json中提取日期
@Generated("jsonschema2pojo")
public class Example {
@SerializedName("qotd_date")
@Expose
private String qotdDate;
@SerializedName("quote")
@Expose
private Quote quote;
public String getQotdDate() {
return qotdDate;
}
public void setQotdDate(String qotdDate) {
this.qotdDate = qotdDate;
}
public Quote getQuote() {
return quote;
}
public void setQuote(Quote quote) {
this.quote = quote;
}
}
课程引言:
@Generated("jsonschema2pojo")
public class Quote {
@SerializedName("id")
@Expose
private Integer id;
@SerializedName("dialogue")
@Expose
private Boolean dialogue;
@SerializedName("private")
@Expose
private Boolean _private;
@SerializedName("tags")
@Expose
private List<Object> tags = null;
@SerializedName("url")
@Expose
private String url;
@SerializedName("favorites_count")
@Expose
private Integer favoritesCount;
@SerializedName("upvotes_count")
@Expose
private Integer upvotesCount;
@SerializedName("downvotes_count")
@Expose
private Integer downvotesCount;
@SerializedName("author")
@Expose
private String author;
@SerializedName("author_permalink")
@Expose
private String authorPermalink;
@SerializedName("body")
@Expose
private String body;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public Boolean getDialogue() {
return dialogue;
}
public void setDialogue(Boolean dialogue) {
this.dialogue = dialogue;
}
public Boolean getPrivate() {
return _private;
}
public void setPrivate(Boolean _private) {
this._private = _private;
}
public List<Object> getTags() {
return tags;
}
public void setTags(List<Object> tags) {
this.tags = tags;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public Integer getFavoritesCount() {
return favoritesCount;
}
public void setFavoritesCount(Integer favoritesCount) {
this.favoritesCount = favoritesCount;
}
public Integer getUpvotesCount() {
return upvotesCount;
}
public void setUpvotesCount(Integer upvotesCount) {
this.upvotesCount = upvotesCount;
}
public Integer getDownvotesCount() {
return downvotesCount;
}
public void setDownvotesCount(Integer downvotesCount) {
this.downvotesCount = downvotesCount;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public String getAuthorPermalink() {
return authorPermalink;
}
public void setAuthorPermalink(String authorPermalink) {
this.authorPermalink = authorPermalink;
}
public String getBody() {
return body;
}
public void setBody(String body) {
this.body = body;
}
}
类客户端:
public interface Client {
@GET("qotd_date")
Call<List<Example>> reposForUser();
}
类MyAdapter:
public class MyAdapter extends ArrayAdapter<Example> {
private Context context;
private List<Example> values;
public MyAdapter(Context context, List<Example> values) {
super(context, R.layout.list_item_pagination, values);
this.context = context;
this.values = values;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
if (row == null) {
LayoutInflater inflater =
(LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = inflater.inflate(R.layout.list_item_pagination, parent, false);
}
TextView textView = (TextView) row.findViewById(R.id.list_item_pagination_text);
Example item = values.get(position);
String message = item.getQotdDate();
textView.setText(message);
return row;
}
}
类主要活动:
public class MainActivity extends AppCompatActivity {
private ListView listView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView = (ListView) findViewById(R.id.pagination_list);
Retrofit.Builder builder = new Retrofit.Builder()
.baseUrl("https://favqs.com/api/")
.addConverterFactory(GsonConverterFactory.create());
Retrofit retrofit = builder.build();
Client client = retrofit.create(Client.class);
Call<List<Example>> call = client.reposForUser();
call.enqueue(new Callback<List<Example>>() {
@Override
public void onResponse(Call<List<Example>> call, Response<List<Example>> response) {
List<Example> repos = response.body();
listView.setAdapter(new MyAdapter(MainActivity.this, repos));
}
@Override
public void onFailure(Call<List<Example>> call, Throwable t) {
Toast.makeText(MainActivity.this, "error :(", Toast.LENGTH_SHORT).show();
}
});
}
}
应用程序会启动,但不会在listView中输出任何内容。吐司会显示消息:“错误:(“
也许问题发生在代码的这一部分:
call.enqueue(new Callback<List<Example>>() {
@Override
public void onResponse(Call<List<Example>> call, Response<List<Example>> response) {
List<Example> repos = response.body();
Log.d("mylog1", String.valueOf(repos));
Log.d("mylog1","mylog");
listView.setAdapter(new MyAdapter(MainActivity.this, repos));
}
@Override
public void onFailure(Call<List<Example>> call, Throwable t) {
Toast.makeText(MainActivity.this, "error :(", Toast.LENGTH_SHORT).show();
t.printStackTrace();
}
});
call.enqueue (new Callback <List <Example>> () {
之后的代码未开始执行
方法启动onFailure(Call<List<Example>> call, Throwable t)
字符串t.printStackTrace();
给出错误消息:
W/System.err: com.google.gson.stream.MalformedJsonException: Use JsonReader.setLenient(true) to accept malformed JSON at line 1 column 1 path $
W/System.err: at com.google.gson.stream.JsonReader.syntaxError(JsonReader.java:1562)
at com.google.gson.stream.JsonReader.checkLenient(JsonReader.java:1403)
at com.google.gson.stream.JsonReader.doPeek(JsonReader.java:592)
at com.google.gson.stream.JsonReader.peek(JsonReader.java:424)
at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.read(CollectionTypeAdapterFactory.java:74)
at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.read(CollectionTypeAdapterFactory.java:61)
at retrofit2.converter.gson.GsonResponseBodyConverter.convert(GsonResponseBodyConverter.java:40)
at retrofit2.converter.gson.GsonResponseBodyConverter.convert(GsonResponseBodyConverter.java:27)
at retrofit2.OkHttpCall.parseResponse(OkHttpCall.java:243)
W/System.err: at retrofit2.OkHttpCall$1.onResponse(OkHttpCall.java:153)
at okhttp3.RealCall$AsyncCall.execute(RealCall.java:174)
at okhttp3.internal.NamedRunnable.run(NamedRunnable.java:32)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
at java.lang.Thread.run(Thread.java:923)
W/System: A resource failed to call close.
A resource failed to call close.
1条答案
按热度按时间2ledvvac1#
从调用中删除List,因为端点favqs.com/api/qotd返回的是一个对象,而不是列表