我需要使用
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)); }
以上块给出了以下错误:
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;
这里再次创建响应并返回.
请告诉我任何更新.