Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/193.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 使用嵌套的请求参数改装post数据_Android_Retrofit2 - Fatal编程技术网

Android 使用嵌套的请求参数改装post数据

Android 使用嵌套的请求参数改装post数据,android,retrofit2,Android,Retrofit2,目前我正在进行改装。我了解自定义对象的post数据以及普通和多表单数据。但我被卡住了,因为我想用嵌套的请求参数将数据发布到服务器 例如 devicetype": "simulator", "deviceid": "6ea09052e5b1fd10", "appversion": "0.1", "apiversion": "0.1", 我可以将其作为RequestBody字符串发布,因为我希望使用多种形式的数据发布带有图像的数据。所以我澄清了这个概念 但现在我有了另一个自定义数据 "site

目前我正在进行改装。我了解自定义对象的post数据以及普通和多表单数据。但我被卡住了,因为我想用嵌套的请求参数将数据发布到服务器

例如

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"
    }
]}

先进的帮助将不胜感激

我认为您应该将json作为请求主体发送。为此,需要创建一个对应json的pojo。我们把它叫做,
ReqBody

// Add header items if required
@POST("endpoint")
Call<ResBody> getData(@Body ReqBody body);
//如果需要,添加标题项
@POST(“端点”)
调用getData(@Body ReqBody Body);

另外,将
.addConverterFactory(GsonConverterFactory.create())
添加到您的
改装.Builder()

我想您遇到的问题与我几天前遇到的问题相同。您可以在此处找到整个主题以查看我的示例:

你必须这样做:

public interface API {

  @POST("YOUR_ENDPOINT")
  Call<Post> createPost(@Body PostRequest post);
}

您可以尝试这样做,首先需要为请求的第一部分创建一个类。所以我会创建这样的东西->

class Article(
    @field:Json(name = "devicetype") var devicetype: String,
    @field:Json(name = "deviceid") var deviceid: String,
    @field:Json(name = "appversion") var appversion: String,
    @field:Json(name = "apiversion") var apiversion: String,
    @field:Json(name = "timezone") var timezone: String,
    @field:Json(name = "modeltype") var modeltype: String,
    @field:Json(name = "deviceos") var deviceos: String,
    @field:Json(name = "userdeviceid") var userdeviceid: String,
    @field:Json(name = "visitorid") var visitorid: String,
    @field:Json(name = "siteid") var siteid: String,
    @field:Json(name = "visitordetail") var visitordetail: List<VisitorDetail>    //Call the visitordetail class over here like this
) : Serializable
class VisitorDetail(
    @field:Json(name = "id") var id: String,
    @field:Json(name = "userid") var userid: String,
    @field:Json(name = "name") var name: String,
    @field:Json(name = "email") var email: String,
    @field:Json(name = "mobile") var mobile: String,
    @field:Json(name = "dealername") var dealername: String,
    @field:Json(name = "sitedetail") var sitedetail: List<SiteDetail>    //Call the site details class over here like this
) : Serializable
class Selections(
    @field:Json(name = "id") var id: String,
    @field:Json(name = "visitorid") var visitorid: String,
    @field:Json(name = "siteid") var siteid: String,
    @field:Json(name = "designno") var designno: String,
    @field:Json(name = "qty") var qty: String,
    @field:Json(name = "roomtype") var roomtype: String,
    @field:Json(name = "remarks") var remarks: String
):Serializable
我已经在Kotlin中完成了这项工作,但是您可以通过将其转换为java来尝试它。逻辑是,只要将所有响应转换为pojo类,就可以调用嵌套请求数据。只需一个接一个地传递它,然后将主pojo调用到您的改装中,其余的将由库处理。你也可以为多个部分做这个。
如果有任何愚蠢的错误,比如缺少逗号或其他什么,那么我会提前为此道歉。

最后我解决了这个问题

若有人想将带有额外参数的图像上传到服务器。以下是方法

首先在改型API请求接口中创建方法

@Multipart
@POST("test/test.post.json")
Call<APIResponseSiteDetails> addImages(@PartMap Map<String, RequestBody> params);
现在,如果您有另一个使用密钥对值的自定义请求,并且希望发送单个项目,那么您必须以这种方式装箱

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;
}
对于字符串值请求正文

  public static RequestBody toRequestBody(String value) {
  RequestBody body = RequestBody.create(MediaType.parse("text/plain"), value);
  return body;
}

上面是使用带有图像(多部分)数据的密钥对值执行单个、多个post请求的代码。我希望这对你们大家都有帮助

这完全取决于服务器对它的期望,它只是期望使用post方法得到各种形式的数据。
multipart
你是说?然后将其作为multipartbody的一部分发送。我可以发送它,但它是嵌套请求。我不知道如何处理itI,我认为您应该为自定义数据传递RequestBody。因此,我也可以使用此方法发送包含多部分表单数据的数据吗?这就是我几天前所做的,它工作得非常完美。您能分享完整的片段吗?所以这对我来说很容易你说的代码片段是什么意思?我已经检查了你的链接。我关于发送图像的问题。使用Json数据,我们无法发送图像文件。请检查一下好吗?
 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();
                            }
                        }
                    }
}
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;
}