gpt4 book ai didi

java - 来自 Android 应用程序的发布请求失败 + RestEasy + Jackson

转载 作者:行者123 更新时间:2023-12-01 13:10:38 25 4
gpt4 key购买 nike

我正在尝试从 Android 应用程序发送 POST 请求,但收到 MediaType 冲突错误,或者至少我是这么认为的。我想随请求发送一些数据,它应该可以工作,但我认为我遗漏了一些东西。我正在使用 Jackson 和 RestEasy,因为它们随 Wildfly 服务器一起提供。

这是我的服务器的错误日志:

15:18:20,790 WARN  [org.jboss.resteasy.core.ExceptionHandler] (default task-6) Failed executing POST /posts/post: org.jboss.resteasy.core.NoMessageBodyWriterFoundFailure: Could not find MessageBodyWriter for response object of type: domains.Post of media type: application/octet-stream
at org.jboss.resteasy.core.ServerResponseWriter.writeNomapResponse(ServerResponseWriter.java:67) [resteasy-jaxrs-3.0.6.Final.jar:]
at org.jboss.resteasy.core.SynchronousDispatcher.writeResponse(SynchronousDispatcher.java:427) [resteasy-jaxrs-3.0.6.Final.jar:]
at org.jboss.resteasy.core.SynchronousDispatcher.invoke(SynchronousDispatcher.java:376) [resteasy-jaxrs-3.0.6.Final.jar:]
at org.jboss.resteasy.core.SynchronousDispatcher.invoke(SynchronousDispatcher.java:179) [resteasy-jaxrs-3.0.6.Final.jar:]
at org.jboss.resteasy.plugins.server.servlet.ServletContainerDispatcher.service(ServletContainerDispatcher.java:220) [resteasy-jaxrs-3.0.6.Final.jar:]
at org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher.service(HttpServletDispatcher.java:56) [resteasy-jaxrs-3.0.6.Final.jar:]
at org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher.service(HttpServletDispatcher.java:51) [resteasy-jaxrs-3.0.6.Final.jar:]
at javax.servlet.http.HttpServlet.service(HttpServlet.java:790) [jboss-servlet-api_3.1_spec-1.0.0.Final.jar:1.0.0.Final]
at io.undertow.servlet.handlers.ServletHandler.handleRequest(ServletHandler.java:85) [undertow-servlet-1.0.0.Final.jar:1.0.0.Final]
at io.undertow.servlet.handlers.security.ServletSecurityRoleHandler.handleRequest(ServletSecurityRoleHandler.java:61) [undertow-servlet-1.0.0.Final.jar:1.0.0.Final]
at io.undertow.servlet.handlers.ServletDispatchingHandler.handleRequest(ServletDispatchingHandler.java:36) [undertow-servlet-1.0.0.Final.jar:1.0.0.Final]
at org.wildfly.extension.undertow.security.SecurityContextAssociationHandler.handleRequest(SecurityContextAssociationHandler.java:78)
at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:25) [undertow-core-1.0.0.Final.jar:1.0.0.Final]
at io.undertow.servlet.handlers.security.SSLInformationAssociationHandler.handleRequest(SSLInformationAssociationHandler.java:113) [undertow-servlet-1.0.0.Final.jar:1.0.0.Final]
at io.undertow.security.handlers.AuthenticationCallHandler.handleRequest(AuthenticationCallHandler.java:52) [undertow-core-1.0.0.Final.jar:1.0.0.Final]
at io.undertow.security.handlers.AbstractConfidentialityHandler.handleRequest(AbstractConfidentialityHandler.java:45) [undertow-core-1.0.0.Final.jar:1.0.0.Final]
at io.undertow.servlet.handlers.security.ServletConfidentialityConstraintHandler.handleRequest(ServletConfidentialityConstraintHandler.java:61) [undertow-servlet-1.0.0.Final.jar:1.0.0.Final]
at io.undertow.servlet.handlers.security.CachedAuthenticatedSessionHandler.handleRequest(CachedAuthenticatedSessionHandler.java:70) [undertow-servlet-1.0.0.Final.jar:1.0.0.Final]
at io.undertow.security.handlers.SecurityInitialHandler.handleRequest(SecurityInitialHandler.java:76) [undertow-core-1.0.0.Final.jar:1.0.0.Final]
at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:25) [undertow-core-1.0.0.Final.jar:1.0.0.Final]
at org.wildfly.extension.undertow.security.jacc.JACCContextIdHandler.handleRequest(JACCContextIdHandler.java:61)
at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:25) [undertow-core-1.0.0.Final.jar:1.0.0.Final]
at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:25) [undertow-core-1.0.0.Final.jar:1.0.0.Final]
at io.undertow.servlet.handlers.ServletInitialHandler.handleFirstRequest(ServletInitialHandler.java:240) [undertow-servlet-1.0.0.Final.jar:1.0.0.Final]
at io.undertow.servlet.handlers.ServletInitialHandler.dispatchRequest(ServletInitialHandler.java:227) [undertow-servlet-1.0.0.Final.jar:1.0.0.Final]
at io.undertow.servlet.handlers.ServletInitialHandler.access$000(ServletInitialHandler.java:73) [undertow-servlet-1.0.0.Final.jar:1.0.0.Final]
at io.undertow.servlet.handlers.ServletInitialHandler$1.handleRequest(ServletInitialHandler.java:146) [undertow-servlet-1.0.0.Final.jar:1.0.0.Final]
at io.undertow.server.Connectors.executeRootHandler(Connectors.java:168) [undertow-core-1.0.0.Final.jar:1.0.0.Final]
at io.undertow.server.HttpServerExchange$1.run(HttpServerExchange.java:687) [undertow-core-1.0.0.Final.jar:1.0.0.Final]
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source) [rt.jar:1.7.0_51]
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source) [rt.jar:1.7.0_51]
at java.lang.Thread.run(Unknown Source) [rt.jar:1.7.0_51]

这是我的 postRequest() 方法:

public static void postRequest(String serviceUrl, Post post) {
HttpURLConnection conn;

try {
URL url = new URL(serviceUrl);
conn = (HttpURLConnection)url.openConnection();
conn.setRequestMethod("POST");
conn.setRequestProperty("Content-Type", "application/json");
conn.setDoInput(true);

ObjectMapper mapper = new ObjectMapper();
DataOutputStream wr = new DataOutputStream(conn.getOutputStream());
mapper.writeValue(wr, post);

wr.flush();
wr.close();

int responseCode = conn.getResponseCode();
System.out.println("\nSending 'POST' request to url " + url);
System.out.println("Response Code: " + responseCode);

BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));

String inputLine;
StringBuffer response = new StringBuffer();

while((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();

System.out.println(response.toString());

} catch(MalformedURLException e) {
e.printStackTrace();
} catch(IOException e) {
e.printStackTrace();
}
}

这是我的客户的错误日志:

04-06 09:18:18.634: I/System.out(1479): Sending 'POST' request to url http://10.0.2.2:8080/IslamicPostsWS/rest/posts/post
04-06 09:18:18.634: I/System.out(1479): Response Code: 500
04-06 09:18:18.665: W/System.err(1479): java.io.FileNotFoundException: http://10.0.2.2:8080/IslamicPostsWS/rest/posts/post
04-06 09:18:18.755: W/System.err(1479): at libcore.net.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:186)
04-06 09:18:18.755: W/System.err(1479): at services.JSONRequest.postRequest(JSONRequest.java:96)
04-06 09:18:18.755: W/System.err(1479): at com.example.slidingmenu.AddPostFragment$RequestItemsServiceTask.doInBackground(AddPostFragment.java:65)
04-06 09:18:18.755: W/System.err(1479): at com.example.slidingmenu.AddPostFragment$RequestItemsServiceTask.doInBackground(AddPostFragment.java:1)
04-06 09:18:18.827: W/System.err(1479): at android.os.AsyncTask$2.call(AsyncTask.java:287)
04-06 09:18:18.827: W/System.err(1479): at java.util.concurrent.FutureTask.run(FutureTask.java:234)
04-06 09:18:18.835: W/System.err(1479): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
04-06 09:18:18.867: W/System.err(1479): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
04-06 09:18:18.935: W/System.err(1479): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
04-06 09:18:18.935: W/System.err(1479): at java.lang.Thread.run(Thread.java:841)

编辑 -- 服务器端代码:

package api;
import java.util.List;
import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Response;

import database_wrappers.PostWrapper;
import domains.Post;

@Path("/posts")
public class PostAPI {
PostWrapper postWrapper = new PostWrapper();

@GET
@Path("/get")
@Produces("application/json; charset=UTF-8")
public List<Post> getPosts() {
List<Post> posts = postWrapper.getPosts();
return posts;
}

@POST
@Path("/post")
@Consumes("application/json; charset-UTF-8")
public Response insertPost(Post post) {
postWrapper.insertPost(post);
return Response.status(200).entity(post).build();
}
}

还有我的 PostWrapper:

public void insertPost(Post post) {

try {
Context initContext = new InitialContext();
Context context = (Context) initContext.lookup("java:comp/env");

dataSource = (DataSource) context.lookup("jdbc/myJdbc");
con = dataSource.getConnection();
String query = "INSERT INTO islamic_posts (post_title, post_content) VALUES" + "(?, ?)";
stmt = con.prepareStatement(query);
stmt.setString(1, post.getPostTitle());
stmt.setString(2, post.getPostContent());
stmt.executeUpdate();

con.close();

} catch(SQLException e) {
e.printStackTrace();
} catch (NamingException e) {
e.printStackTrace();
}
}

最佳答案

移动 @Produces("application/json; charset-UTF-8")@Consumes("application/json; charset-UTF-8")将注释添加到类级别,或者将两个注释都放在两个方法中。发生错误的原因是默认情况下的 RESTEasy 尝试将 insertPostResponse 解析为 application/octet-stream 并且没有 提供者来做到这一点。

此外,您应该考虑在 Android 项目中使用客户端库,例如 Retrofit 。它将使您的代码更加简洁。

关于java - 来自 Android 应用程序的发布请求失败 + RestEasy + Jackson,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22895289/

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