从服务器接收的json数据,gson解析生成bean对象,怎么让页面显示出来
问题描述:
1.我在mainactivity建了一个空的内部类
Response response = okHttpClient.newCall (request).execute ();
String date = response.body ().string ();
Gson gson = new Gson ();
F_User f_user = gson.fromJson (date, F_User.class);
```
这样是不是就能创建,并且给F_User这个类的对象赋值
2.接着怎么显示在页面不知道咋做,
``` if (date != null) {
Gson gson = new Gson ();
F_User f_user = gson.fromJson (date, F_User.class);
//把解析好的数据发送到页面
TextView tv_id = findViewById (R.id.tv1);
tv_id.setText(f_user.getF_id ());
TextView tv_name = findViewById (R.id.tv2);
tv_name.setText(f_user.getF_username ());
TextView tv_group = findViewById (R.id.tv3);
tv_group.setText(f_user.getF_group ());
TextView tv_role = findViewById (R.id.tv4);
tv_role.setText(f_user.getF_role ());
TextView tv_role_index = findViewById (R.id.tv5);
tv_role_index.setText(f_user.getF_role_index ());
TextView tv_passcode = findViewById (R.id.tv6);
tv_passcode.setText(f_user.getF_passcode ());
//加入message
Message message = new Message ();
message.what =1;
handler.sendMessage (message);
public Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
case 1://_这个位置写的对吗_
*_ BaseAdapter adapter=new BaseAdapter () {
}
(this,R.layout.activity_main);
.setAdapter();
break_;*
}
```是这个样子就可以了吗?
3.下面是我copy的模板地址
[引用地址](https://blog.****.net/qq_26650589/article/details/70256219 "A")
因为数据格式变了,导致项目解析数据之后的步骤不知道还需要不,想用ListView接,因为刚接触安卓,拖控件也拖不明白,辛苦大神们了。
答
这个就好比画图纸,有人来给你讲了这里面需要画什么,需要注意什么,什么逻辑,
然后你会根据这些内容并将其呈现在图纸中。
现在你已经做好了数据接收以及呈现的内容,name你需要去绘制需要呈现的ui界面,然后将这些数据填充到界面上各个控件里就好了,例如
TextView tv_name = findViewById(R.id.tvame);
tv_name.setText(f_user.name);
答
直接获取bean就行了
答
假设你的F_User里面有一个name属性,页面有一个TextView,接下来就是
TextView tv_name = findViewById(R.id.tvame);
tv_name.setText(f_user.name);
答
页面显示就是,你用bean去获取就可以,比如f_user.getName f_user.getId
答
在activity的oncreate方法中加入了一行效果拔群的代码,就跳过了线程不规范引起的问题。
可以获取到参数,并且显示出来了,虽然直接textview显示效果不好,可能和显示屏适配有关系。
StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
.detectDiskReads().detectDiskWrites().detectNetwork()
.penaltyLog().build());
StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
.detectLeakedSqlLiteObjects().detectLeakedClosableObjects()
.penaltyLog().penaltyDeath().build());