RecyclerView+Retrofit+OkHttp+RxJava
阿新 • • 發佈:2017-12-06
泛型方法 bsp list row ket ica throws 一個 tro
*圖片初始化
public class FrescoClass extends Application{
@Override
public void onCreate() {
super.onCreate();
Fresco.initialize(this);
}
}
*主頁面
public class MainActivity extends AppCompatActivity {
private RecyclerView mRv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
Flowable<UserBean> add = RetrofitHelper.getShopAPI().add();
//使用Rxjava異步
add.subscribeOn(Schedulers.io())//被觀察者執行線程
.observeOn(AndroidSchedulers.mainThread())//觀察者線程
.subscribe(new Consumer<UserBean>() {
@Override
public void accept(UserBean userBean) throws Exception {
List<UserBean.NewslistBean> newslist = userBean.getNewslist();
mRv.setLayoutManager(new LinearLayoutManager(MainActivity.this));
MyAdapter myAdapter = new MyAdapter(newslist,MainActivity.this);
mRv.setAdapter(myAdapter);
// Toast.makeText(MainActivity.this,newslist.get(1).getCtime(),Toast.LENGTH_LONG).show();
}
});
}
private void initView() {
mRv = (RecyclerView) findViewById(R.id.rv);
}
}
*適配器
public class MyAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>{
private List<UserBean.NewslistBean> list;
private Context context;
public MyAdapter(List<UserBean.NewslistBean> list, Context context) {
this.list = list;
this.context = context;
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(context).inflate(R.layout.item,parent,false);
return new MyViewHolder(view);
}
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
MyViewHolder my = (MyViewHolder) holder;
my.tv.setText(list.get(position).getTitle());
Uri uri = Uri.parse(list.get(position).getPicUrl());
my.img.setImageURI(uri);
}
@Override
public int getItemCount() {
return list.size();
}
class MyViewHolder extends RecyclerView.ViewHolder{
private final SimpleDraweeView img;
private final TextView tv;
public MyViewHolder(View itemView) {
super(itemView);
img = itemView.findViewById(R.id.img);
tv = itemView.findViewById(R.id.tv);
}
}
}
*網絡接口
public class net {
public static final String A = "http://api.tianapi.com/";
public static final String B = "nba/?key=71e58b5b2f930eaf1f937407acde08fe&num=10";
}
* 封裝的網絡請求的類
public class RetrofitHelper {
private static OkHttpClient okHttpClient;
private static ServiceApi serviceApi;
static {
initOkHttpClient();
}
/**
* 初始化 OkHttpClient
*/
private static void initOkHttpClient() {
if (okHttpClient == null) {
synchronized (RetrofitHelper.class) {
if (okHttpClient == null) {
okHttpClient = new OkHttpClient.Builder()
.build();
}
}
}
}
/**
* 定義一個泛型方法
*
* @param clazz
* @param url
* @param <T>
* @return
*/
public static <T> T createAPI(Class<T> clazz, String url) {
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(url)
.client(okHttpClient)
.addConverterFactory(GsonConverterFactory.create())
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.build();
return retrofit.create(clazz);
}
public static ServiceApi getShopAPI() {
if (serviceApi == null) {
synchronized (RetrofitHelper.class) {
if (serviceApi == null) {
serviceApi = createAPI(ServiceApi.class, net.A);
}
}
}
return serviceApi;
}
}
*接口
public interface ServiceApi {
@GET(net.B)
Flowable<UserBean> add();
}
*封裝bean類
public class UserBean {
/**
* code : 200
* msg : success
* newslist : [{"ctime":"2016-12-04 13:00","title":"格林:理解科爾吸食大麻 不過我從沒吸過","description":"NBA新聞","picUrl":"http://www.51tyw.com/uploads/allimg/161204/1-161204120131.jpg","url":"http://www.51tyw.com/nba/2421.html"},{"ctime":"2016-12-04 00:00","title":"三分紀錄延續!火箭隊連續19場比賽命中10+三分球","description":"NBA新聞","picUrl":"http://www.51tyw.com/uploads/allimg/161203/1-161203233J3.jpg","url":"http://www.51tyw.com/nba/2417.html"},{"ctime":"2016-12-04 00:00","title":"詹姆斯談三連敗:是時候緊起來了 必須打得男人點","description":"NBA新聞","picUrl":"http://www.51tyw.com/uploads/allimg/161203/1-161203234010.jpg","url":"http://www.51tyw.com/nba/2418.html"},{"ctime":"2016-12-03 12:00","title":"騎士輸贏都靠三分?那還要詹姆斯做什麽?","description":"NBA新聞","picUrl":"http://www.51tyw.com/uploads/allimg/161203/1-161203104344.jpg","url":"http://www.51tyw.com/nba/2407.html"},{"ctime":"2016-12-03 00:00","title":"公牛vs騎士直播看點:詹偉兄弟對決","description":"NBA新聞","picUrl":"http://www.51tyw.com/uploads/allimg/161202/1-161202221205.jpg","url":"http://www.51tyw.com/nba/2395.html"},{"ctime":"2016-12-02 22:00","title":"火箭和勇士聯手創NBA三分紀錄","description":"NBA新聞","picUrl":"http://www.51tyw.com/uploads/allimg/161202/1-161202214212.jpg","url":"http://www.51tyw.com/nba/2392.html"},{"ctime":"2016-12-02 22:00","title":"巴克利:勇士打得像女式籃球,太軟了!","description":"NBA新聞","picUrl":"http://www.51tyw.com/uploads/allimg/161202/1-161202215032.jpg","url":"http://www.51tyw.com/nba/2394.html"},{"ctime":"2016-12-02 20:00","title":"詹姆斯完成月最佳球員4連霸的壯舉!","description":"NBA新聞","picUrl":"http://www.51tyw.com/uploads/allimg/161202/1-1612021PI0.jpg","url":"http://www.51tyw.com/nba/2388.html"},{"ctime":"2016-12-01 00:00","title":"威少再下三雙戰書!詹皇會不會先認慫?","description":"NBA新聞","picUrl":"http://www.51tyw.com/uploads/allimg/161128/1-16112Q13251.jpg","url":"http://www.51tyw.com/nba/2374.html"},{"ctime":"2016-12-01 00:00","title":"騎士這樣的防守想奪冠?也許衛冕只是空談!","description":"NBA新聞","picUrl":"http://www.51tyw.com/uploads/allimg/161130/1-161130222229.jpg","url":"http://www.51tyw.com/nba/2375.html"}]
*/
private int code;
private String msg;
private List<NewslistBean> newslist;
public int getCode() {
return code;
}
public void setCode(int code) {
this.code = code;
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
public List<NewslistBean> getNewslist() {
return newslist;
}
public void setNewslist(List<NewslistBean> newslist) {
this.newslist = newslist;
}
public static class NewslistBean {
/**
* ctime : 2016-12-04 13:00
* title : 格林:理解科爾吸食大麻 不過我從沒吸過
* description : NBA新聞
* picUrl : http://www.51tyw.com/uploads/allimg/161204/1-161204120131.jpg
* url : http://www.51tyw.com/nba/2421.html
*/
private String ctime;
private String title;
private String description;
private String picUrl;
private String url;
public String getCtime() {
return ctime;
}
public void setCtime(String ctime) {
this.ctime = ctime;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getPicUrl() {
return picUrl;
}
public void setPicUrl(String picUrl) {
this.picUrl = picUrl;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
}
}
RecyclerView+Retrofit+OkHttp+RxJava