1、内容如果有不对的,希望可以指出或补充。
2、任务练习。
1 分析
总体要求:访问当前时间接口,将信息显示到app页面里面(自行设计)。接口的地址:http://poetry.apiopen.top/poetryFull?count=2&page=1
JSON数据分析:接口地址对应的数据是有数据头的复杂数组数据。
JSON数据结构的字段↓
JSON数据数组内容的字段↓
2 文件
根据 JSON 建立的Java类 ,这部分是返回对应的所有字段。
3 依赖
1 布局
activity_main.xml
<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
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解析 - 从简单数据到复杂数据