I am trying to serialize the request before sending it to the retrofit for webservice calls.
as i am serializing the request , i need to pass json string to retrofit calls in @Body paramenter and due to that the generated json string results into following json string with the " (Double quotes in front and end ).
"{"access_token":"d80fa6bd6f78cc704104d61146c599bc94b82ca225349ee68762fc6c70d2dcf0","fitness":[{"_id":"1d051bfe-df30-4fa0-808b-9d7300a608ab","activity_id":"877284d3-4f36-4ec0-a536-11563207dc4d","calories":600.0,"distance":40.0,"intensity":"100","timestamp":"2018-07-18T12:56:43+00:00","type":"Running","utc_offset":"+05:30"},{"_id":"2004ff72-707d-489a-927e-4cdeed410095","activity_id":"5ed7c90f-805e-4763-aa62-7f8126c84f06","calories":600.0,"distance":40.0,"intensity":"100","timestamp":"2018-07-18T12:56:43+00:00","type":"Running","utc_offset":"+05:30"}]}"
as there are double quotes the third party api is unable to parse it successfully.
here is my reqeust serializer code
public class RequestSerializer implements JsonSerializer<Request<?>> {
@Override
public JsonElement serialize(Request<?> request, Type typeOfSrc, JsonSerializationContext context) {
JsonObject jsonObject = new GsonBuilder().create().toJsonTree(request,Request.class).getAsJsonObject();
JsonElement requestList = jsonObject.get("requestList");
jsonObject.remove("requestList");
jsonObject.add("fitness",requestList);
return jsonObject;
}
}
code to call retrofit webservice
GsonBuilder builder = new GsonBuilder();
builder.excludeFieldsWithoutExposeAnnotation();
builder.registerTypeAdapter(Request.class, new RequestSerializer());
Gson gson = builder.create();
String data = gson.toJson(request);
Flowable<Response> fitnessFlowable = new WebRequest().getRemoteClient().create(FitnessApi.class).postFitnessData("5b238abb4d3590001d9b94a8",data);
1条答案
按热度按时间smdnsysy1#
使用对象消除了POST @Body中的字符串引用请求,例如我的工作代码:
使用