在我的应用程序中,我想从我的应用程序向服务器发送 json 数据,对于服务器请求,我使用“logs”:[ { “post_id”:“3”, “log”:[{“timer”:2,“last”:19,“ping”:1}, {“timer”:6,“last”:3,“ping”:5}, {“timer”:9,“last”:11,“ping”:7}, ] },
{ “post_id”:“6”, “log”:[{“timer”:9,“last”:3,“ping”:13}] },
{ “post_id”:“20”, “log”:[{“timer”:10,“last”:1,“ping”:5},{“timer”:4,“last”:2,“ping”:9}, {“timer”:9,“last”:4,“ping”:6}, {“timer”:3,“last”:19,“ping”:9}, ] },]
库。
我应使用以下 format 格式发送数据:
button
当用户在Room
上点击时,我会得到以下数据:计时器-最后- ping
用于将上述数据存储到设备中,我使用room
库。
我可以将此数据(计时器-最后- ping )存储到@Entity(tableName = Constants.SOCKET_PING_DATABASE_TABLE)
public class SocketPingEntity {
@PrimaryKey()
@NonNull
@ColumnInfo(name = Constants.SOCKET_PING_DATABASE_PING_TIME)
private int ping;
@ColumnInfo(name = Constants.SOCKET_PING_DATABASE_CLICK_TIME)
private int timer;
@ColumnInfo(name = Constants.SOCKET_PING_DATABASE_LAST_PING)
private Long last;
public int getPing() {
return ping;
}
public void setPing(int ping) {
this.ping = ping;
}
public int getTimer() {
return timer;
}
public void setTimer(int timer) {
this.timer = timer;
}
public Long getLast() {
return last;
}
public void setLast_ping(Long last) {
this.last = last;
}
}
中,但是我不知道如何我可以根据 post_id 来连接这些数据吗?
然后最终将上述数据转换为 json 格式并发送到服务器。
房间实体类:
@Insert(onConflict = OnConflictStrategy.REPLACE)
void addPingInfo(SocketPingEntity pingEntity);
Room Dao类用于存储数据:
SocketPingEntity entity = new SocketPingEntity();
entity.setPing((int) socketPing);
entity.setClick_time((int) remainTime);
entity.setLast_ping(lastPing);
socketPingDatabase.socketPingDao().addPingInfo(entity);
使用此代码将数据添加到房间中:
pingEntityList.addAll(socketPingDatabase.socketPingDao().getSocketPingEntityList());
SocketPingBodySendData pingBodySendData = new SocketPingBodySendData();
pingBodySendData.setAuction_id(auctionID);
pingBodySendData.setData(new Gson().toJsonTree(pingEntityList));
Log.i("socketSendDataLog", "\n Json : " + new Gson().toJsonTree(pingEntityList));
Call<SocketPingResponse> pingResponseCall = apis.getSocketPingLog(jwtToken, pingBodySendData);
pingResponseCall.enqueue(new Callback<SocketPingResponse>() {
@Override
public void onResponse(Call<SocketPingResponse> call, Response<SocketPingResponse> response) {
if (response.body() != null) {
Log.e("socketLogResponse", "Response : " + response.body().getStatus());
if (response.body().getStatus().equals("ok")) {
isSendSocketPingData = true;
pingEntityList.clear();
socketPingDatabase.socketPingDao().deleteAll();
}
}
}
@Override
public void onFailure(Call<SocketPingResponse> call, Throwable t) {
Log.e("socketLogResponse", "Error : " + t.toString());
}
});
使用此代码将数据设置为json格式的服务器:
@POST("api/user_log")
Call<SocketPingResponse> getSocketPingLog(@Body SocketPingBodySendData socketPingBodySendData);
呼叫api代码:
public class SocketPingBodySendData {
@SerializedName("auction_id")
@Expose
int auction_id;
@SerializedName("data")
@Expose
JsonElement data;
public int getAuction_id() {
return auction_id;
}
public void setAuction_id(int auction_id) {
this.auction_id = auction_id;
}
public JsonElement getData() {
return data;
}
public void setData(JsonElement data) {
this.data = data;
}
}
SocketPingBodySendData类代码:
{“log”:[{“clickTime”:34,“pingTime”:“160"},{“clickTime”:43,“pingTime”:“310”},{“clickTime”:59,“pingTime”:“380"}]}
使用上述代码时,发送诸如以下的数据:
“logs”:[ { “post_id”:“3”, “log”:[{“timer”:2,“last”:19,“ping”:1}, {“timer”:6,“last”:3,“ping”:5}, {“timer”:9,“last”:11,“ping”:7}, ] },
{ “post_id”:“6”, “log”:[{“timer”:9,“last”:3,“ping”:13}] },
{ “post_id”:“20”, “log”:[{“timer”:10,“last”:1,“ping”:5},{“timer”:4,“last”:2,“ping”:9}, {“timer”:9,“last”:4,“ping”:6}, {“timer”:3,“last”:19,“ping”:9}, ] },]
但是我要发送这样的消息:
PS > install-package gudusoft.gsqlparser -source https://www.nuget.org/api/v2
我怎么办?