柱体改造 2 包括大括号
Posted
技术标签:
【中文标题】柱体改造 2 包括大括号【英文标题】:Post body retrofit2 include curly brackets 【发布时间】:2019-11-13 11:34:50 【问题描述】:我想在改造中发布带有正文的请求,其中括号还有另一个括号
期待正文请求:"attributes":"data":"FOOBAR","deviceId":171,"type":"custom"
我尝试过使用反斜杠,但总是收到错误的请求。
Activity.java
StringBuilder stringBuilder = new StringBuilder("\"data\":\"");
stringBuilder.append(commandInput.getText());
stringBuilder.append("\"");
Tasker task = new Tasker(idUnit, "custom", stringBuilder.toString());
Call<Tasker> call2 = mApiService.postCommand(task);
call2.enqueue(new Callback<Tasker>()
@Override
public void onResponse(Call<Tasker> call, Response<Tasker> response)
Toast.makeText(CommandActivity.this, getString(R.string.command_sent), Toast.LENGTH_SHORT).show();
@Override
public void onFailure(Call<Tasker> call, Throwable t)
Toast.makeText(CommandActivity.this, getString(R.string.command_failed), Toast.LENGTH_SHORT).show();
);
Tasker.java
public class Tasker
private long deviceId;
private String type;
private String attributes;
public Tasker(long deviceId, String type, String attributes)
this.deviceId = deviceId;
this.type = type;
this.attributes = attributes;
预期结果负载:
"attributes":"data":"FOOBAR","deviceId":171,"type":"custom"
实际输出载荷:
"attributes":"\"data\":\"FOOBAR\"","deviceId":171,"type":"custom"
【问题讨论】:
commandInput.getText()
是什么?看来你放的斜线太多了。
【参考方案1】:
您需要一些类或 Map 对象来保存属性,而不是 String attributes
另外,尝试使用 JsonObject 类,而不仅仅是 StringBuilder
【讨论】:
以上是关于柱体改造 2 包括大括号的主要内容,如果未能解决你的问题,请参考以下文章