如何使用 Retrofit 调用 Json



我有如下JSON:

{
"Search": [
{
"Title": "Iron Man",
"Year": "2008"
},
{
"Title": "Iron Man2",
"Year": "20010"
},
{
"Title": "Iron Man3",
"Year": "2013"
}
]
}

然后我创建了如下 API 服务:

public interface MyApi {
@GET("/movies")
Call<ArrayList<MovieSearch>> getartistdata();
}

我的数据类如下

public class MovieSearch {
@SerializedName("Search")
public List<Search> Search =null;
}
public class Search {
@SerializedName("Title")
public String Title="";
@SerializedName("Year")
public String Year="";
@SerializedName("Poster")
public String Poster="";
public Search() {
}
public Search(String Title, String Year, String Poster) {
this.Title = Title;
this.Year = Year;
}
}

现在我正在尝试实现视图模型类,如下所示

public class MyListViewModel extends ViewModel {
public String Title = "";
public String Year= "";
public String Poster = "";
public MutableLiveData<ArrayList<MyListViewModel>> mutableLiveData = new MutableLiveData<>();
private ArrayList<MyListViewModel> arrayList;
private ArrayList<MovieSearch> myList;
public String getImageurl() {
return Poster;
}
@BindingAdapter({"imageUrl"})
public static void loadimage(ImageView imageView, String imageUrl) {
Glide.with(imageView.getContext()).load(imageUrl).apply(RequestOptions.circleCropTransform()).into(imageView);
//Picasso.with(imageView.getContext()).load(imageUrl).into(imageView);
}
public MyListViewModel() {
}
public MyListViewModel(MovieSearch myList) {
this.Title = myList.Title;
this.Poster = myList.Poster;
this.Year= myList.Year;
}
public MutableLiveData<ArrayList<MyListViewModel>> getMutableLiveData() {
arrayList = new ArrayList<>();
MyApi api = MyClient.getInstance().getMyApi();
Call<ArrayList<MovieSearch>> call = api.getartistdata();
call.enqueue(new Callback<ArrayList<MovieSearch>>() {
@Override
public void onResponse(Call<ArrayList<MovieSearch>> call, Response<ArrayList<MovieSearch>> response) {
myList = new ArrayList<>();
myList = response.body();
for (int i = 0; i < myList.size(); i++) {
MovieSearch myk = myList.get(i);
MyListViewModel myListViewModel = new MyListViewModel(myk);
arrayList.add(myListViewModel);
mutableLiveData.setValue(arrayList);
}
}
@Override
public void onFailure(Call<ArrayList<MovieSearch>> call, Throwable t) {
}
});
return mutableLiveData;
}
}

但是我在以下方面收到"无法解析符号"错误:

public MyListViewModel(MovieSearch myList) {
this.Title = myList.Title;
this.Poster = myList.Poster;
this.Year= myList.Year;
}

我正在尝试从 JSON 获取数据并将其绑定到视图持有者。但是我不知道该怎么称呼它。有人请帮我修复它。

你已经非常接近了,你只需要对服务类和视图模型进行一些更改就可以完成这项工作。进行以下更改

MyApi 接口:->因为你的 json 不是 arraylist,而是 MovieSearch 的对象,所以在搜索的数组列表存在的情况下进行相应的更改。

public interface MyApi {
@GET("/movies")
Call<MovieSearch> getartistdata();
}

MyListView模型类:

public class MyListViewModel extends ViewModel {
public MutableLiveData<ArrayList<Search>> mutableLiveData = new MutableLiveData<>();
@BindingAdapter({"imageUrl"})
public static void loadimage(ImageView imageView, String imageUrl) {
Glide.with(imageView.getContext()).load(imageUrl).apply(RequestOptions.circleCropTransform()).into(imageView);
//Picasso.with(imageView.getContext()).load(imageUrl).into(imageView);
}

public MyListViewModel() {
//do something else, your view model is not POJO it's the handler not storage.
}
public MutableLiveData<ArrayList<Search>> getSearchResults() {
MutableLiveData<ArrayList<Search>> localData = new MutableLiveData<>();
MyApi api = MyClient.getInstance().getMyApi();
Call<MovieSearch> call = api.getartistdata();
call.enqueue(new Callback<MovieSearch>() {
@Override
public void onResponse(Call<MovieSearch> call, Response<MovieSearch> response) {
List<Search> myList = response.body().Search; 
mutableLiveData.setValue(myList);
localData.setValue(myList);
}
@Override
public void onFailure(Call<ArrayList<MovieSearch>> call, Throwable t) {
//handle the error
}
});
return localData;
}
}

通过创建视图模型的对象并在某个函数中调用上面的视图模型。 这部分通常位于一些 UI 类活动或片段中:

//listening for change in live data
// this would be in ui layer
myListViewModelObject.getSearchResults().observe(this, new Observer<ArrayList<Search>>() {
@Override
public void onChanged(@Nullable ArrayList<Search> obj) {
// handle changes here, to use the data write code below
}
});

建议:不要使用视图模型来存储数据,这些数据不是为了什么。它用于处理数据和操作,而不是数据的守护者。在视图模型中管理数据和业务逻辑可能会导致许多问题。如果可能的话,总是将事情分解成更小的部分,例如,从其他类或 mathod 获取改造对象,不要将其放在调用方法本身中,这将代码重复,并且还可能影响性能。

注意:我还没有测试代码,只是做了最好的更改,它应该运行。如果我删除了任何部分,请添加缺少的部分。

在这里,我注意到您保留了一个视图模型数组。最主要的是ViewModels不应该那样使用。

我认为如果您阅读ViewModels的一些文档和示例实现以更好地理解,那就更好了。


来到您的实施,一切看起来都很好,但有一件事是将您的MovieSearch列表转换为MyListViewModel列表。

在视图中模型

public class MyListViewModel extends ViewModel {
private MutableLiveData<MovieSearch> mutableLiveData = new MutableLiveData<>();
@BindingAdapter({"imageUrl"})
public static void loadimage(ImageView imageView, String imageUrl) {
Glide.with(imageView.getContext()).load(imageUrl).apply(RequestOptions.circleCropTransform()).into(imageView);
//Picasso.with(imageView.getContext()).load(imageUrl).into(imageView);
}
public LiveData<MovieSearch> getMutableLiveData() {
loadData();
return mutableLiveData;
}
private void loadData(){
MyApi api = MyClient.getInstance().getMyApi();
Call<MovieSearch> call = api.getartistdata();
call.enqueue(new Callback<MovieSearch>() {
@Override
public void onResponse(Call<MovieSearch> call, Response<MovieSearch> response) {
MovieSearch movieSearch = response.body();
mutableLiveData.setValue(myList); 
}
@Override
public void onFailure(Call<ArrayList<MovieSearch>> call, Throwable t) {
}
});
}
}

在活动或片段中,您可以通过getMutableLiveData()访问 liveData,并将List<MovieSearch>设置为适配器。


还有一件事,鉴于您的响应 JSON,您的 API 应该如下所示

public interface MyApi {
@GET("/movies")
Call<MovieSearch> getartistdata();
}

MovieSearch不在列表中


适配器

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {
private ArrayList<Search> arrayList = new ArrayList<>;
private LayoutInflater layoutInflater;
public void submitList(ArrayList<Search> searchList){
this.arrayList = searchList;
notifyDataSetChanged();
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
if (layoutInflater==null){
layoutInflater=LayoutInflater.from(parent.getContext());
}
MyListBinding myListBinding= DataBindingUtil.inflate(layoutInflater, R.layout.mylist,parent,false);
return new ViewHolder(myListBinding);
}
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
Search search =arrayList.get(position);
holder.bind(search);
}
@Override
public int getItemCount() {
return arrayList.size();
}
class ViewHolder extends RecyclerView.ViewHolder{
private MyListBinding myListBinding;
public ViewHolder(@NonNull MyListBinding myListBinding) {
super(myListBinding.getRoot());
this.myListBinding=myListBinding;
}
public void bind(Search myli){
this.myListBinding.setMylistmodel(myli);
myListBinding.executePendingBindings();
}
public MyListBinding getMyListBinding(){
return myListBinding;
}
}
}

活动

adapter = MyAdapter();
recyclerview=(RecyclerView)findViewById(R.id.recyclerView);
recyclerview.setLayoutManager(new LinearLayoutManager(getApplicationContext()));
recyclerview.setAdapter(adapter);
myListViewModel = ViewModelProviders.of(this).get(MyListViewModel.class);
myListViewModel.getSearchResults().observe(this, new Observer<MovieSearch>() {
@Override
public void onChanged(@Nullable MovieSearch movieSearch) {
// handle changes here, to use the data write code below
adapter.submitList(movieSearch.Search);
}
});

还有一件小事,请不要以大写字母开头的变量名称。最好从一开始就遵循命名约定。

最新更新