Search code examples
androidruby-on-railsspringrobospice

How do I make a a java object to recieve a model from my rails api?


I've just started learning about Android Spring through a library called RoboSpice which is used for asynchronous RESTFUL tasks. I'm aiming to connect an android app to a rails api. It also uses Jackson. I'm getting the hang of it, but the main thing I can't wrap my head around is how I make an object class in java and then perform a POST or GET and expect to get the correct object back from the rails model or how it could know how to save posted data in the model. For example, if I have a model called Interview that belongs to a user (who has many) which contains info like job, date, time, company.

How would I write that object? Is there a very specific structure I need for that object class? How could I use get to retrieve the interviews from a user? Any explanations or links to a good tutorial would be a big help.

thanks


Solution

  • I have used Retrofit to communicate between my rails app and android app. I thought it was json only, but apparently it works with XML as well.

    Here is the link to the main web page, and here is the link to the page that talks about supporting xml.

    Retrofit makes communicating between android and a Restful API, pretty easy. You can get all the details in the first link above.

    One last excellent resource for Android RestFul API access. This Blog post combines using Retrofit and Otto (also from square). This was actually how I setup my link. Since retrofit supports xml, I think everything in this blog post will also apply to a XML based API.

    I think the big part of your question was on the conversion process. Retrofit uses Simple, for xml, it uses gson for json by default, but the process for converting is pretty similar. The main thing you need to do is to define a java class that has fields that match the xml nodes in the xml that is sent / expected by the api. the converter will map those nodes to the java object fields and vise versa.

    Here is an example (directly from the Simple tutorial link above). The java class is basically just the fields you want to send back and forth and getters and setters for those fields.

    Nested Java class "template" that will be filled in by the converter when it does the de-serialization

    @Root
    public class Configuration {
    
       @Element
       private Server server;
    
       @Attribute
       private int id;
    
       public int getIdentity() {
          return id;
       }
    
       public Server getServer() {
          return server;           
       }
    }
    
    public class Server {
    
       @Attribute
       private int port;
    
       @Element
       private String host;
    
       @Element
       private Security security;
    
       public int getPort() {
          return port;           
       }
    
       public String getHost() {
          return host;           
       }
    
       public Security getSecurity() {
          return security;           
       }
    }
    
    public class Security {
    
       @Attribute
       private boolean ssl;
    
       @Element
       private String keyStore;
    
       public boolean isSSL() {
          return ssl;           
       }
    
       public String getKeyStore() {
          return keyStore;           
       }
    }
    

    and the xml that it would serialize

    <configuration id="1234">
       <server port="80">
          <host>www.domain.com</host>
          <security ssl="true">
             <keyStore>example keystore</keyStore>
          </security>
       </server>
    </configuration>