首页 避坑指南

错误解析

MainActivity代码:

public class MainActivity extends AppCompatActivity {
    private Button button;
    private TextView text01;
    Gson gson = new Gson();
    private Handler handler = new Handler(){
        public void handleMessage(Message msg){
            super.handleMessage(msg);
            text01.setText(msg.obj.toString());
        }
    };
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //初始化组件
        button = (Button) this.findViewById(R.id.doClick);
        text01=findViewById(R.id.text01);
        //设置监听器
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new Thread(new MyThread()).start();
            }
        });
    }
    public class MyThread implements Runnable {

        @Override
        public void run() {
            try {

                String urlstr = "https://api.cp91.vip/data/bjpk10/last.json";
                String sread = null;
                StringBuffer buffer = new StringBuffer();
                String string = buffer.toString();
                URL url = new URL(urlstr);

                // 得到connection对象
                HttpURLConnection connection = (HttpURLConnection) url.openConnection();
                //设置请求方式
                connection.setRequestMethod("GET");
                //连接
                connection.connect();
                //得到响应码并进行响应
                if (connection.getResponseCode() == HttpURLConnection.HTTP_OK) {
                    //得到响应
                    InputStream inputStream = connection.getInputStream();
                    //解析响应流
                    BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream, "utf-8"));

                    while ((sread = bufferedReader.readLine()) != null){
                        buffer.append(sread);
                        buffer.append("\r\n");
                    }

                    System.out.println("获取到的json数据:\n" + buffer.toString());

                    string = buffer.toString();

                    if (string != null && string.startsWith("\ufeff")) {
                        string = string.substring(1);
                    }

                    StringBuffer bufferResult = new StringBuffer();

                    List<UserBean> list = gson.fromJson(string, new TypeToken<List<UserBean>>() {
                    }.getType());
                    for (int i = 0; i < list.size(); i++) {
                        UserBean test = list.get(i);
                        bufferResult.append(test.show() + "\n");
                    }

                    string = bufferResult.toString();
                    System.out.println("解析之后数据:\n" + string);
                    Message message = Message.obtain();
                    message.obj=string;
                    handler.sendMessage(message);

                } else {
                    System.out.println("得不到响应");
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}

UserBean代码

package com.example.homework15;

public class UserBean {
    private String issue;
    private String openNum;
    private String openDataTime;

    public String getIssue() {
        return issue;
    }

    public void setIssue(String issue) {
        this.issue = issue;
    }

    public String getOpenNum() {
        return openNum;
    }

    public void setOpenNum(String openNum) {
        this.openNum = openNum;
    }

    public String getOpenDataTime() {
        return openDataTime;
    }

    public void setOpenDataTime(String openDataTime) {
        this.openDataTime = openDataTime;
    }

    public String show() {
        return "      " + getIssue() + "              " + getOpenNum() + "    " + getOpenDataTime();
    }
}

得到的结果为:
image-20200605113517283

分析

经过我长时间百度均找不到解决方案,最后创建了一个新项目重新写了一遍代码,便运行成功了。

将两个项目进行对比,终于发现了问题所在:解析为空的项目中Bean类定义时间的语句为

private String openDataTime;

而使用Postman显示的数据为

image-20200605114521867

因此应该把语句改成

private String openDateTime;

真的是一字之差,天壤之别,所以以后解析数据定义Bean类时,能复制最好直接复制,不要用手敲!!!




文章评论

目录