gpt4 book ai didi

java - ReSTLet 表示错误 422

转载 作者:太空宇宙 更新时间:2023-11-04 12:26:16 26 4
gpt4 key购买 nike

我收到此表示错误。有人可以帮忙修复吗?

错误:

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

最佳答案

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

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

我使用的 Jackson 依赖项没有此方法。我添加了2.8.0但没有用。我添加了7.5,它起作用了。以下是我添加的并强制 gradle 不要从缓存中使用它们。

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

关于java - ReSTLet 表示错误 422,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38363143/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com