Page 1 of 2 12 LastLast
Results 1 to 10 of 13

Thread: Unable to pass list [Rest, XML, Android]

  1. #1
    Join Date
    Apr 2011
    Location
    Belgium (Europe)
    Posts
    9

    Default Unable to pass list [Rest, XML, Android]

    Hello Everyone

    I'm trying to create an android application that would connect on a server to get and post information about Events and the Users that can create the events.

    I implemented a function on the server that finds all the users in the database, and that sends them with webservices. But when I try to get back that information on my Android application, the application crashes.
    Because I work with lists, I tried to work with DTO, but the result is still the same.

    I'll put some code to show you how I work

    Code:
    @XmlRootElement(name="user")
    @Entity
    @Table(name = "SA_USER")
    public class User extends BaseEntity implements Serializable {
    
    	private static final long serialVersionUID = -8386881380312242149L;
    	
    	@Column(name = "FIRST_NAME", nullable = false, length = 32)
    	@Index(name = "INDEX_SA_USER_NAME", columnNames = {"FIRST_NAME", "LAST_NAME"})
    	private String firstName;
    	
    	@Column(name = "LAST_NAME", nullable = false, length = 32)
    	private String lastName;
    	
    	@Column(name = "USER_NAME", nullable = false, unique = true, length = 64)
    	@IndexColumn(name = "INDEX_SA_USER_USER_NAME")
    	private String userName;
    	
    	@Column(name = "PASSWORD", length = 64)
    	private String password;
    	
    	@Column(name = "EMAIL", nullable = false, unique = true, length = 128)
    	private String email;
    	
    	@Column(name = "PHONE_NUMBER", nullable = true, length = 20)
    	private String phoneNumber;
    	
    	@Column(name = "MOBILE", nullable = true, length = 20)
    	private String mobile;
    	
    	@ManyToOne(cascade = CascadeType.ALL)
    	@JoinColumn(name = "GRADE_ID", nullable = false)
    	private Grade grade;
    That's some code from my User class (I work with Hibernate)
    I am able to insert and retrieve correctly information in my Database.
    When I want to pass several Users to a client via REST, I use this method :

    Code:
    @RequestMapping(value="/user/all", method = RequestMethod.GET, headers="Accept=application/xml, application/json")
    	public @ResponseBody UserList getAllUsers() {
    		LOGGER.debug("findAllUsers!!");
    		UserList list = new UserList();
    		list.setUsers(this.teacherService.findAllUsers());
    		return list;
    	}
    The UserList code is the following :

    Code:
    @XmlRootElement(name="userList")
    public class UserList {
    	private List<User> users;
    	
    	public List<User> getUsers() {
    		return users;
    	}
    	
    	public void setUsers(List<User> users) {
    		this.users = users;
    	}
    }


    That's for the server side.

    I tried to get the userlist with the following code on the client (Android) side :

    Code:
    try {
            	userList = restTemplate.getForObject(URL, UserList.class);
            }
        	catch (RestClientException e) {
            	Toast.makeText(getApplicationContext(), "La connexion a �chou�e!", Toast.LENGTH_LONG).show();
            }
    my UserList on client side looks like this :
    Code:
    @Root(name = "userList")
    public class UserList {
    	@ElementList(inline = true)
    	private List<User> users;
    	
    	public List<User> getUsers() {
    		return users;
    	}
    	
    	public void setUsers(List<User> users) {
    		this.users = users;
    	}
    }
    my User class on the client side looks like this :

    Code:
    @Root
    public class User {
    
    	@Element
    	private Long id;
    	@Element
    	private Long version;
    	@Element
    	private String firstName;
    	@Element
    	private String lastName;
    	@Element
    	private String userName;
    	@Element
    	private String password;
    	@Element
    	private String email;
    	@Element
    	private String phoneNumber;
    	@Element
    	private String mobile;
    	@Element
    	private Grade grade;
    to implement all this, I used the spring documentation.

    But, when I try to get the users, The app crashes. I tried to connect to the server using this FireFox add-on ( https://addons.mozilla.org/en-US/fir...on/restclient/ ) and with that add-on, I'm able to get all my users correctly


    I hope what I wrote is somewhat understandable
    If someone has an idea, it would be very great!

    Thank you in advance!

  2. #2
    Join Date
    Nov 2010
    Posts
    175

    Default

    Hi Stef102, can you please provide the exception details, and also a sample of the format of the xml response? My guess is that the Simple XML library is throwing the exception because of a deserialization issue.
    Roy Clarkson
    Spring Mobile Projects Lead

  3. #3
    Join Date
    Apr 2011
    Location
    Belgium (Europe)
    Posts
    9

    Default

    Hello,

    Thank you for your reply! I had a closer look to my XML file, and managed to solve my problem. It was the fact that the objects passed in the XML file were of type "users" instead of "user". At first, I tought it was normal, because the list was named "users", but, when I corrected that, it solved the problem.

    I hope that this can be usefull for other newbies like I am
    Do I have to add something like "Answered" in the title of the subject, or is it OK like this?

    Thanks again

  4. #4
    Join Date
    Nov 2010
    Posts
    175

    Default

    Great! I'm glad you found the issue. These forums do not have a method to mark an accepted answer. But it's always helpful to others to communicate how you resolve issues, so they can benefit from it. Thanks!
    Roy Clarkson
    Spring Mobile Projects Lead

  5. #5
    Join Date
    Apr 2011
    Location
    Belgium (Europe)
    Posts
    9

    Default

    Ok, thanks again for the answer!

    Anyway, I will stay subscribed to this thread, so if there's any question left, i will see it

  6. #6
    Join Date
    Sep 2011
    Posts
    6

    Default

    Hi clark ,

    As u mentioned in the thread , i am getting the exception because of deserialization issue.
    ant the exception is :

    Caused by: org.codehaus.jackson.map.JsonMappingException: Can not deserialize instance of java.util.List out of START_OBJECT.

    any idea ?

    thanks,
    Arun

  7. #7
    Join Date
    Nov 2010
    Posts
    175

    Default

    As I mentioned earlier in this thread, it is difficult to diagnose deserialization without more information. Obviously, Jackson is having problems with something. Take a look at your android log and stacktrace to see if they help provide any additional information as to where the specific issue is. Hope that helps.
    Roy Clarkson
    Spring Mobile Projects Lead

  8. #8
    Join Date
    Sep 2011
    Posts
    2

  9. #9
    Join Date
    Nov 2010
    Posts
    175

    Default

    I think the issue may be that you are trying to deserialize json into a List<>. Try creating a wrapper bean with a property that holds your list and deserialize to that bean instead. Deserialization is simply the process of converting json data into a java bean. This is handled by RestTemplate and Jackson, in this case.
    Roy Clarkson
    Spring Mobile Projects Lead

  10. #10
    Join Date
    Oct 2011
    Posts
    3

    Default

    I have the same probleman.

    I created two simple projects to demonstrate how the problem occurs.
    http://www.megaupload.com/?d=54ZTRQ0W

    By clicking the button two (second), you will see that everything is fine, but clicking a button one (first), there is the problem in question.


    Note that the return (json) is different in both cases, if a (problematic) is the return
    {"category": {"name": "Cagetory 1"}}

    and the second return (json) is:
    {"category ":[{" name": "Cagetory 1 "},{" name": "Cagetory 2"}]}


    This is stacktrace
    Code:
    11-10 04:58:15.449: ERROR/AndroidRuntime(343): FATAL EXCEPTION: AsyncTask #2
    11-10 04:58:15.449: ERROR/AndroidRuntime(343): java.lang.RuntimeException: An error occured while executing doInBackground()
    11-10 04:58:15.449: ERROR/AndroidRuntime(343):     at android.os.AsyncTask$3.done(AsyncTask.java:200)
    11-10 04:58:15.449: ERROR/AndroidRuntime(343):     at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
    11-10 04:58:15.449: ERROR/AndroidRuntime(343):     at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
    11-10 04:58:15.449: ERROR/AndroidRuntime(343):     at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
    11-10 04:58:15.449: ERROR/AndroidRuntime(343):     at java.util.concurrent.FutureTask.run(FutureTask.java:137)
    11-10 04:58:15.449: ERROR/AndroidRuntime(343):     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1068)
    11-10 04:58:15.449: ERROR/AndroidRuntime(343):     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:561)
    11-10 04:58:15.449: ERROR/AndroidRuntime(343):     at java.lang.Thread.run(Thread.java:1096)
    11-10 04:58:15.449: ERROR/AndroidRuntime(343): Caused by: org.springframework.web.client.ResourceAccessException: I/O error: Can not deserialize instance of java.util.List out of START_OBJECT token
    11-10 04:58:15.449: ERROR/AndroidRuntime(343):  at [Source: org.apache.http.conn.EofSensorInputStream@44f7f238; line: 1, column: 2] (through reference chain: com.impact.client.tablet.dat.request.CategoryRest["category"]); nested exception is org.codehaus.jackson.map.JsonMappingException: Can not deserialize instance of java.util.List out of START_OBJECT token
    11-10 04:58:15.449: ERROR/AndroidRuntime(343):  at [Source: org.apache.http.conn.EofSensorInputStream@44f7f238; line: 1, column: 2] (through reference chain: com.impact.client.tablet.dat.request.CategoryRest["category"])
    11-10 04:58:15.449: ERROR/AndroidRuntime(343):     at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:477)
    11-10 04:58:15.449: ERROR/AndroidRuntime(343):     at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:425)
    11-10 04:58:15.449: ERROR/AndroidRuntime(343):     at org.springframework.web.client.RestTemplate.postForObject(RestTemplate.java:303)
    11-10 04:58:15.449: ERROR/AndroidRuntime(343):     at com.impact.client.tablet.dat.request.CategoryRequest.oneCategory(CategoryRequest.java:20)
    11-10 04:58:15.449: ERROR/AndroidRuntime(343):     at com.impact.client.tablet.dat.busca.OneCategoryDAT.doInBackground(OneCategoryDAT.java:22)
    11-10 04:58:15.449: ERROR/AndroidRuntime(343):     at com.impact.client.tablet.dat.busca.OneCategoryDAT.doInBackground(OneCategoryDAT.java:1)
    11-10 04:58:15.449: ERROR/AndroidRuntime(343):     at android.os.AsyncTask$2.call(AsyncTask.java:185)
    11-10 04:58:15.449: ERROR/AndroidRuntime(343):     at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
    11-10 04:58:15.449: ERROR/AndroidRuntime(343):     ... 4 more
    11-10 04:58:15.449: ERROR/AndroidRuntime(343): Caused by: org.codehaus.jackson.map.JsonMappingException: Can not deserialize instance of java.util.List out of START_OBJECT token
    11-10 04:58:15.449: ERROR/AndroidRuntime(343):  at [Source: org.apache.http.conn.EofSensorInputStream@44f7f238; line: 1, column: 2] (through reference chain: com.impact.client.tablet.dat.request.CategoryRest["category"])
    11-10 04:58:15.449: ERROR/AndroidRuntime(343):     at org.codehaus.jackson.map.deser.StdDeserializationContext.mappingException(StdDeserializationContext.java:198)
    11-10 04:58:15.449: ERROR/AndroidRuntime(343):     at org.codehaus.jackson.map.deser.CollectionDeserializer.handleNonArray(CollectionDeserializer.java:149)
    11-10 04:58:15.449: ERROR/AndroidRuntime(343):     at org.codehaus.jackson.map.deser.CollectionDeserializer.deserialize(CollectionDeserializer.java:107)
    11-10 04:58:15.449: ERROR/AndroidRuntime(343):     at org.codehaus.jackson.map.deser.CollectionDeserializer.deserialize(CollectionDeserializer.java:97)
    11-10 04:58:15.449: ERROR/AndroidRuntime(343):     at org.codehaus.jackson.map.deser.CollectionDeserializer.deserialize(CollectionDeserializer.java:26)
    11-10 04:58:15.449: ERROR/AndroidRuntime(343):     at org.codehaus.jackson.map.deser.SettableBeanProperty.deserialize(SettableBeanProperty.java:252)
    11-10 04:58:15.449: ERROR/AndroidRuntime(343):     at org.codehaus.jackson.map.deser.SettableBeanProperty$MethodProperty.deserializeAndSet(SettableBeanProperty.java:356)
    11-10 04:58:15.449: ERROR/AndroidRuntime(343):     at org.codehaus.jackson.map.deser.BeanDeserializer.deserializeFromObject(BeanDeserializer.java:494)
    11-10 04:58:15.449: ERROR/AndroidRuntime(343):     at org.codehaus.jackson.map.deser.BeanDeserializer.deserialize(BeanDeserializer.java:350)
    11-10 04:58:15.449: ERROR/AndroidRuntime(343):     at org.codehaus.jackson.map.ObjectMapper._readMapAndClose(ObjectMapper.java:2395)
    11-10 04:58:15.449: ERROR/AndroidRuntime(343):     at org.codehaus.jackson.map.ObjectMapper.readValue(ObjectMapper.java:1655)
    11-10 04:58:15.449: ERROR/AndroidRuntime(343):     at org.springframework.http.converter.json.MappingJacksonHttpMessageConverter.readInternal(MappingJacksonHttpMessageConverter.java:146)
    11-10 04:58:15.449: ERROR/AndroidRuntime(343):     at org.springframework.http.converter.AbstractHttpMessageConverter.read(AbstractHttpMessageConverter.java:148)
    11-10 04:58:15.449: ERROR/AndroidRuntime(343):     at org.springframework.web.client.HttpMessageConverterExtractor.extractData(HttpMessageConverterExtractor.java:68)
    11-10 04:58:15.449: ERROR/AndroidRuntime(343):     at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:470)
    11-10 04:58:15.449: ERROR/AndroidRuntime(343):     ... 11 more
    11-10 04:58:15.749: WARN/ActivityManager(67):   Force finishing activity com.impact.client/.tablet.controller.cardapio.CardapioLayoutActivity
    11-10 04:58:19.639: ERROR/WindowManager(343): Activity com.impact.client.tablet.controller.cardapio.CardapioLayoutActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@44f66fb0 that was originally added here
    11-10 04:58:19.639: ERROR/WindowManager(343): android.view.WindowLeaked: Activity com.impact.client.tablet.controller.cardapio.CardapioLayoutActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@44f66fb0 that was originally added here
    11-10 04:58:19.639: ERROR/WindowManager(343):     at android.view.ViewRoot.<init>(ViewRoot.java:247)
    11-10 04:58:19.639: ERROR/WindowManager(343):     at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:148)
    11-10 04:58:19.639: ERROR/WindowManager(343):     at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91)
    11-10 04:58:19.639: ERROR/WindowManager(343):     at android.view.Window$LocalWindowManager.addView(Window.java:424)
    11-10 04:58:19.639: ERROR/WindowManager(343):     at android.app.Dialog.show(Dialog.java:241)
    11-10 04:58:19.639: ERROR/WindowManager(343):     at com.impact.client.tablet.controller.cardapio.AAsyncActivityImplDefault.showProgressDialog(AAsyncActivityImplDefault.java:36)
    11-10 04:58:19.639: ERROR/WindowManager(343):     at com.impact.client.tablet.controller.cardapio.AAsyncActivityImplDefault.showLoadingProgressDialog(AAsyncActivityImplDefault.java:26)
    11-10 04:58:19.639: ERROR/WindowManager(343):     at com.impact.client.tablet.controller.cardapio.AAsyncActivity.showLoadingProgressDialog(AAsyncActivity.java:15)
    11-10 04:58:19.639: ERROR/WindowManager(343):     at com.impact.client.tablet.dat.ADownloadTask.onPreExecute(ADownloadTask.java:32)
    11-10 04:58:19.639: ERROR/WindowManager(343):     at android.os.AsyncTask.execute(AsyncTask.java:391)
    11-10 04:58:19.639: ERROR/WindowManager(343):     at com.impact.client.tablet.controller.cardapio.CardapioLayoutModel.oneCategory(CardapioLayoutModel.java:24)
    11-10 04:58:19.639: ERROR/WindowManager(343):     at com.impact.client.tablet.controller.cardapio.CardapioLayoutActivity$1.onClick(CardapioLayoutActivity.java:39)
    11-10 04:58:19.639: ERROR/WindowManager(343):     at android.view.View.performClick(View.java:2408)
    11-10 04:58:19.639: ERROR/WindowManager(343):     at android.view.View$PerformClick.run(View.java:8816)
    11-10 04:58:19.639: ERROR/WindowManager(343):     at android.os.Handler.handleCallback(Handler.java:587)
    11-10 04:58:19.639: ERROR/WindowManager(343):     at android.os.Handler.dispatchMessage(Handler.java:92)
    11-10 04:58:19.639: ERROR/WindowManager(343):     at android.os.Looper.loop(Looper.java:123)
    11-10 04:58:19.639: ERROR/WindowManager(343):     at android.app.ActivityThread.main(ActivityThread.java:4627)
    11-10 04:58:19.639: ERROR/WindowManager(343):     at java.lang.reflect.Method.invokeNative(Native Method)
    11-10 04:58:19.639: ERROR/WindowManager(343):     at java.lang.reflect.Method.invoke(Method.java:521)
    11-10 04:58:19.639: ERROR/WindowManager(343):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
    11-10 04:58:19.639: ERROR/WindowManager(343):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
    11-10 04:58:19.639: ERROR/WindowManager(343):     at dalvik.system.NativeStart.main(Native Method)
    Last edited by netstart; Nov 10th, 2011 at 12:15 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •