gpt4 book ai didi

java - com.fasterxml.jackson.databind.JsonMappingException : No content to map due to end-of-input

转载 作者:塔克拉玛干 更新时间:2023-11-03 03:01:43 53 4
gpt4 key购买 nike

我需要用 java POJO 类映射一个 JSON 数组对象。我写的代码是这样的:

// execute the client with get method 
InputStream inputStream = getMethod.getResponseBodyAsStream();

BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));

ObjectMapper objectMapper = new ObjectMapper();

JsonFactory jsonFactory = new JsonFactory();
List<OwnerDetail> owners = new ArrayList<>();
JsonParser jsonParser = jsonFactory.createJsonParser(inputStream);

if (jsonParser.nextToken() != null && jsonParser.)
{ // end-of-input
owners = objectMapper.readValue(bufferedReader, TypeFactory.defaultInstance().constructCollectionType(List.class, OwnerDetail.class));
}

上面的 block 给我以下错误:

com.fasterxml.jackson.databind.JsonMappingException: No content to map due to end-of-input
at [Source: java.io.BufferedReader@5e66c5fc; line: 1, column: 1]
at com.fasterxml.jackson.databind.JsonMappingException.from(JsonMappingException.java:164)
at com.fasterxml.jackson.databind.ObjectMapper._initForReading(ObjectMapper.java:3029)
at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:2971)
at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:2128)

如有任何帮助,我们将不胜感激。谢谢。

最佳答案

读取响应后,响应中的数据被消耗。如果您的代码在拦截器中,您可以尝试再次创建响应并返回如下:

    Request request = chain.request();
Response originalResponse = chain.proceed(request);
final ResponseBody original = originalResponse.body();
// if(request.url().toString().equalsIgnoreCase(string)){
if (originalResponse.code() == HttpURLConnection.HTTP_OK) {
try {
String response = originalResponse.body().string();
JSONObject mainObject = new JSONObject(response);

// your mapping - manipulation code here.
originalResponse = originalResponse.newBuilder()
.header("Cache-Control", "max-age=60")
.body(ResponseBody.create(original.contentType(),
mainObject.toString().getBytes()))
.build();

} catch (JSONException | IOException e) {
e.printStackTrace();
}
}
return originalResponse;

此处响应再次创建并返回。

请让我知道任何更新。

关于java - com.fasterxml.jackson.databind.JsonMappingException : No content to map due to end-of-input,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27104197/

53 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com