How do I construct and pass raw json data with okHttpClient in android

醉酒当歌 提交于 2020-07-06 05:29:45

问题


I am using a query where I need to POST to an endpoint with okhttpclient. The issue is I have the payload value and the senttime. I just don't know how to create a json object to send over as a raw body object to the endpoint. To make things more clear: here's my postman screenshot: enter image description here

This is my post request : https://example.org/api/service-profile/v1/device-notification/{deviceId} Now I am planning to pass {deviceId} in the path, authorization token in the header and finally , as raw payload query like this in the body :

    {
      "payload": {"notificationID":"677291f5-a784-43df-af2e-0363a4067e9c","title":"test 223","body":"payload","lockScreenVisibility":1,"groupMessage":"","fromProjectNumber":"819539062812","priority":5,"rawPayload":"{\"google.delivered_priority\":\"normal\",\"google.sent_time\":1591849563191,\"google.ttl\":259200,\"google.original_priority\":\"normal\",\"custom\":\"{\\\"i\\\":\\\"677291f5-a784-43df-af2e-0363a4067e9c\\\"}\",\"oth_chnl\":\"\",\"pri\":\"5\",\"abc\":\"1\",\"from\":\"819539062812\",\"alert\":\"payload\",\"title\":\"test 223\",\"grp_msg\":\"\",\"google.message_id\":\"0:1591849563205759%409ebbcaf9fd7ecd\",\"google.c.sender.id\":\"819539062812\",\"notificationId\":-1451117355}"}
    ,
      "sentTime": 1591849563191
    }

trouble is I have the payload value and senttime, but I dont know how to construct a json object such that i can create the structure as { "payload" :pass in my payload value, "senttime":pass in sent time} and then pass it as a raw body param over okhttpclient? Any idea how to accomplish this?

Thanks!

来源:https://stackoverflow.com/questions/62377933/how-do-i-construct-and-pass-raw-json-data-with-okhttpclient-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!