嘿,我是mvvm架构的新手,我试图使用改造功能来调用Google图书api,并在列表视图中显示数据,并且我遇到了一些问题。
检查码:https://github.com/OmarQaisi/MVVM-Example
1-是否可以进行改进以返回对象列表而不是JsonObject(如果我的模型类不具有json主体内的所有变量)?一个简单的例子:可以说Json响应有4个实例(bookName,bookPages,bookAuthor,bookPrice),而我的Book.java类只有2个实例(bookName和bookAuthor)可以翻新自动将Json响应转换为Book Object吗?
到目前为止,这是我将JsonObject转换为我的应用程序中的Book对象的方式:
public class BookViewModel extends ViewModel {
private static final String TAG = "BookViewModel";
public MutableLiveData<List<Book>> books = new MutableLiveData<>();
private List<Book> dataSet = new ArrayList<>();
public void getBooks(String bookName, int maxResult){
BookClient.getInstance().getBooks(bookName,maxResult).enqueue(new Callback<JsonObject>() {
@Override
public void onResponse(Call<JsonObject> call, Response<JsonObject> response) {
if (!response.isSuccessful()) {
Log.d(TAG, "Code: " + response.code());
return;
}
try {
JsonObject root = response.body();
JsonArray booksArray = root.getAsJsonArray("items");
for (int i=0; i<booksArray.size(); i++){
JsonObject currentObject = booksArray.get(i).getAsJsonObject();
JsonObject info = currentObject.getAsJsonObject("volumeInfo");
JsonElement bookTitle = info.get("title");
if(info.has("averageRating") && info.has("description")){
JsonElement bookDescription= info.get("description");
JsonElement bookRating = info.get("averageRating");
JsonElement bookURL = info.get("infoLink");
JsonElement bookRatingReviews = info.get("ratingsCount");
JsonElement bookPageCount = info.get("pageCount");
String bookCategories= "";
if(info.has("categories")){
JsonArray categories = info.getAsJsonArray("categories");
JsonElement bookCategoriesElement = categories.get(0);
bookCategories = bookCategoriesElement.getAsString();
}
JsonObject imageLinks = info.getAsJsonObject("imageLinks");
JsonElement bookThumbnail = imageLinks.get("thumbnail");
JsonElement bookPublishedDate = info.get("publishedDate");
JsonElement bookAuthor;
if(info.has("authors")){
JsonArray authors = info.getAsJsonArray("authors");
bookAuthor = authors.get(0);
}else
bookAuthor = info.get("publisher");
dataSet.add(new Book(bookTitle.getAsString(),
bookAuthor.getAsString(),
bookDescription.getAsString(),
bookThumbnail.getAsString(),
bookURL.getAsString(),
bookCategories,
bookPublishedDate.getAsString(),
bookRating.getAsDouble(),
bookRatingReviews.getAsInt(),
bookPageCount.getAsInt()));
}
}
}catch (JsonParseException e){
Log.e(TAG, "Problem parsing the book JSON results", e);
}
books.setValue(dataSet);
}
@Override
public void onFailure(Call<JsonObject> call, Throwable t) {
Log.d(TAG, "onFailure: " + t.getMessage());
}
});
}//getBooks
} // BookViewModel
2-我搜索新书后,我的Listview没有更新!
3-使用mvvm进行改造时,应该避免任何错误吗?还是我在代码中制作了其中任何一个?请任何提示/建议都意味着很多。
谢谢您<3 <3
1。是否可以获取翻新的对象列表回答->是,点击api后,使用序列化将Json Element转换为Java对象。2。为什么您的列表未更新,因为您已将数据集声明为全局数据集,所以我对其进行了修复
public class BookViewModel extends ViewModel {
private static final String TAG = "BookViewModel";
public MutableLiveData<List<Book>> books = new MutableLiveData<>();
public void getBooks(String bookName, int maxResult){
BookClient.getInstance().getBooks(bookName,maxResult).enqueue(new Callback<JsonObject>() {
@Override
public void onResponse(Call<JsonObject> call, Response<JsonObject> response) {
List<Book> dataSet = new ArrayList<>();
if (!response.isSuccessful()) {
Log.d(TAG, "Code: " + response.code());
return;
}
try {
JsonObject root = response.body();
JsonArray booksArray = root.getAsJsonArray("items");
for (int i=0; i<booksArray.size(); i++){
JsonObject currentObject = booksArray.get(i).getAsJsonObject();
JsonObject info = currentObject.getAsJsonObject("volumeInfo");
JsonElement bookTitle = info.get("title");
if(info.has("averageRating") && info.has("description")){
JsonElement bookDescription= info.get("description");
JsonElement bookRating = info.get("averageRating");
JsonElement bookURL = info.get("infoLink");
JsonElement bookRatingReviews = info.get("ratingsCount");
JsonElement bookPageCount = info.get("pageCount");
String bookCategories= "";
if(info.has("categories")){
JsonArray categories = info.getAsJsonArray("categories");
JsonElement bookCategoriesElement = categories.get(0);
bookCategories = bookCategoriesElement.getAsString();
}
JsonObject imageLinks = info.getAsJsonObject("imageLinks");
JsonElement bookThumbnail = imageLinks.get("thumbnail");
JsonElement bookPublishedDate = info.get("publishedDate");
JsonElement bookAuthor;
if(info.has("authors")){
JsonArray authors = info.getAsJsonArray("authors");
bookAuthor = authors.get(0);
}else
bookAuthor = info.get("publisher");
dataSet.add(new Book(bookTitle.getAsString(),
bookAuthor.getAsString(),
bookDescription.getAsString(),
bookThumbnail.getAsString(),
bookURL.getAsString(),
bookCategories,
bookPublishedDate.getAsString(),
bookRating.getAsDouble(),
bookRatingReviews.getAsInt(),
bookPageCount.getAsInt()));
Log.d("boss", "onResponse: "+ bookAuthor.getAsString());
}
}
}catch (JsonParseException e){
Log.e(TAG, "Problem parsing the book JSON results", e);
}
books.setValue(dataSet);
Log.d("boss", "getBooks: "+ dataSet.size() + dataSet.get(0).getAuthor());
}
@Override
public void onFailure(Call<JsonObject> call, Throwable t) {
Log.d(TAG, "onFailure: " + t.getMessage());
}
});
}//getBooks
}//BookViewModel
`