0

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

1 Answer 1

1

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:

{"data":
   {
    "allFormsResult": [
        {
            "FormId": 1,
            "FormName": "Formulario 1"
        },
        {
            "FormId": 2,
            "FormName": "Formulario 2"
        },
        {
            "FormId": 3,
            "FormName": "Formulario 3"
        }
    ]
  }
}

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 "[".

Sign up to request clarification or add additional context in comments.

5 Comments

Thanks. How can I fix that? I can modify that web service (made with C# and WCF), but I don't know how.
how do you parsing with Gson in Android?
Parsing is made by spring framework. All my code about parsing it is shown here (on LoadAll() method).
i mean to say that how you used Gson?, response is ok but problem is at parsing side
Ok. I don't have any idea about how parsing JSON on Android, so I have followed this example: static.springsource.org/spring-android/docs/1.0.x/reference/…. I've used Spring Framework to do it all.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.