我是Android新手并开发了一款定期向ThingSpeak频道发送数据的应用。
数据将是Person类型的对象列表。我想以JSON格式发送它们,因此我正在使用具有POST功能的Retrofit 2来实现此目的。
假设我有一个频道号为123456的api_key APIKEY123和ThingSpeak api:api.thingspeak.com。我的POST功能如下所示:
POST(URL_STRING)
Call<List<Person>> postData(para1, para2);
请问我应该在URL_STRING,para1和para2中添加什么?我应该使用什么类型的编码标签?提前谢谢。
答案 0 :(得分:0)
您需要将Person,para1,para2替换为您喜欢的内容。
例如 - “更新频道”https://www.mathworks.com/help/thingspeak/update-a-channel.html
你将用api_key和name替换para1,para2。
和Person将替换为包含json对象的Channel类
{
"id": 4,
"name": "Updated Channel",
"description": null,
"metadata": null,
"latitude": null,
"longitude": null,
"created_at": "2014-03-25T13:12:50-04:00",
"elevation": null,
"last_entry_id": null,
"ranking": 15,
"username": "hans",
"tags": [],
"api_keys":
[
{
"api_key": "XXXXXXXXXXXXXXXX",
"write_flag": true
}
]
}
Implementing Retrofit2 service interface将为您提供有关如何实施服务界面的建议。
修改强>
以下将满足您的需求。阅读Implementing Retrofit2 service interface了解如何使用它。
服务接口的语法
interface InterfaceTS {
@GET
Call<Channel> updateChannel(
@Query("api_key") String key,
@Query("name") String name);
}
频道语法:
package com.example;
import java.util.List;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
public class Channel {
@SerializedName("id")
@Expose
private Integer id;
@SerializedName("name")
@Expose
private String name;
@SerializedName("description")
@Expose
private Object description;
@SerializedName("metadata")
@Expose
private Object metadata;
@SerializedName("latitude")
@Expose
private Object latitude;
@SerializedName("longitude")
@Expose
private Object longitude;
@SerializedName("created_at")
@Expose
private String createdAt;
@SerializedName("elevation")
@Expose
private Object elevation;
@SerializedName("last_entry_id")
@Expose
private Object lastEntryId;
@SerializedName("ranking")
@Expose
private Integer ranking;
@SerializedName("username")
@Expose
private String username;
@SerializedName("tags")
@Expose
private List<Object> tags = null;
@SerializedName("api_keys")
@Expose
private List<ApiKey> apiKeys = null;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Object getDescription() {
return description;
}
public void setDescription(Object description) {
this.description = description;
}
public Object getMetadata() {
return metadata;
}
public void setMetadata(Object metadata) {
this.metadata = metadata;
}
public Object getLatitude() {
return latitude;
}
public void setLatitude(Object latitude) {
this.latitude = latitude;
}
public Object getLongitude() {
return longitude;
}
public void setLongitude(Object longitude) {
this.longitude = longitude;
}
public String getCreatedAt() {
return createdAt;
}
public void setCreatedAt(String createdAt) {
this.createdAt = createdAt;
}
public Object getElevation() {
return elevation;
}
public void setElevation(Object elevation) {
this.elevation = elevation;
}
public Object getLastEntryId() {
return lastEntryId;
}
public void setLastEntryId(Object lastEntryId) {
this.lastEntryId = lastEntryId;
}
public Integer getRanking() {
return ranking;
}
public void setRanking(Integer ranking) {
this.ranking = ranking;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public List<Object> getTags() {
return tags;
}
public void setTags(List<Object> tags) {
this.tags = tags;
}
public List<ApiKey> getApiKeys() {
return apiKeys;
}
public void setApiKeys(List<ApiKey> apiKeys) {
this.apiKeys = apiKeys;
}
}
API密钥的语法
package com.example;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
public class ApiKey {
@SerializedName("api_key")
@Expose
private String apiKey;
@SerializedName("write_flag")
@Expose
private Boolean writeFlag;
public String getApiKey() {
return apiKey;
}
public void setApiKey(String apiKey) {
this.apiKey = apiKey;
}
public Boolean getWriteFlag() {
return writeFlag;
}
public void setWriteFlag(Boolean writeFlag) {
this.writeFlag = writeFlag;
}
}