http 接口数据
api
package com.example.work12_10.mvvm.http;
import com.example.work12_10.entity.VideoEntity;
import io.reactivex.Observable;
import retrofit2.http.GET;
import retrofit2.http.Query;
public interface Api {
@GET("/video/findVideos?")
Observable<VideoEntity> getVideoData(@Query("currentPage")int currentPage, @Query("pageSize")int pageSize);
}
RetrofitUtils
package com.example.work12_10.mvvm.http;
import java.util.concurrent.TimeUnit;
import io.reactivex.android.schedulers.AndroidSchedulers;
import okhttp3.OkHttpClient;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;
public class RetrofitUtils {
private RetrofitUtils() {
}
private static RetrofitUtils retrofitUtils = null;
private Retrofit retrofit;
public static RetrofitUtils getInstance() {
if(retrofitUtils == null) {
retrofitUtils = new RetrofitUtils();
}
return retrofitUtils;
}
public Retrofit getRetrofit() {
if(retrofit == null) {
retrofit = createRetrofit();
}
return retrofit;
}
private Retrofit createRetrofit() {
OkHttpClient okHttpClient = new OkHttpClient.Builder()
.connectTimeout(5, TimeUnit.MINUTES)
.writeTimeout(5, TimeUnit.MINUTES)
.readTimeout(5, TimeUnit.MINUTES)
.addInterceptor(new HttpLoggingInterceptor().setLevel(HttpLoggingInterceptor.Level.BODY))
.build();
retrofit = new Retrofit.Builder()
.baseUrl("http://10.161.9.80:7005/")
.client(okHttpClient)
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.addConverterFactory(GsonConverterFactory.create())
.build();
return retrofit;
}
}
model 解耦
VideoModel
package com.example.work12_10.mvvm.model;
import com.example.work12_10.entity.VideoEntity;
import com.example.work12_10.mvvm.http.Api;
import com.example.work12_10.mvvm.http.RetrofitUtils;
import io.reactivex.Observable;
public class VideoModel {
protected Api api;
public VideoModel() {
api = RetrofitUtils.getInstance().getRetrofit().create(Api.class);
}
public Observable<VideoEntity> getVideoData(int currentPage, int pageSize) {
return api.getVideoData(currentPage,pageSize);
}
}
viewmodel 暂时没搞清楚实际作用(待改进)
VideoModel
package com.example.work12_10.mvvm.model;
import com.example.work12_10.entity.VideoEntity;
import com.example.work12_10.mvvm.http.Api;
import com.example.work12_10.mvvm.http.RetrofitUtils;
import io.reactivex.Observable;
public class VideoModel {
protected Api api;
public VideoModel() {
api = RetrofitUtils.getInstance().getRetrofit().create(Api.class);
}
public Observable<VideoEntity> getVideoData(int currentPage, int pageSize) {
return api.getVideoData(currentPage,pageSize);
}
}
view 视图层 展示数据
MainActivity
package com.example.work12_10.mvvm.view;
import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;
import android.os.Bundle;
import com.example.work12_10.R;
import com.example.work12_10.adapter.MyVideoAdapter;
import com.example.work12_10.databinding.ActivityMainBinding;
import com.example.work12_10.entity.VideoEntity;
import com.example.work12_10.mvvm.viewmodel.VideoViewModel;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
ActivityMainBinding mainBinding;
private List<VideoEntity.DataDTO> list = new ArrayList<>();
private MyVideoAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mainBinding = DataBindingUtil.setContentView(this,R.layout.activity_main);
VideoViewModel videoViewModel = new ViewModelProvider(this).get(VideoViewModel.class);
videoViewModel.getVideo(1,20);
videoViewModel.videoEntityData.observe(this, videoEntity -> {
list.addAll(videoEntity.getData());
adapter = new MyVideoAdapter(R.layout.item_video,list);
mainBinding.rv.setLayoutManager(new StaggeredGridLayoutManager(2,StaggeredGridLayoutManager.VERTICAL));
mainBinding.rv.setAdapter(adapter);
adapter.notifyDataSetChanged();
});
}
}