Restlet表示错误422

时间:2016-07-13 23:09:24

标签: java android jackson restlet restlet-2.3.1

我收到此代表错误。有人可以帮忙解决这个问题吗?

错误:

 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(相关部分)

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(相关部分)

@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客户端

    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();

1 个答案:

答案 0 :(得分:0)

我启用了以下标记并使用了正确的依赖项。

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')

我使用的杰克逊依赖没有这种方法。我添加了2.8.0,但它没有用。我添加了7.5并且它有效。以下是我添加的内容,强制gradle不要在缓存中使用它们。

iframe
相关问题