简体   繁体   中英

Retrofit 2 POST JSON to ThingSpeak Channel

I am new to Android and developing an app that will send data periodically to a ThingSpeak Channel.
The data will be a list of objects of type Person. I want to send them under JSON format, thus I'm using Retrofit 2 with POST function for this purpose.
Let say I have a channel number 123456 with api_key APIKEY123 and ThingSpeak api: api.thingspeak.com . And my POST function looks like this:

POST(URL_STRING)
Call<List<Person>> postData(para1, para2);


May I ask what should I put in URL_STRING, para1 and para2? What's the type of encoding tag should I use? Thank you in advance.

have you seen this yet? https://www.mathworks.com/help/thingspeak/rest-api.html .

you will need to replace Person, para1, para2 with what you like to do.

For example - "update a channel" https://www.mathworks.com/help/thingspeak/update-a-channel.html

you will replace para1,para2 with api_key and name.

and Person will be replaced with the Channel class containing the json object

{
  "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 will give you an idea on how to implement the service interface.

edit
the following will have what you need. read up Implementing Retrofit2 service interface on how to use it.

Syntax for service interface

interface InterfaceTS {

    @GET
    Call<Channel> updateChannel(
            @Query("api_key") String key,
            @Query("name") String name);

}

Syntax for Channel:

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;
    }

}

Syntax for API Key

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;
    }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM