安卓基础练习 05 |Volley+Gson综合应用

写在前面的话

1、内容如果有不对的,希望可以指出或补充。
2、任务练习。

一、步骤展示

(一)准备

1 分析

总体要求:访问当前时间接口,将信息显示到app页面里面(自行设计)。接口的地址:http://poetry.apiopen.top/poetryFull?count=2&page=1

JSON数据分析:接口地址对应的数据是有数据头的复杂数组数据。

JSON数据结构的字段↓
在这里插入图片描述
JSON数据数组内容的字段↓
在这里插入图片描述
2 文件

根据 JSON 建立的Java类 ,这部分是返回对应的所有字段。

3 依赖

项目对应的 build.gradle 文件
在这里插入图片描述
清单文件
在这里插入图片描述

(二)具体实施

1 布局

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:padding="10dp"
    android:background="@mipmap/bg">
    <!--按钮部分-->
    <Button
        android:id="@+id/btn"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:onClick="start"
        android:text="开始解析"
        android:textSize="18sp"
        android:backgroundTint="#400000FF"/>
    <!--展示部分-->
    <TextView
        android:id="@+id/tv_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="解析成功:"
        android:textSize="15sp"
        android:visibility="invisible"/>
    <!--ScrollView(滚动条,竖直滚动条)-->
    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
        <TextView
            android:id="@+id/tv_content"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="20sp"
            android:textColor="@color/white"
            android:background="#400000FF"/>
    </ScrollView>

</LinearLayout>

2 Java类

Result.java

package com.example.task05;

import java.util.List;

public class Result {
    //json结构
    private String code;
    private String message;
    private List<ResultBean> result;

    public class ResultBean{
        //json的内容
        private String title;
        private String dynasty;
        private String writer;
        private String content;
        private String type;
        private String remark;
        private String appreciation;

        public ResultBean(String title, String dynasty, String writer, String content, String type, String remark, String appreciation) {
            this.title = title;
            this.dynasty = dynasty;
            this.writer = writer;
            this.content = content;
            this.type = type;
            this.remark = remark;
            this.appreciation = appreciation;
        }

        public ResultBean(){

        }

        public String getTitle() {
            return title;
        }

        public void setTitle(String title) {
            this.title = title;
        }

        public String getDynasty() {
            return dynasty;
        }

        public void setDynasty(String dynasty) {
            this.dynasty = dynasty;
        }

        public String getWriter() {
            return writer;
        }

        public void setWriter(String writer) {
            this.writer = writer;
        }

        public String getContent() {
            return content;
        }

        public void setContent(String content) {
            this.content = content;
        }

        public String getType() {
            return type;
        }

        public void setType(String type) {
            this.type = type;
        }

        public String getRemark() {
            return remark;
        }

        public void setRemark(String remark) {
            this.remark = remark;
        }

        public String getAppreciation() {
            return appreciation;
        }

        public void setAppreciation(String appreciation) {
            this.appreciation = appreciation;
        }

        @Override
        //显示形式
        public String toString() {
            return  " 诗歌名称→" + title +"\n"+
                    " 朝代→" + dynasty +"\n"+
                    " 作者→" + writer +"\n"+
                    " 内容→" + content +"\n"+
                    " 派别→" + type +"\n"+
                    " 注释→" + remark +"\n"+
                    " 解析→" + appreciation +"\n\n";
        }
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public List<ResultBean> getResult() {
        return result;
    }

    public void setResult(List<ResultBean> result) {
        this.result = result;
    }
}

3 主要实现方法

MainActivity.java

package com.example.task05;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.google.gson.Gson;

import java.util.List;

public class MainActivity extends AppCompatActivity {
    private TextView tvTitle,tvContent;
    private String content = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //获取控件
        tvTitle = findViewById(R.id.tv_title);
        tvContent = findViewById(R.id.tv_content);
    }

    public void start(View view){
        if (content == null){
            Toast.makeText(this, "解析成功,再点一次进行清空", Toast.LENGTH_SHORT).show();
            tvTitle.setVisibility(View.VISIBLE);//显示可见

            getDatas();//调用方法
        }else{
            tvTitle.setVisibility(View.INVISIBLE);//隐藏
            // 清空内容
            tvContent.setText("");
            content = null;
        }
    }

    private void getDatas(){
        //获取到RequestQueue对象
        RequestQueue requestQueue = Volley.newRequestQueue(this);
        //json数据地址
        String jsonDataUrl = "http://poetry.apiopen.top/poetryFull?count=2&page=1";
        //数据请求
        StringRequest stringRequest = new StringRequest(jsonDataUrl,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        Log.i("成功提示", "成功获取到json数据");
                        //1获取到json数据
                        content = response;
                        //2解析json数据
                        Gson gson = new Gson();//Gson对象
                        Result result = gson.fromJson(content,Result.class);
                        List<Result.ResultBean> resultList = result.getResult();//对象中拿到集合
                        //3转为字符串
                        String resultData = resultList.toString();
                        //4去掉[]
                        String resultData2 = resultData.substring(0,resultData.length() - 1);//尾部
                        String resultData3 = resultData2.substring(1);//头部
                        //5展示数据
                        tvContent.setText(resultData3);
                    }
                },new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError e) {
                        Log.i("错误提示", String.valueOf(e));
                    }
                });
        requestQueue.add(stringRequest);//添加请求
    }
}

二、效果展示

运行结果如下。

三、补充

1、ScrollView(滚动条)

2、Android:Gson解析 - 从简单数据到复杂数据

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值