重写SimpleXmlRequestBodyConverter中的convert方法,将转换的数据进行解析。
public RequestBody convert(T value) throws IOException { Buffer buffer = new Buffer(); try { OutputStreamWriter osw = new OutputStreamWriter(buffer.outputStream(), CHARSET); this.serializer.write(value, osw); osw.flush(); } catch (Exception var4) { throw new RuntimeException(var4); } RequestBody requestBody = RequestBody.create(MEDIA_TYPE, buffer.readByteString()); buffer = new Buffer();//随便处理--- requestBody.writeTo(buffer); Charset charset = Charset.forName("UTF-8"); MediaType contentType = requestBody.contentType(); if (contentType != null) { charset = contentType.charset(charset); } String string = buffer.readString(charset); String encryptStr = string.replace(""", "\""); requestBody = MultipartBody.create(contentType, encryptStr); return requestBody; }
也可以用Okhttp拦截器来实现 双引号转成"问题
最终解析效果为 <xml>{"name":"小明","age":"12"}</xml>
可实现xml中加json的方法。
后台数据返回也是如此处理,后续在补充吧。