我使用android rest模板解析我的android項目中的json服務。 當我嘗試解析json我有一些錯誤。Json在android中使用restTemplate解析(BEGIN_ARRAY,但是BEGIN_OBJECT)
錯誤:
**org.springframework.http.converter.HttpMessageNotReadableExeption:
Could not read JSON:java.lang.IllegalStateExeption:
Exepted BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 2;
nested exception in com.google.gson.JsonSyntaxException:java**
.......等
這裏是我的JSON:
{
"kategorijaartikla": [
{
"id": "1",
"kategorija": "kategorija1"
},
{
"id": "2",
"kategorija": "kategorija2"
}
]
}
這裏是我的RestTemplate代碼:
String url = "http://tomcat.fit.ba/asdf/rest/GetAllKategorije";
HttpHeaders requestHeaders = new HttpHeaders();
requestHeaders.setAccept(Collections.singletonList(new MediaType("application","json")));
HttpEntity<?> requestEntity = new HttpEntity<Object>(requestHeaders);
GsonHttpMessageConverter messageConverter = new GsonHttpMessageConverter();
List<HttpMessageConverter<?>> messageConverters = new ArrayList<HttpMessageConverter<?>>();
messageConverters.add(messageConverter);
RestTemplate restTemplate = new RestTemplate();
restTemplate.getMessageConverters().add(messageConverter);
restTemplate.getMessageConverters().add(new org.springframework.http.converter.json.GsonHttpMessageConverter());
ResponseEntity<kategorijaartikla[]> responseEntity = restTemplate.exchange(url, HttpMethod.GET, requestEntity, kategorijaartikla[].class);
kategorijaartikla[] events = responseEntity.getBody();
kategorijaartikla.java
public class kategorijaartikla {
private Integer id; private String kategorija;
public kategorijaartikla() { }
public Integer getId() { return id; } public void setId(Integer newId) { this.id = newId; }
public String getKategorija() { return kategorija; }
public void setKategorija(String newKategorija) { this.kategorija = newKategorija; }
logcat的:
12-07 02:55:21.123: E/Trace(1043): error opening trace file: No such file or directory (2)
12-07 02:55:22.693: D/libEGL(1043): loaded /system/lib/egl/libEGL_emulation.so
12-07 02:55:22.702: D/(1043): HostConnection::get() New Host Connection established 0x2a0d9c80, tid 1043
12-07 02:55:22.723: D/libEGL(1043): loaded /system/lib/egl/libGLESv1_CM_emulation.so
12-07 02:55:22.754: D/libEGL(1043): loaded /system/lib/egl/libGLESv2_emulation.so
12-07 02:55:22.903: W/EGL_emulation(1043): eglSurfaceAttrib not implemented
12-07 02:55:22.963: D/OpenGLRenderer(1043): Enabling debug mode 0
12-07 02:55:23.363: D/dalvikvm(1043): GC_CONCURRENT freed 231K, 6% free 6196K/6535K, paused 76ms+41ms, total 284ms
12-07 02:57:16.873: W/EGL_emulation(1043): eglSurfaceAttrib not implemented 12-07 02:57:20.872: D/dalvikvm(1043): null clazz in OP_INSTANCE_OF, single-stepping
這將有助於有其餘的錯誤堆棧... – PearsonArtPhoto
錯誤是在頂部的帖子,如有必要我可以上傳截圖。 –
比它複製/粘貼更好。 – PearsonArtPhoto