내 retorfit2 POST 요청 응답에서 문자열을 제거하고 싶습니다.
이것은 내 응답입니다 .
/*-secure-{"response":{"response":{"response":{"token":"95a2c5a8","email":"[email protected]","name":"xxx"},"status":true,"code":0.0},"status":200},"status":200}*/
보안상의 이유로 내 서버가 자동으로 문자열을 추가합니다.
/*-secure-
과 */
이 때문에 json으로 변환하면 오류가 발생합니다 .
com.google.gson.stream.MalformedJsonException: Use JsonReader.setLenient(true) to accept malformed JSON at line 1 column 2 path $
내 사용자 정의 gson 변환기 클래스
final class CustomGsonResponseBodyConverter<T> implements Converter<ResponseBody, T> {
private final Gson gson;
private final TypeAdapter<T> adapter;
private Pattern pattern = Pattern.compile("^\\/\\*-secure-\\W(.*)\\*\\/$");
CustomGsonResponseBodyConverter(Gson gson, TypeAdapter<T> adapter) {
this.gson = gson;
this.adapter = adapter;
}
@Override
public T convert(ResponseBody value) throws IOException {
String response = value.string();
System.out.println(response);
Matcher matcher = pattern.matcher(response);
JsonReader jsonReader = gson.newJsonReader(new StringReader(matcher.group(1)));
try {
return adapter.read(jsonReader);
} finally {
value.close();
}
}
}
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다