进一步使用Retrofit,结合MVP让你的网络请求更加方便
来源:互联网 发布:必发数据 下载 编辑:程序博客网 时间:2024/06/05 01:01
//Adapter的类创建出来等会使用
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder>{
private Context context;
private List<MyBean.SongListBean> song_list;
public MyAdapter(Context context, List<MyBean.SongListBean> song_list) {
this.context = context;
this.song_list = song_list;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view=View.inflate(context, R.layout.rlv_item,null);
ViewHolder holder=new ViewHolder(view);
return holder;
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
holder.tv_title.setText(song_list.get(position).title);
holder.tv_name.setText(song_list.get(position).artist_name);
DraweeController controller= Fresco.newDraweeControllerBuilder()
.setUri(song_list.get(position).pic_big)
.setAutoPlayAnimations(true)
.build();
holder.sdv.setController(controller);
}
@Override
public int getItemCount() {
return song_list.size();
}
class ViewHolder extends RecyclerView.ViewHolder{
private final SimpleDraweeView sdv;
private final TextView tv_title;
private final TextView tv_name;
public ViewHolder(View itemView) {
super(itemView);
sdv = itemView.findViewById(R.id.sdv);
tv_title = itemView.findViewById(R.id.tv_title);
tv_name = itemView.findViewById(R.id.tv_name);
}
}
}
//定义一个接口 Api 等会要使用到
public interface MyApi {
//http://tingapi.ting.baidu.com/v1/restserver/ting?method=baidu.ting.billboard.billList&type=1&size=10&offset=0
public static final String URL="http://tingapi.ting.baidu.com/";
@GET("v1/restserver/ting?method=baidu.ting.billboard.billList&type=1&size=10&offset=0")
Observable<MyBean> getCall();
}
//定义一个Fresco的加载图片用到的类
public class MyApp extends Application{
@Override
public void onCreate() {
super.onCreate();
Fresco.initialize(this);
}
}
//创建一个Bean的类
//Model包下的类
public class ShopModel {
public void getShop(){
NetWorkUtils.getapi().getCall()
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Observer<MyBean>() {
@Override
public void onCompleted() {
System.out.println("====onCompleted===");
}
@Override
public void onError(Throwable e) {
System.out.println("====onError==="+e);
}
@Override
public void onNext(MyBean myBean) {
System.out.println("====onNext==="+myBean);
final List<MyBean.SongListBean> song_list = myBean.song_list;
onShop.getShopSuccess(song_list);
}
});
}
private OnShop onShop;
public void setOnShop(OnShop onShop) {
this.onShop = onShop;
}
public interface OnShop{
void getShopSuccess(List<MyBean.SongListBean> song_list);
}
}
//net包下创建一个NetWorkUtils的类
public class NetWorkUtils {
private NetWorkUtils(){
}
private static MyApi api;
public static MyApi getapi(){
if(api==null){
synchronized (NetWorkUtils.class){
if(api==null){
OkHttpClient client=new OkHttpClient.Builder()
.addInterceptor(new LoggingInterceptor())
.build();
Retrofit retrofit=new Retrofit.Builder()
.baseUrl(MyApi.URL)
.client(client)
.addConverterFactory(GsonConverterFactory.create())
.addCallAdapterFactory(RxJavaCallAdapterFactory.create())
.build();
api=retrofit.create(MyApi.class);
}
}
}
return api;
}
}
//presenter包下创建一个IShopPresenter的接口
public interface IShopPresenter<T> {
void Attech(T view);
void Detech();
}
//presenter包下创建一个ShopPresenter的类
public class ShopPresenter implements IShopPresenter<ShopView>,ShopModel.OnShop {
private ShopView shopView;
private final ShopModel shopModel;
private SoftReference<ShopView> softReference;
public ShopPresenter(ShopView shopView) {
this.shopView = shopView;
shopModel = new ShopModel();
shopModel.setOnShop(this);
}
public void requestShop(){
shopModel.getShop();
}
@Override
public void Attech(ShopView view) {
softReference=new SoftReference<ShopView>(view);
System.out.println("===attech===="+"绑定");
}
@Override
public void Detech() {
System.out.println("===detech==="+"解绑");
softReference.clear();
}
@Override
public void getShopSuccess(List<MyBean.SongListBean> song_list) {
shopView.getShopSuccess(song_list);
}
}
//view包下的view接口
public interface ShopView {
void getShopSuccess(List<MyBean.SongListBean> song_list);
}
//创建一个BeasActivity的类
public abstract class BaseActivity<T extends IShopPresenter> extends AppCompatActivity{
T mPresenter;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
createPresenter();
}
public abstract void createPresenter();
@Override
protected void onDestroy() {
super.onDestroy();
mPresenter.Detech();
}
}
//创建一个拦截器的类网络拦截器LoggingInterceptor
public class LoggingInterceptor implements Interceptor {
private static final String UA = "User-Agent";
@Override
public Response intercept(Chain chain) throws IOException {
Request request = chain.request()
.newBuilder()
.addHeader(UA, makeUA())
.build();
return chain.proceed(request);
}
private String makeUA() {
String s = Build.BRAND + "/" + Build.MODEL + "/" + Build.VERSION.RELEASE;
return Build.BRAND + "/" + Build.MODEL + "/" + Build.VERSION.RELEASE;
}
}
//MainActivity的类
public class MainActivity extends BaseActivity<ShopPresenter> implements ShopView {
@BindView(R.id.rlv)
RecyclerView rlv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.bind(this);
mPresenter.requestShop();
}
@Override
public void createPresenter() {
mPresenter=new ShopPresenter(this);
}
@Override
public void getShopSuccess(final List<MyBean.SongListBean> song_list) {
LinearLayoutManager linearLayoutManager=new LinearLayoutManager(MainActivity.this);
rlv.setLayoutManager(linearLayoutManager);
MyAdapter ma=new MyAdapter(MainActivity.this,song_list);
rlv.setAdapter(ma);
}
}
//Main的布局文件
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.bwei.duanzeshan.rxjava_retrofit.MainActivity">
<android.support.v7.widget.RecyclerView
android:id="@+id/rlv"
android:layout_width="match_parent"
android:layout_height="wrap_content"></android.support.v7.widget.RecyclerView>
</RelativeLayout>
//item的布局文件
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:fresco="http://schemas.android.com/apk/res-auto">
<com.facebook.drawee.view.SimpleDraweeView
android:id="@+id/sdv"
android:layout_width="150dp"
android:layout_height="100dp"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
fresco:placeholderImage="@mipmap/ic_launcher"
android:layout_marginLeft="18dp"
android:layout_marginStart="18dp"
android:layout_marginTop="17dp" />
<TextView
android:id="@+id/tv_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/sdv"
android:layout_marginLeft="12dp"
android:textSize="15sp"
android:layout_marginStart="12dp"
android:layout_toEndOf="@+id/sdv"
android:layout_toRightOf="@+id/sdv"
android:text="TextView" />
<TextView
android:id="@+id/tv_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/sdv"
android:layout_marginBottom="12dp"
android:layout_toEndOf="@+id/sdv"
android:layout_toRightOf="@+id/sdv"
android:text="TextView" />
</RelativeLayout>
//别忘了加联网权限呦!!!!
//加依赖
compile 'com.squareup.retrofit2:retrofit:2.0.2'
// Retrofit库
compile 'com.squareup.okhttp3:okhttp:3.1.2'
// Okhttp库
compile 'com.squareup.retrofit2:converter-gson:2.0.2'
compile 'io.reactivex:rxjava:1.0.14'
compile 'io.reactivex:rxandroid:1.0.1'
compile 'com.squareup.retrofit2:adapter-rxjava:2.0.2'
implementation 'com.android.support:recyclerview-v7:27.0.1'
compile 'com.jakewharton:butterknife:8.5.1'
annotationProcessor 'com.jakewharton:butterknife-compiler:8.5.1'
compile 'com.facebook.fresco:fresco:0.11.0'
- 进一步使用Retrofit,结合MVP让你的网络请求更加方便
- okhttp,让你的网络请求变得更加简单。
- 使用MVP+Retrofit+rxjava让你的代码更简洁
- 使用Retrofit和Rxjava让你的网络请求更简单
- 使用Retrofit和Rxjava让你的网络请求更简单
- rxjava 和retrofit 结合使用 请求网络
- RxJava结合Retrofit使用 自定义封装类结合泛型 请求网络数据+Fresco+RecyclerView+MVP分层
- Android基于Retrofit2.0+RxJava的结合使用,让你的网络请求更简单
- MVP+Retrofit封装网络请求
- RxJava 与 Retrofit 结合网络请求,你值得拥有
- 使用MVP+Retrofit+RxJava实现的的Android Demo (下)使用Retrofit+RxJava处理网络请求
- MVP框架使用Retrofit+Rxjava请求网络数据
- Retrofit网络请求和MVP的简单的讲解
- mvp下rxjava+retrofit的登录网络请求demo
- 简单的MVP+Retrofit+RxJava请求网络数据(一)
- (安卓) MVP 框架 (Rxjava2+Retrofit)结合 实现网络请求
- 使用Retrofit结合RxJava实现Get与Post网络请求
- 使用sed处理文件,方便你的程序进一步处理
- 第十三周项目2
- 第十二周项目4Floyd算法验证
- flex
- 假设检验之几种检验方法的比较
- 【第九周】项目2-二叉树遍历的递归算法
- 进一步使用Retrofit,结合MVP让你的网络请求更加方便
- 2017年11月21日开始重点强化学习
- jsp返回上一页
- aliyun.repo
- 简单了解阿里云Web应用防火墙(下篇)
- JSON解析异常-org.json.JSONException: Expected a ':' after a key
- AngularJs路由$依赖注入
- Android Mediaplayer+sufaceView
- 自定义的动画