Search code examples
androidgoogle-app-enginerestletobjectify

Android app > Restlet + Objectify > Google App Engine = Unsupported Media Type (415) - Unsupported Media Type


-Hello, I followed this tutorial and I managed to store data in Google App Engine via a Restlet Webservice (using Objectify) called from an Android App :

http://www.tutos-android.com/webservice-rest-android-appengine-restlet-objectify

The tutorial uses a model class named "User" :

  • User.java
  • UserController.java
  • UserControllerInterface.java

Now I want to use my own model class named "Track" and build my own app.

So I created a new Android project, a new Restlet Server project and I created a new Google Engine App just to be clean.

In short, I have the same coding than the tutorial except that I work with "Tracks" instead of "Users" :

  • Track.java
  • TrackController.java
  • TrackControllerInterface.java

When I call the create method via the Android App, I get a : "Unsupported Media Type (415) - Unsupported Media Type"

My Interface :

public interface TrackControllerInterface {
    @Put
    void create(Track track);

    @Get
    Container getAllTracks();
}

My RestletDispatch :

public class RestletDispatch extends Application
{
  @Override
  public synchronized Restlet createInboundRoot()
  {
    final Router router = new Router(getContext());
    router.attach("/track", TrackController.class);
    return router;
  }
}

My Track server side (I removed all extra attributes except the name just to be sure) :

public class Track implements Serializable {

    private static final long serialVersionUID = 2028908999105872121L;

    @Id
    private Long id;
    private String name;

    public Track() {

    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

My TrackController server side :

import static com.restlet.pcn.vttracks.server.OfyService.ofy;

public class TrackController extends ServerResource implements
    TrackControllerInterface {

    public TrackController() {
    }

    @Override
    public void create(Track track) {
        Track tk = new Track();
        tk.setName(track.getName());
        ofy().put(tk);
    }

    @Override
    public Container getAllTracks() {
        Container content = null;
        List tracks = new ArrayList();

        Query<Track> qTracks = ofy().query(Track.class);

        for (Track track : qTracks){
            tracks.add(track);
        }
        content = new Container();
        content.setTrack_list(tracks);

        return content;
    }
}

The call in the Android App :

EditText name = (EditText) findViewById(R.id.track_create_name);
Track track = new Track();
track.setName(name.getText().toString());
final TrackController tc = new TrackController();
try {
    tc.create(track);
} catch (Exception e) {

In the logs of the Google App Engine I see :

93.0.180.137 - - [16/Feb/2013:00:46:07 -0800] "PUT /rest/track HTTP/1.1" 415 546 - "Restlet-Framework/2.0rc4" "vttracks-app-test2.appspot.com" ms=1407 cpu_ms=1027 cpm_usd=0.000061 instance=00c61b117c40b5128fe48b35a014925f9d2f

I tried to use a String instead of Track in the Interface and it worked without 415 error so there must be a problem with the definition/use of the Track Class :

public interface TrackControllerInterface {
    @Put
    void create(String trackName);

    @Get
    Container getAllTracks();
}

- What did I miss when I "refactored" from "User" to "Track" ?

- What do I have to check to correct a "415 error" with these Frameworks ?

I looked for other questions about 415 but can't find any pointers for this case.

Thanks in advance for your help.


Solution

  • OK, I found what was wrong : the package of the "Track" class on the server side and the package of the "Track" class on the client side were different causing serialization problems.

    When I refactored I created new Projects and reorganized the packages...

    Thanks to Thierry and Koma for answering.