public class MainActivity extends AppCompatActivity { private String url = "url地址"; private RecyclerView recyclerView; private RecyclerAdapter adapter; private List<bean类> list = new ArrayList<>(); private Handler handler = new Handler() { @Override public void handleMessage(Message msg) { String s = (String) msg.obj; Gson gson = new Gson(); ViewBean bean = gson.fromJson(s, ViewBean.class); list.addAll(bean.list); adapter.notifyDataSetChanged(); Log.e("-----------------", "---" + list.size()); adapter.setOnItemClickLitener(new RecyclerAdapter.OnItemClickLitener() { @Override public void onItemClick(View view, int position) { Toast.makeText(MainActivity.this, list.get(position), Toast.LENGTH_SHORT).show(); } }); } }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); recyclerView = (RecyclerView) findViewById(R.id.recycle); boolean conn = Connutlis.isConn(this); //判断网络是否连接成功 if(!conn){ //没有网络 Connutlis.setNetworkMethod(this); loadData(); }else { loadData(); } //设置recycler管理器
okhttp请求+recycleview+网络是否连接成功
最新推荐文章于 2022-11-16 10:12:02 发布
本文展示了如何在Android应用中使用OkHttp发送网络请求获取数据,并将其加载到RecyclerView中。同时,实现了一个网络连接状态检测功能,当网络不可用时,提示用户并引导设置网络。
摘要由CSDN通过智能技术生成