I’m developing an Android 3.1 and above application.
I’m using Spring Framework 1.0.0.0RC1 for Android, and Google GSon 2.1.
I’m getting an error when I’m trying to parsing JSON.
This is JSON returned by “http://192.168.1.128/RestServiceImpl.svc/forms/”.
{
"allFormsResult": [
{
"FormId": 1,
"FormName": "Formulario 1"
},
{
"FormId": 2,
"FormName": "Formulario 2"
},
{
"FormId": 3,
"FormName": "Formulario 3"
}
]
}
Here I do everything:
public class FormSpringController
{
public static List<Form> LoadAll()
{
HttpHeaders requestHeaders = new HttpHeaders();
requestHeaders.setAccept(Collections.singletonList(new MediaType("application","json")));
HttpEntity<?> requestEntity = new HttpEntity<Object>(requestHeaders);
String url = "http://192.168.1.128/RestServiceImpl.svc/forms/";
GsonHttpMessageConverter messageConverter = new GsonHttpMessageConverter();
List<HttpMessageConverter<?>> messageConverters = new ArrayList<HttpMessageConverter<?>>();
messageConverters.add(messageConverter);
RestTemplate restTemplate = new RestTemplate();
restTemplate.setMessageConverters(messageConverters);
ResponseEntity<Form[]> responseEntity = restTemplate.exchange(url, HttpMethod.GET, requestEntity, Form[].class);
Form[] result= responseEntity.getBody();
return Arrays.asList(result);
}
}
When I try to parse it I get the following error:
W/System.err(519): Caused by: java.lang.IllegalStateException: Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 2
Do you know how can I fix it?
UPDATE
@hotveryspicy has suggested me there is a “problem” with JSON. This is how I’m generating JSON response (C# code):
public class RestServiceImpl : IRestServiceImpl
{
public List<FormContract> allForms()
{
List<FormContract> list = null;
using (var vAdmEntities = new ADMDatabase.ADMEntities())
{
list = new List<FormContract>();
foreach (var form in vAdmEntities.Form)
{
FormContract formC = new FormContract
{
FormName = form.name.Trim(),
FormId = form.formId
};
list.Add(formC);
}
}
return list;
}
}
as your string starts with “
{“, which means it is an Object(json concept) and it seems that your considering it as an array, which is wrong.EDITED:
just append a object “data”, and then continue with you parsing. It is a problem of Json when it got first element as an Array “
[“.