2014-11-14 11 views
28

sto ottenendo questa risposta dal server {"status":"true","msg":"success"}Nessun contenuto per mappare a causa di end-of-input jackson parser

Sto cercando di analizzare questa stringa JSON utilizzando la libreria Jackson parser ma in qualche modo sto affrontando mapping-eccezione dichiarando

com.fasterxml.jackson.databind.JsonMappingException: No content to map due to end-of-input 
at [Source: [email protected]; line: 1, column: 1] 

Perché otteniamo questo tipo di eccezioni?

Come capire che cosa sta causando questa eccezione?

Sto cercando di analizzare con seguente modo:

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(); 
} 

classe 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; 
} 
} 

risposta

7
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(); 
} 

non so come ha funzionato e perché ha funzionato? :(ma ha funzionato

+6

Pensa che è perché quando chiami getResponseAsString() legge tutti i byte dalla risposta e dalla connessione chiusa. throw exception – Koloritnij

+0

Questo funziona a causa della stringa che hai sostituito e non a causa di Feature.AUTO_CLOSE_SOURCE .... –

0

Per uno, @JsonProperty("status") e @JsonProperty("msg") dovrebbe essere solo lì solo quando si dichiara i campi, non sulla setter e ricevitori.

In realtà, il modo più semplice per analizzare questo sarebbe

@JsonAutoDetect //if you don't want to have getters and setters for each JsonProperty 
public class StatusResponses { 

    @JsonProperty("status") 
    private String status; 

    @JsonProperty("msg") 
    private String message; 

} 
+0

ma come sarà questo risolverà l'eccezione mappatura? e senza getter e setter come posso accedere ai valori arsed per un ulteriore uso? – Swapnil

+0

È può ancora aggiungere getter e setter senza annotazione.Penso che la tua annotazione stia confondendo il mapper –

4

Ho potuto correggere questo errore Nel mio caso il problema era al client Per errore non ho chiuso il flusso che stavo scrivendo al server, ho chiuso il flusso e ha funzionato bene. l'errore suona come server non è stato in grado di identificare la fine di ingresso.

OutputStream out = new BufferedOutputStream(urlConnection.getOutputStream()); 
out.write(jsonstring.getBytes()); 
out.close() ; //This is what I did 
+1

Questa risposta ha funzionato per me – bademba

Problemi correlati