2014-04-06 2 views
0

Android 응용 프로그램에서 POST 요청을 보내려고하고 있는데 MediaType 충돌 오류가 발생하거나 적어도 생각한 것입니다. 요청과 함께 일부 데이터를 보내고 싶지만 작동해야하지만 뭔가 빠뜨린 것 같습니다. Wildfly 서버와 함께 Jackson과 RestEasy를 사용하고 있습니다.안드로이드 응용 프로그램에서 게시물 요청이 실패했습니다. + RestEasy + Jackson

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) 

: 내 클라이언트에서 오류 로그

여기
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(); 
     } 
    } 

입니다 : 여기

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() 방법 : 여기

내 서버에서 내 오류 로그입니다 EDIT - 서버 측 코드 :

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(); 
     } 
    } 
+0

서버 측 코드를 게시하십시오. – rubenlop

+0

나는 그것을 배치했다. GET 메서드가 제대로 작동한다고 말하고 싶습니다. – Ilhami

+0

GET은 아무 것도 소비하지 않고'application/json'을 생성하기 때문에 잘 작동합니다. – rubenlop

답변

1
클래스 수준으로 @Produces("application/json; charset-UTF-8")@Consumes("application/json; charset-UTF-8") 주석을 이동하거나 두 가지 방법 모두 주석을 넣어

. RESTEasy 기본값에 대한 ResponseinsertPost 구문을 시도하고 application/octet-streamProvider 수행 할 수 없기 때문에 오류가 발생합니다.

또한 Android 프로젝트의 클라이언트 라이브러리 (예 : Retrofit) 사용을 고려해야합니다. 코드를 훨씬 깔끔하게 만듭니다.

+0

감사합니다. :) 둘 다 주석 수준을 클래스 수준에 넣을 때 작동했습니다. – Ilhami

관련 문제