目錄
- 寫在前面的話
- 一、步驟展示
- (一)準備
- (二)具體實施
- 二、效果展示
- 三、補充
寫在前面的話
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決議 - 從簡單資料到復雜資料
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/255924.html
標籤:其他
上一篇:Android 系統編譯技巧
