我试图根据Wordpress RestAPI的json数据在MainActivity上创建一个简单表。我已经能够获得“ title”和“ id”。但是我在构造表时遇到问题。
json数据看起来很幸运:
{
"id": 807,
"date": "2018-08-18T19:06:52",
"date_gmt": "2018-08-18T13:36:52",
"guid": {
"rendered": "http://wordpresssite.com/?page_id=807"
},
"modified": "2019-04-27T14:32:50",
"modified_gmt": "2019-04-27T09:02:50",
"slug": "sample-post",
"status": "publish",
"type": "page",
"link": "http://wordpresssite.com/sample-page/",
"title": {
"rendered": "Test results"
},
"content": {
"rendered": "<div class=\"entry\">\n<p>Check out <strong>Current data</strong> & <strong>Previous Data</strong> here. Among the others, <strong>data</strong> usually is declared first. This page is entirely dedicated to update you with <strong>test data</strong>.</p>\n</script></p>\n<p> </p>\n<h2 style=\"text-align: center;\">data</h2>\n\n<table id=\"tablepress-5\" class=\"tablepress tablepress-id-5\">\n<thead>\n<tr class=\"row-1 odd\">\n\t<th class=\"column-1\">Date</th><th class=\"column-2\">First Round</th><th class=\"column-3\">Second Round</th>\n</tr>\n</thead>\n<tbody class=\"row-hover\">\n<tr class=\"row-2 even\">\n\t<td class=\"column-1\">09/05/19</td><td class=\"column-2\">98</td><td class=\"column-3\">21</td>\n</tr>\n<tr class=\"row-3 odd\">\n\t<td class=\"column-1\">08/05/19</td><td class=\"column-2\">32</td><td class=\"column-3\">91</td>\n</tr>\n<tr class=\"row-4 even\">\n\t<td class=\"column-1\">07/05/19</td><td class=\"column-2\">74</td><td class=\"column-3\">87</td>\n</tr>\n<tr class=\"row-5 odd\">\n\t<td class=\"column-1\">05/05/19</td><td class=\"column-2\">20</td><td class=\"column-3\">27</td>\n</tr>\n<tr class=\"row-6 even\">\n\t<td
这是我做标题的方法:
StringRequest request = new StringRequest(Request.Method.GET, url, new Response.Listener<String>() {
@Override
public void onResponse(String s) {
gson = new Gson();
list = (List) gson.fromJson(s, List.class);
postTitle = new String[list.size()];
for(int i=0;i<list.size();++i){
mapPost = (Map<String,Object>)list.get(i);
mapTitle = (Map<String, Object>) mapPost.get("title");
postTitle[i] = (String) mapTitle.get("rendered");
}
postList.setAdapter(new ArrayAdapter(MainActivity.this,android.R.layout.simple_list_item_1,postTitle));
}
},
答案 0 :(得分:1)
您可以按照以下方法设计表格视图。您可以根据json数据动态创建视图。
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:stretchColumns="0,1,2"
android:gravity="center">
<TableRow
android:background="#FFFFFF"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_margin="1dp"
android:layout_weight="1"
>
<TableRow
android:background="#000000"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_margin="1dp"
android:layout_weight="1"
>
</TableRow>
</TableRow>
<TableRow
android:background="#000000"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_margin="1dp"
android:layout_weight="1"
>
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text=" Date "
android:layout_margin="1dp"
android:layout_column="0"
android:background="#FFFFFF"
android:textStyle="bold"
android:gravity="center"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Miles "
android:layout_margin="1dp"
android:layout_column="1"
android:background="#FFFFFF"
android:gravity="center"
android:textStyle="bold"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Calories"
android:layout_margin="1dp"
android:background="#FFFFFF"
android:gravity="center"
android:textStyle="bold"
android:layout_column="2"
/>
</TableRow>
<TableRow
android:background="#000000"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_margin="1dp"
android:layout_weight="1"
>
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text=" Text"
android:layout_margin="1dp"
android:layout_column="0"
android:background="#FFFFFF"
android:gravity="center"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text=" Text"
android:layout_margin="1dp"
android:layout_column="1"
android:background="#FFFFFF"
android:gravity="center"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text=" Text"
android:layout_margin="1dp"
android:background="#FFFFFF"
android:gravity="center"
android:layout_column="2" />
</TableRow>
</TableLayout>
答案 1 :(得分:1)
我认为您可以使用JSOUP库来解析您的html内容。
答案 2 :(得分:0)
我希望下面的内容可以作为您的输入内容:
{
"id": 807,
"date": "2018-08-18T19:06:52",
"date_gmt": "2018-08-18T13:36:52",
"guid": {
"rendered": "http://wordpresssite.com/?page_id=807"
},
"modified": "2019-04-27T14:32:50",
"modified_gmt": "2019-04-27T09:02:50",
"slug": "sample-post",
"status": "publish",
"type": "page",
"link": "http://wordpresssite.com/sample-page/",
"title": {
"rendered": "Test results"
},
"content": {
"rendered": "your content...."
}
}
现在您可以通过以下方法解析此数据:
1)在您的gradel文件中添加依赖项
dependencies {
implementation 'com.google.code.gson:gson:2.8.5'
}
2)创建pojo类:
-----------------------------------com.example.Content.java-----------
package com.example;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
public class Content {
@SerializedName("rendered")
@Expose
private String rendered;
public String getRendered() {
return rendered;
}
public void setRendered(String rendered) {
this.rendered = rendered;
}
}
-----------------------------------com.example.Example.java-------------------------
package com.example;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
public class Example {
@SerializedName("id")
@Expose
private Integer id;
@SerializedName("date")
@Expose
private String date;
@SerializedName("date_gmt")
@Expose
private String dateGmt;
@SerializedName("guid")
@Expose
private Guid guid;
@SerializedName("modified")
@Expose
private String modified;
@SerializedName("modified_gmt")
@Expose
private String modifiedGmt;
@SerializedName("slug")
@Expose
private String slug;
@SerializedName("status")
@Expose
private String status;
@SerializedName("type")
@Expose
private String type;
@SerializedName("link")
@Expose
private String link;
@SerializedName("title")
@Expose
private Title title;
@SerializedName("content")
@Expose
private Content content;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
public String getDateGmt() {
return dateGmt;
}
public void setDateGmt(String dateGmt) {
this.dateGmt = dateGmt;
}
public Guid getGuid() {
return guid;
}
public void setGuid(Guid guid) {
this.guid = guid;
}
public String getModified() {
return modified;
}
public void setModified(String modified) {
this.modified = modified;
}
public String getModifiedGmt() {
return modifiedGmt;
}
public void setModifiedGmt(String modifiedGmt) {
this.modifiedGmt = modifiedGmt;
}
public String getSlug() {
return slug;
}
public void setSlug(String slug) {
this.slug = slug;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getLink() {
return link;
}
public void setLink(String link) {
this.link = link;
}
public Title getTitle() {
return title;
}
public void setTitle(Title title) {
this.title = title;
}
public Content getContent() {
return content;
}
public void setContent(Content content) {
this.content = content;
}
}
--------------com.example.Guid.java-----------------------------------
package com.example;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
public class Guid {
@SerializedName("rendered")
@Expose
private String rendered;
public String getRendered() {
return rendered;
}
public void setRendered(String rendered) {
this.rendered = rendered;
}
}
-----------------------------------com.example.Title.java-----------------------------------
package com.example;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
public class Title {
@SerializedName("rendered")
@Expose
private String rendered;
public String getRendered() {
return rendered;
}
public void setRendered(String rendered) {
this.rendered = rendered;
}
}
3)使用Gson库解析数据并获得结果:
Gson gson = new Gson();
String stringToParse =""; //string you want to parse.
Example response = gson.fromJson(stringToParse , Example.class);
Content content = response.getContent();
希望这对您有所帮助。