1

I am getting this representation error. Can someone help to get it fixed?

Error:

 Process: com.package, PID: 10791
java.lang.RuntimeException: Unable to resume activity {com.package/com.package.MainActivity}: org.restlet.resource.ResourceException: Unprocessable Entity (422) - The server understands the content type of the request entity and the syntax of the request entity is correct but was unable to process the contained instructions
    at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3103)
    at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3134)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2481)
    at android.app.ActivityThread.-wrap11(ActivityThread.java)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
    at android.os.Handler.dispatchMessage(Handler.java:102)
    at android.os.Looper.loop(Looper.java:148)
    at android.app.ActivityThread.main(ActivityThread.java:5417)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
 Caused by: org.restlet.resource.ResourceException: Unprocessable Entity (422) - The server understands the content type of the request entity and the syntax of the request entity is correct but was unable to process the contained instructions
    at org.restlet.resource.Resource.toObject(Resource.java:893)
    at org.restlet.engine.resource.ClientInvocationHandler.invoke(ClientInvocationHandler.java:324)
    at java.lang.reflect.Proxy.invoke(Proxy.java:393)
    at $Proxy4.retrieve(Unknown Source)
    at com.package.MainActivity.onResume(MainActivity.java:236)
    at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1258)
    at android.app.Activity.performResume(Activity.java:6312)
    at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3092)
    at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3134) 
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2481) 
    at android.app.ActivityThread.-wrap11(ActivityThread.java) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
    at android.os.Handler.dispatchMessage(Handler.java:102) 
    at android.os.Looper.loop(Looper.java:148) 
    at android.app.ActivityThread.main(ActivityThread.java:5417) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
 Caused by: java.io.IOException: Unable to create the Object representation
    at org.restlet.engine.converter.DefaultConverter.toObject(DefaultConverter.java:282)
    at org.restlet.service.ConverterService.toObject(ConverterService.java:229)
    at org.restlet.resource.Resource.toObject(Resource.java:889)
    at org.restlet.engine.resource.ClientInvocationHandler.invoke(ClientInvocationHandler.java:324) 
    at java.lang.reflect.Proxy.invoke(Proxy.java:393) 
    at $Proxy4.retrieve(Unknown Source) 
    at com.package.MainActivity.onResume(MainActivity.java:236) 
    at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1258) 
    at android.app.Activity.performResume(Activity.java:6312) 
    at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3092) 
    at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3134) 
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2481) 
    at android.app.ActivityThread.-wrap11(ActivityThread.java) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
    at android.os.Handler.dispatchMessage(Handler.java:102) 
    at android.os.Looper.loop(Looper.java:148) 
    at android.app.ActivityThread.main(ActivityThread.java:5417) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
 Caused by: java.lang.IllegalArgumentException: The serialized representation must have this media type: application/x-java-serialized-object or this one: application/x-java-serialized-object+xml
    at org.restlet.representation.ObjectRepresentation.<init>(ObjectRepresentation.java:194)
    at org.restlet.representation.ObjectRepresentation.<init>(ObjectRepresentation.java:123)
    at org.restlet.representation.ObjectRepresentation.<init>(ObjectRepresentation.java:104)
    at org.restlet.engine.converter.DefaultConverter.toObject(DefaultConverter.java:279)
    at org.restlet.service.ConverterService.toObject(ConverterService.java:229) 
    at org.restlet.resource.Resource.toObject(Resource.java:889) 
    at org.restlet.engine.resource.ClientInvocationHandler.invoke(ClientInvocationHandler.java:324) 
    at java.lang.reflect.Proxy.invoke(Proxy.java:393) 
    at $Proxy4.retrieve(Unknown Source) 
    at com.package.MainActivity.onResume(MainActivity.java:236) 
    at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1258) 
    at android.app.Activity.performResume(Activity.java:6312) 
    at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3092) 
    at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3134) 
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2481) 
    at android.app.ActivityThread.-wrap11(ActivityThread.java) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
    at android.os.Handler.dispatchMessage(Handler.java:102) 
    at android.os.Looper.loop(Looper.java:148) 
    at android.app.ActivityThread.main(ActivityThread.java:5417) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 

User.java (relevant part)

public class User  implements Serializable {
private static final long serialVersionUID = 1L;

private int id=-1;
private String gid;
private int roleId;
private String email;
private String displayname;

public User(int id, String gid, String email, int roleId){
    super();
    this.id = id;
    this.gid = gid;
    this.email = email;
    this.roleId = roleId;
}
public User(String displayName){
    super();
    this.displayname = displayName;
}
public User(){
}

UserResource.java

public interface UserResource {

    @Get //("xml|json|x-java-serialized-object|x-java-serialized-object+xml")
    public User retrieve();

    @Put
    public int registerUser(Representation jsonStr);

    @Delete
    public void remove();

}

UserServerResource.java (relevant part)

@Override
    public User retrieve() {
        // TODO Auto-generated method stub
//      UserServerResourceImpl impl = new UserServerResourceImpl();
//        return impl.retrieveUser(user);
        User user = new User("Hardocoded");
        return user;
    }

Android client

    StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
    StrictMode.setThreadPolicy(policy);
    Engine.getInstance().getRegisteredConverters().add(new JacksonConverter());

    ClientResource cr = new ClientResource("http://localhost:8111/user");

    cr.setRequestEntityBuffering(true);
    cr.accept(MediaType.APPLICATION_JSON);

    UserResource ucr = cr.wrap(UserResource.class);
    User user = ucr.retrieve();
Junaid
  • 1,606
  • 7
  • 29
  • 48

1 Answers1

2

I enabled following tags and used correct dependencies.

 @Get ("xml|json|x-java-serialized-object|x-java-serialized-object+xml")

The jackson dependencies I was using didn't have this method. I added 2.8.0 but it didn't work. I added 7.5 and it worked. Following are the ones I added and forced gradle not to use them from cache.

compile files('libs/jackson-databind-2.7.5.jar')
compile files('libs/jackson-core-2.7.5.jar')
compile files('libs/jackson-annotations-2.7.5.jar')
Junaid
  • 1,606
  • 7
  • 29
  • 48