android - 使用嵌套请求参数改造帖子数据
问题描述
目前我正在改造。我知道带有自定义对象以及普通和多表单数据的发布数据。但是我被卡住了,因为我想使用嵌套的请求参数将数据发布到服务器。
例如,
devicetype": "simulator",
"deviceid": "6ea09052e5b1fd10",
"appversion": "0.1",
"apiversion": "0.1",
我可以将其作为RequestBody
字符串发布,因为我想使用多格式数据发布带有图像的数据。所以我已经清除了这个概念。
但现在我有另一个自定义数据。
"sitedetail": {
"id": "1",
"name": "xxx",
"visitorid": "1"
},
那么我如何将其作为请求参数传递。确实需要传递一个@Body,我们通常将其作为自定义数据或json数据传递。我卡在这里。
更新
{
"devicetype": "simulator",
"deviceid": "6ea09052e5b1fd10",
"appversion": "0.1",
"apiversion": "0.1",
"timezone": "Asia/Kolkata",
"modeltype": "MI A1",
"deviceos": "9.0",
"userdeviceid": "1",
"visitorid": "1",
"siteid": "1",
"visitordetail": {
"id": "1",
"userid": "2",
"name": "xxx",
"email": "xxx@xxx.in",
"mobile": "xxxxxxxxx",
"dealername": "xxx"
},
"sitedetail": {
"id": "1",
"name": "xxx",
"visitorid": "1"
},
"selections": [
{
"id": "1",
"visitorid": "1",
"siteid": "1",
"designno": "xxx",
"qty": "3",
"roomtype": "xxx",
"remarks": "xxx"
},
{
"id": "0",
"visitorid": "1",
"siteid": "1",
"designno": "xxx",
"qty": "3",
"roomtype": "xxx",
"remarks": "xxx"
}
]}
高级帮助将不胜感激!
解决方案
最后我解决了这个问题。
如果有人想将带有额外附加参数的图像上传到服务器。以下是方法。
首先在您的改造 API 请求接口中创建方法。
@Multipart
@POST("test/test.post.json")
Call<APIResponseSiteDetails> addImages(@PartMap Map<String, RequestBody> params);
现在您必须在要调用的活动或片段类中调用此方法。
Map<String, RequestBody> params = new HashMap<>();
if (imageData != null && imageData.size() > 0) {
for (int i = 0; i < imageData.size(); i++) {
String key = "images[" + i + "]";
File file = new File(imageData.get(i).getImageurl());
try {
File compressedImages = new ImageZipper(AddDetailsActivity.this).compressToFile(file);
params.put("" + key + "\"; filename=\"" + file.getName(), getRequestFile(compressedImages));
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
以上代码段仅用于使用MultiPart发送图像。现在,如果你想用它发送额外的参数。然后简单地使用它。
params.put("devicetype", toRequestBody(Constants.DEVICE_TYPE)); // This is String request body.
现在,如果您有另一个使用密钥对值的自定义请求并且想要发送单个项目,那么您必须以这种方式创建。
public Map<String, RequestBody> getAnotherRequest() {
Map<String, RequestBody> paStringRequestBodyMap = new HashMap<>();
paStringRequestBodyMap.put("userid", Functions.toRequestBody("1"));
paStringRequestBodyMap.put("id", Functions.toRequestBody("1"));
return paStringRequestBodyMap;
}
现在您必须将其添加到主请求正文中,然后您必须执行。
for (Map.Entry<String, RequestBody> entry : getAnotherRequest().entrySet()) {
String key = entry.getKey();
RequestBody value = entry.getValue();
params.put("details[" + key + "]", value);
}
现在,如果你想发送多个请求,那么你需要执行下面的代码,
public Map<String, Map> getContacts() {
Map<String, Map> paStringRequestBodyMap = new HashMap<>();
if (showRoomDataList != null && showRoomDataList.size() > 0) {
for (int i = 0; i < showRoomDataList.size(); i++) {
Map<String, RequestBody> innnerList = new HashMap<>();
innnerList.put("id", Functions.toRequestBody("0"));
innnerList.put("visitorid", Functions.toRequestBody("0"));
innnerList.put("siteid", Functions.toRequestBody("0"));
innnerList.put("typeid", Functions.toRequestBody(showRoomDataList.get(i).getTypeid()));
paStringRequestBodyMap.put(i + "", innnerList);
}
}
return paStringRequestBodyMap;
}
现在把它放到主请求中,
for (Map.Entry<String, Map> entry : getContacts().entrySet()) {
String key = entry.getKey();
Map value = entry.getValue();
Iterator it = value.entrySet().iterator();
while (it.hasNext()) {
Map.Entry pair = (Map.Entry) it.next();
RequestBody requestBody = (RequestBody) pair.getValue();
params.put("contacts[" + key + "]" + "[" + pair.getKey() + "]", requestBody);
it.remove(); // avoids a ConcurrentModificationException
}
}
对于图像多部分请求正文
private RequestBody getRequestFile(File file) {
RequestBody fbody = RequestBody.create(MediaType.parse("image/*"), file);
return fbody;
}
对于字符串值请求正文
public static RequestBody toRequestBody(String value) {
RequestBody body = RequestBody.create(MediaType.parse("text/plain"), value);
return body;
}
以上是使用带有图像(多部分)数据的密钥对值执行单个、多个发布请求的代码。我希望这对你们所有人都有帮助。
推荐阅读
- javascript - HTML 表格样式上一个兄弟
- java - Oauth2 保护资源
- c++ - OpenGL 光照位置固定在不想要的位置
- python - Redshift Python API 用于查询 SQL 过程列表
- spring-boot - Spring JPA Native Query没有给出结果集
- java - 将 KeyStore 和 Cipher 用于用户控制的程序
- r - R - 带加权词的字符串距离
- javascript - 将第三方 js 文件导入 angular typescript 项目
- calculated-columns - Spotfire - 按列分区和连续分区中的行数
- firebase - AngularFireStorageModule 未找到