OKhttp
来源:互联网 发布:s7200ppi编程电缆驱动 编辑:程序博客网 时间:2024/06/15 03:13
加依赖
recyclerview依赖:
compile 'com.android.support:recyclerview-v7:26.0.0-alpha1'okhttp依赖:
compile 'com.squareup.okhttp3:okhttp:3.9.0'gson依赖:
compile 'com.google.code.gson:gson:2.8.1'glide:依赖
compile 'com.github.bumptech.glide:glide:3.7.0'
先加权限
<uses-permission android:name="android.permission.INTERNET"/> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
主布局
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.bwei.com.day.MainActivity"> <android.support.v7.widget.RecyclerView android:id="@+id/recycler" android:layout_width="match_parent" android:layout_height="match_parent"></android.support.v7.widget.RecyclerView></RelativeLayout>main类public class MainActivity extends AppCompatActivity { RecyclerView recycler; List<Bean.DataBean.WallpaperListInfoBean>list = new ArrayList<>(); String path = "http://bz.budejie.com/?typeid=2&ver=3.4.3&no_cry=1&client=android&c=wallPaper&a=random&bigid=0"; Handler handler = new Handler(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //初始化控件 initData(); initView(); } private void initView() { recycler = (RecyclerView) findViewById(R.id.recycler); LinearLayoutManager manager = new LinearLayoutManager(this); GridLayoutManager grid = new GridLayoutManager(this,3); //瀑布流 StaggeredGridLayoutManager stge= new StaggeredGridLayoutManager(2,StaggeredGridLayoutManager.VERTICAL); recycler.setLayoutManager(manager); } private void initData() { OkHttpClient client = new OkHttpClient(); Request request = new Request.Builder() .url(path) .build(); Call call = client.newCall(request); call.enqueue(new Callback() { @Override public void onFailure(Call call, IOException e) { Log.d("onFailure", "请求失败"); } @Override public void onResponse(Call call, Response response) throws IOException { String result = response.body().string(); Bean bean = Bean.objectFromData(result); list = bean.data.WallpaperListInfo; handler.post(new Runnable() { @Override public void run() { recycler.setAdapter(new MyAdapter(MainActivity.this,list)); } }); } }); }}
适配器public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder>{ Context context; List<Bean.DataBean.WallpaperListInfoBean>list = new ArrayList<>(); public MyAdapter(Context context, List<Bean.DataBean.WallpaperListInfoBean> list) { this.context = context; this.list = list; } @Override public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { View view = View.inflate(context,R.layout.item,null); return new MyAdapter.ViewHolder(view); } @Override public void onBindViewHolder(ViewHolder holder, int position) { holder.tv.setText(list.get(position).UserName); Glide.with(context).load(list.get(position).WallPaperMiddle).into(holder.img); } @Override public int getItemCount() { return list==null ? 0 : list.size(); } protected class ViewHolder extends RecyclerView.ViewHolder { TextView tv; ImageView img; public ViewHolder(View itemView) { super(itemView); tv = itemView.findViewById(R.id.tv); img = itemView.findViewById(R.id.img); } }}bean类根据自己的解析来写!
阅读全文
1 0
- OkHttp
- okhttp
- OkHttp
- OkHttp
- OkHttp
- okhttp
- OKHTTP
- okhttp
- Okhttp
- okHttp
- OkHttp
- okhttp
- Okhttp
- okhttp
- OkHttp
- okhttp
- okhttp
- okhttp
- ajax跨域问题的解决办法
- Spring MVC之@RequestParam @RequestBody @RequestHeader 等详解
- mysql常用SQL语句
- 项目名后有[]的情况解决
- mat初始化出现乱码
- OKhttp
- 大型互联网b2b b2c o2o电子商务云平台
- 避免多次点击PerfectClickListener
- python使用ip代理池,测试ip是否有用
- 自定义一个List排序规则,然后对另一个List按照该规则排序
- sslsocket实例
- 51nod 1562玻璃切割(降维,反向处理)
- Spring Cloud还是Dubbo
- VMware如何监测性能问题