前言
因工作需要,学了点安卓知识。现在已经能写点简单页面,解析数据并展示,但是老是忘记,卡壳。特整理一下并记录要点。
正题
一、所需框架
1.okHttp 网络请求
2.Gson 字符串转Json
3.GsonFormat Json转Bean
4.Glide 加载图片
二、特殊配置
由于我这里用到的是http请求 需要特殊配置
manifest 文件添加配置
//允许网络访问
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
// http请求需额外设置网络安全配置
<application
android:networkSecurityConfig="@xml/net_work_config"
.../>
//新建net_work_config文件 配置明文访问权限
<network-security-config>
<base-config cleartextTrafficPermitted="true" />
</network-security-config>
三、进入正题
- Activity 核心代码
public class HomeActivity extends AppCompatActivity {
private OkHttpClient okHttpClient;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
initData();
}
/*初始化数据*/
private void initData() {
okHttpClient = new OkHttpClient();
//1.创建请求
Request request = new Request.Builder().url("http://v.juhe.cn/toutiao/index?key=160f6a145f32d7c5410a2437923e01ea").build();
Call call = okHttpClient.newCall(request);
//2.异步发送请求
call.enqueue(new Callback() {
@Override
public void onFailure(@NonNull Call call, @NonNull IOException e) {
Log.i("test",e.getMessage());
}
//2.1 数据处理
@Override
public void onResponse(@NonNull Call call, @NonNull Response response) throws IOException {
//2.2 Json反序列化
Gson gson = new Gson();
NewsBean newsBean = gson.fromJson(response.body().string(),NewsBean.class);
//2.3 刷新UI
HomeActivity.this.runOnUiThread(new Runnable() {
@Override
public void run() {
RecyclerView rv = findViewById(R.id.rv);
rv.setLayoutManager(new LinearLayoutManager(HomeActivity.this));
rv.setAdapter(new MyHomeAdapter(newsBean.getResult().getData(),HomeActivity.this));
}
});
}
});
}
}
- Adapter 核心代码
public class MyHomeAdapter extends RecyclerView.Adapter<MyHomeAdapter.MyViewHolder> {
private List<NewsBean.ResultBean.DataBean> dataBeans;
private Context context;
//构造函数
public MyHomeAdapter(List<NewsBean.ResultBean.DataBean> dataBeans, Context context) {
this.dataBeans = dataBeans;
this.context = context;
}
//将home_recycler绑定给MyViewHolder
@NonNull
@Override
public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = View.inflate(context,R.layout.home_recycler,null);
return new MyViewHolder(view);
}
//填充展示数据
@Override
public void onBindViewHolder(@NonNull MyViewHolder holder, @SuppressLint("RecyclerView") int position) {
NewsBean.ResultBean.DataBean dataBean = dataBeans.get(position);
holder.tv_title.setText(dataBean.getTitle());
holder.tv_desc.setText(dataBean.getAuthor_name() + " " +dataBean.getDate());
Glide.with(context).load(dataBean.getThumbnail_pic_s()).into(holder.iv);
//监听点击事件
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(context,"点击了"+position+"行",Toast.LENGTH_LONG).show();
}
});
}
@Override
public int getItemCount() {
return dataBeans.size();
}
//MyViewHolder 处理 将MyViewHolder持有控件 和home_recycler内部控件一一关联
protected class MyViewHolder extends RecyclerView.ViewHolder{
private ImageView iv;
private TextView tv_title;
private TextView tv_desc;
public MyViewHolder(@NonNull View itemView) {
super(itemView);
this.iv = itemView.findViewById(R.id.iv);
this.tv_title = itemView.findViewById(R.id.tv_title);
this.tv_desc = itemView.findViewById(R.id.tv_desc);
}
}
}
- Bean 的处理
通过GsonFormat 生成 Bean代码
四、成品展示