接口;
http://api.tianapi.com/nba/?key=71e58b5b2f930eaf1f937407acde08fe&num=10
依赖:
compile 'com.squareup.retrofit2:retrofit:2.3.0'
compile 'com.squareup.retrofit2:converter-gson:2.3.0'
compile 'com.squareup.retrofit2:adapter-rxjava2:2.3.0'
compile 'io.reactivex.rxjava2:rxandroid:2.0.1'
compile 'com.squareup.retrofit2:converter-scalars:2.3.0'
compile 'io.reactivex.rxjava2:rxjava:2.1.7'
compile 'com.android.support:recyclerview-v7:26.1.0'
compile 'com.facebook.fresco:fresco:1.5.0'
IApplication
package com.example.myapplication;
import android.app.Application;
import com.facebook.drawee.backends.pipeline.Fresco;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;
/**
* Created by Administrator on 2017/12/6.
*/
public class IApplication extends Application {
public static IGEtDataBase igEtDataBase;
@Override
public void onCreate() {
super.onCreate();
Fresco.initialize(this);
Retrofit retrofit=new Retrofit.Builder()
.baseUrl("http://api.tianapi.com")
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.addConverterFactory(GsonConverterFactory.create())
.build();
igEtDataBase = retrofit.create(IGEtDataBase.class);
}
}
package com.example.myapplication;
import com.example.myapplication.bean.Bean;
import java.util.Map;
import io.reactivex.Observable;
import retrofit2.http.FieldMap;
import retrofit2.http.FormUrlEncoded;
import retrofit2.http.GET;
import retrofit2.http.POST;
import retrofit2.http.QueryMap;
import retrofit2.http.Url;
/**
* Created by Administrator on 2017/12/6.
*/
public interface IGEtDataBase {
@GET("/nba/")
Observable get(@QueryMap Map map);
/*
@GET
Observable get(@Url String url, @QueryMap Map map);
@GET
Observable get(@Url String url);
@FormUrlEncoded
@POST
Observable post(@Url String url, @FieldMap Map map);*/
}
model层
MyModelCallBack
package com.example.myapplication.model;
import com.example.myapplication.bean.Bean;
/**
* Created by Administrator on 2017/12/6.
*/
public interface MyModelCallBack {
public void success(Bean bean);
public void fail(Exception e);
}
package com.example.myapplication.model;
import com.example.myapplication.IApplication;
import com.example.myapplication.bean.Bean;
import java.util.HashMap;
import java.util.Map;
import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;
import retrofit2.Call;
/**
* Created by Administrator on 2017/12/6.
*/
public class MyModel {
public void getData(final MyModelCallBack callBack){
Map map = new HashMap<>();
map.put("key","71e58b5b2f930eaf1f937407acde08fe");
map.put("num","10");
IApplication.igEtDataBase.get(map)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Consumer() {
@Override
public void accept(Bean bean) throws Exception {
callBack.success(bean);
}
});
}
}
MyListener
package com.example.myapplication.view;
import com.example.myapplication.bean.Bean;
/**
* Created by Administrator on 2017/12/6.
*/
public interface MyListener {
public void success(Bean bean);
public void fail(Exception e);
}
MyPresenter
package com.example.myapplication.presenter;
import com.example.myapplication.bean.Bean;
import com.example.myapplication.model.MyModel;
import com.example.myapplication.model.MyModelCallBack;
import com.example.myapplication.view.MyListener;
/**
* Created by Administrator on 2017/12/6.
*/
public class MyPresenter {
MyListener listener;
MyModel model;
public MyPresenter(MyListener listener){
this.listener=listener;
this.model=new MyModel();
}
public void get(){
model.getData(new MyModelCallBack() {
@Override
public void success(Bean bean) {
listener.success(bean);
}
@Override
public void fail(Exception e) {
listener.fail(e);
}
});
}
}
package com.example.myapplication;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import com.example.myapplication.bean.Bean;
import com.example.myapplication.presenter.MyPresenter;
import com.example.myapplication.view.MyListener;
public class MainActivity extends AppCompatActivity implements MyListener{
private RecyclerView recyclerView;
private MyPresenter presenter;
private MyAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
recyclerView = findViewById(R.id.recycle);
presenter = new MyPresenter(this);
presenter.get();
LinearLayoutManager manager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
recyclerView.setLayoutManager(manager);
adapter = new MyAdapter(this);
recyclerView.setAdapter(adapter);
}
@Override
public void success(Bean bean) {
adapter.add(bean);
}
@Override
public void fail(Exception e) {
}
}
package com.example.myapplication;
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import com.example.myapplication.bean.Bean;
import com.facebook.drawee.view.SimpleDraweeView;
import java.util.ArrayList;
import java.util.List;
/**
* Created by Administrator on 2017/12/6.
*/
class MyAdapter extends RecyclerView.Adapter {
Context context;
List list;
public MyAdapter(Context context) {
this.context=context;
}
public void add(Bean bean) {
if(list==null){
list=new ArrayList<>();
}
list.addAll(bean.getNewslist());
notifyDataSetChanged();
}
@Override
public IViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view= LayoutInflater.from(context).inflate(R.layout.adapter_item,null);
return new IViewHolder(view);
}
@Override
public void onBindViewHolder(IViewHolder holder, int position) {
holder.simole.setImageURI(list.get(position).getPicUrl());
holder.title.setText(list.get(position).getTitle());
}
@Override
public int getItemCount() {
return list==null ? 0 : list.size();
}
public class IViewHolder extends RecyclerView.ViewHolder {
private final TextView title;
private final SimpleDraweeView simole;
public IViewHolder(View itemView) {
super(itemView);
title = itemView.findViewById(R.id.title);
simole = itemView.findViewById(R.id.simple);
}
}
}