如何在没有 POJO 类的情况下使用 Retrofit 在 Android (Java) 中发布原始 Json 对象

问题描述 投票:0回答:1

有两项活动,活动A和活动B

我在活动 A 中进行了 API 调用,并将 JsonObject 转换为字符串以供进一步使用,现在我再次使用 JsonParser.parseString(JsonObject 已转换为字符串).getAsJsonObject();

将其转换为 JsonObject

现在我做了一个 Post Api 调用,将其与 Jsonobject 相同发送到服务器使用改造 2.9.0 由于某种原因,它在发送时将其转换为字符串 顺便说一句,我已经尝试过 10-15 个 stackoverflow 示例,但到目前为止都没有成功。因此,如果您亲自发送了原始 jsonobject 来发布 Api,我们非常感谢您的帮助!!

这是示例 JsonObject 数据

{
    "data": {
        "care_of": "S/O: Kaleen Bhaiya",
        "full_name": "Munna Bhaiya",
        "aadhaar_number": "123456789011",
        "zip": "110088",
        "gender": "M",
        "share_code": "6921",
        "face_score": -1,
        "raw_xml": "https://link-to-docs",
        "has_image": true,
        "address": {
            "loc": "Lehna Singh Market",
            "street": "",
            "house": "Flat No - 123",
            "po": "Mirzapur",
            "country": "India",
            "subdist": "Mirzapur",
            "vtc": "Mirzapur",
            "dist": "North Mirzapur",
            "state": "Uttar Pradesh"
        },
        "client_id": "aadhaar_v2_JlgixCkkkqinfMqtnThX",
        "zip_data": "https://link-to-zip",
        "profile_image": "base_64_image",
        "face_status": false,
        "dob": "1996-01-01"
    },
    "success": true,
    "message_code": "success",
    "status_code": 200,
    "message": null
}

但是我得到的是对象转换为字符串那么我应该如何消除这个反斜杠以使其成为jsonObject?

{
    \"data\": {
        \"care_of\": \"S/O: Kaleen Bhaiya\",
        \"full_name\": \"Munna Bhaiya\",
        \"aadhaar_number\": \"123456789011\",
        \"zip\": \"110088\",
        \"gender\": \"M\",
        \"share_code\": \"6921\",
        \"face_score\": \-1\,
        \"raw_xml\": \"https://link-to-docs\",
        \"has_image\": \true\,
        \"address\": \{
            \"loc\": \"Lehna Singh Market\",
            \"street\": "",
            \"house\": \"Flat No - 123\",
            \"po\": \"Mirzapur\",
            \"country\": \"India\",
            \"subdist\": \"Mirzapur",
            \"vtc\": \"Mirzapur\",
            \"dist\": \"North Mirzapur\",
            \"state\": \"Uttar Pradesh\"
        },
        \"client_id\": \"aadhaar_v2_JlgixCkkkqinfMqtnThX\",
        \"zip_data\": \"https://link-to-zip\",
        \"profile_image\": \"base_64_image\",
        \"face_status\": \false\,
        \"dob\": \"1996-01-01\"
    },\
    \"success\": \true\,
    \"message_code\": \"success\",
    \"status_code\": \200\,
    \"message\": \null\
}
android json api retrofit2
1个回答
0
投票

首先您需要在 RetrofitService 中声明一个方法:

interface RetrofitService {
    @POST("path")
    Call<ResponseBody> update(@Body RequestBody requestBody);
}

接下来需要创建RequestBody和Call对象:

    Retrofit retrofit = new Retrofit.Builder().baseUrl("http://somedomain.com").build();
    RetrofitService retrofitService = retrofit.create(RetrofitService.class);
    
    String strRequestBody = "body";
    //use text/json for Json data string, or use text/plain for any other text
    RequestBody requestBody = RequestBody.create(MediaType.parse("text/json"), bodyTextStringJsonValue);
    Call<ResponseBody> call = retrofitService.update(requestBody);
© www.soinside.com 2019 - 2024. All rights reserved.