retrofit|retrofit + rxjava 网络请求展示数据

//依赖 //retrofit加上gson解析 implementation 'com.squareup.retrofit2:converter-gson:2.0.0-beta4' //retrofit 和 rxjava结合 implementation 'com.squareup.retrofit2:adapter-rxjava:2.0.1'//recycleview implementation 'com.android.support:recyclerview-v7:28.0.0' //fresco implementation 'com.facebook.fresco:fresco:0.14.1' //rxjava implementation 'io.reactivex:rxandroid:1.2.1' implementation 'io.reactivex:rxjava:1.1.6'

//注册 package com.example.rikao19; import android.app.Application; import com.facebook.drawee.backends.pipeline.Fresco; /** * date:2018/12/19 * author:111(叮咯呤咚呛) * function: */ public class MyApplication extends Application {@Override public void onCreate() { super.onCreate(); Fresco.initialize(this); } }

//存放接口类 public class Constant { public final static String url="http://www.xieast.com/api/"; }

//创建接口记住是导rx的包package com.example.rikao19; import retrofit2.http.GET; import rx.Observable; /** * date:2018/12/19 * author:111(叮咯呤咚呛) * function: */ public interface MyService { @GET("news/news.php") Observable getCall(); }

//adapter package com.example.rikao19; import android.content.Context; import android.net.Uri; import android.support.annotation.NonNull; import android.support.v7.widget.RecyclerView; import android.view.View; import android.view.ViewGroup; import android.widget.TextView; import com.facebook.drawee.view.SimpleDraweeView; import java.util.ArrayList; import java.util.List; /** * date:2018/12/19 * author:111(叮咯呤咚呛) * function: */ public class MainAdapter extends RecyclerView.Adapter {List list; Context mContext; public MainAdapter(Context context) { mContext = context; list = new ArrayList<>(); }@NonNull @Override public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) { View view = View.inflate(mContext,R.layout.recy_item,null); ViewHolder viewHolder = new ViewHolder(view); return viewHolder; }@Override public void onBindViewHolder(@NonNull ViewHolder viewHolder, int i) { viewHolder.time.setText(list.get(i).getAuthor_name()); viewHolder.sex.setText(list.get(i).getCategory()); viewHolder.mynicheng.setText(list.get(i).getAuthor_name()); viewHolder.myname.setText(list.get(i).getDate()); Uri url = Uri.parse(list.get(i).getThumbnail_pic_s()); viewHolder.simple.setImageURI(url); }@Override public int getItemCount() { return list.size(); }public void setData(List data) { list = data; notifyDataSetChanged(); }public class ViewHolder extends RecyclerView.ViewHolder {SimpleDraweeView simple; TextView myname,mynicheng,sex,time; public ViewHolder(@NonNull View itemView) { super(itemView); simple = itemView.findViewById(R.id.simple); myname = itemView.findViewById(R.id.myname); mynicheng = itemView.findViewById(R.id.mynicheng); sex = itemView.findViewById(R.id.sex); time = itemView.findViewById(R.id.time); } } }

//.java package com.example.rikao19; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.support.v7.widget.LinearLayoutManager; import android.support.v7.widget.RecyclerView; import android.widget.ImageView; import java.util.List; import retrofit2.Retrofit; import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory; import retrofit2.converter.gson.GsonConverterFactory; import rx.Observable; import rx.Observer; import rx.android.schedulers.AndroidSchedulers; import rx.schedulers.Schedulers; public class MainActivity extends AppCompatActivity {private ImageView image; private RecyclerView recy; MainAdapter mAdapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initView(); initData(); }private void initData() { mAdapter = new MainAdapter(this); Retrofit retrofit = new Retrofit.Builder() .baseUrl(Constant.url) .addCallAdapterFactory(RxJavaCallAdapterFactory.create()) .addConverterFactory(GsonConverterFactory.create()) .build(); MyService myService = retrofit.create(MyService.class); Observable call = myService.getCall(); call.subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribe(new Observer() { @Override public void onCompleted() {}@Override public void onError(Throwable e) {}@Override public void onNext(MainBean mainBean) {List data = https://www.it610.com/article/mainBean.getData(); mAdapter.setData(data); recy.setAdapter(mAdapter); recy.setLayoutManager(new LinearLayoutManager(MainActivity.this)); } }); }private void initView() { image = (ImageView) findViewById(R.id.image); recy = (RecyclerView) findViewById(R.id.recy); } }

    推荐阅读