Swa*_*nil 63 java android json jackson
我从服务器获得此响应 {"status":"true","msg":"success"}
我试图使用杰克逊解析器库解析这个json字符串但不知何故我正面临映射异常说明
com.fasterxml.jackson.databind.JsonMappingException: No content to map due to end-of-input
at [Source: java.io.StringReader@421ea4c0; line: 1, column: 1]
Run Code Online (Sandbox Code Playgroud)
为什么我们会遇到这种例外?
如何理解导致此异常的原因?
我试图使用以下方式解析:
StatusResponses loginValidator = null;
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.configure(Feature.AUTO_CLOSE_SOURCE, true);
try {
String res = result.getResponseAsString();//{"status":"true","msg":"success"}
loginValidator = objectMapper.readValue(result.getResponseAsString(), StatusResponses.class);
} catch (Exception e) {
e.printStackTrace();
}
Run Code Online (Sandbox Code Playgroud)
StatusResponse类
@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonPropertyOrder({ "status","msg" })
public class StatusResponses {
@JsonProperty("status")
public String getStatus() {
return status;
}
@JsonProperty("status")
public void setStatus(String status) {
this.status = status;
}
@JsonProperty("msg")
public String getMessage() {
return message;
}
@JsonProperty("msg")
public void setMessage(String message) {
this.message = message;
}
@JsonProperty("status")
private String status;
@JsonProperty("msg")
private String message;
private Map<String, Object> additionalProperties = new HashMap<String, Object>();
@JsonGetter
public Map<String, Object> getAdditionalProperties() {
return additionalProperties;
}
@JsonSetter
public void setAdditionalProperties(Map<String, Object> additionalProperties) {
this.additionalProperties = additionalProperties;
}
}
Run Code Online (Sandbox Code Playgroud)
Swa*_*nil 25
import com.fasterxml.jackson.core.JsonParser.Feature;
import com.fasterxml.jackson.databind.ObjectMapper;
StatusResponses loginValidator = null;
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.configure(Feature.AUTO_CLOSE_SOURCE, true);
try {
String res = result.getResponseAsString();//{"status":"true","msg":"success"}
loginValidator = objectMapper.readValue(res, StatusResponses.class);//replaced result.getResponseAsString() with res
} catch (Exception e) {
e.printStackTrace();
}
Run Code Online (Sandbox Code Playgroud)
不知道它是如何工作的以及为什么有效?:(但它有效
在我的情况下,问题是由于我将 null InputStream 传递给 ObjectMapper.readValue 调用引起的:
ObjectMapper objectMapper = ...
InputStream is = null; // The code here was returning null.
Foo foo = objectMapper.readValue(is, Foo.class)
Run Code Online (Sandbox Code Playgroud)
我猜这是此异常的最常见原因。
对我来说,问题是我读了两遍回复,如下:
System.out.println(response.body().string());
getSucherResponse = objectMapper.readValue(response.body().string(), GetSucherResponse.class);
Run Code Online (Sandbox Code Playgroud)
但是,响应只能读取一次,因为它是流。
我今天遇到了类似的错误,问题是发布请求的内容类型标头。确保内容类型符合您的预期。就我而言,multipart/form-data内容类型标头被发送到 API,而不是application/json.
此错误有时(经常?)隐藏了真正的问题:故障条件可能导致内容不正确,然后无法反序列化。
就我而言,今天,我正在进行 HTTP 调用,并且(愚蠢地)在尝试解组响应正文之前省略了检查 HTTP 状态代码 => 我真正的问题实际上是我遇到了一些身份验证错误,这导致401 Unauthorized了送回给我,身体空空如也。由于我直接解组该空体而没有检查任何内容,因此我得到了这个No content to map due to end-of-input,而没有得到任何有关身份验证问题的线索。
小智 5
我可以解决此错误。就我而言,问题出在客户端。错误地,我没有关闭正在写入服务器的流。我关闭了流,并且运行正常。甚至错误听起来也像服务器无法识别输入结束。
OutputStream out = new BufferedOutputStream(urlConnection.getOutputStream());
out.write(jsonstring.getBytes());
out.close() ; //This is what I did
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
162657 次 |
| 最近记录: |