java - Post request from Android application failing + RestEasy + Jackson -
i trying send post request android application , mediatype conflict error or @ least that's think. want send data request , should work think missing something. using jackson , resteasy come wildfly server.
here error log server:
15:18:20,790 warn [org.jboss.resteasy.core.exceptionhandler] (default task-6) failed executing post /posts/post: org.jboss.resteasy.core.nomessagebodywriterfoundfailure: not find messagebodywriter response object of type: domains.post of media type: application/octet-stream @ org.jboss.resteasy.core.serverresponsewriter.writenomapresponse(serverresponsewriter.java:67) [resteasy-jaxrs-3.0.6.final.jar:] @ org.jboss.resteasy.core.synchronousdispatcher.writeresponse(synchronousdispatcher.java:427) [resteasy-jaxrs-3.0.6.final.jar:] @ org.jboss.resteasy.core.synchronousdispatcher.invoke(synchronousdispatcher.java:376) [resteasy-jaxrs-3.0.6.final.jar:] @ org.jboss.resteasy.core.synchronousdispatcher.invoke(synchronousdispatcher.java:179) [resteasy-jaxrs-3.0.6.final.jar:] @ org.jboss.resteasy.plugins.server.servlet.servletcontainerdispatcher.service(servletcontainerdispatcher.java:220) [resteasy-jaxrs-3.0.6.final.jar:] @ org.jboss.resteasy.plugins.server.servlet.httpservletdispatcher.service(httpservletdispatcher.java:56) [resteasy-jaxrs-3.0.6.final.jar:] @ org.jboss.resteasy.plugins.server.servlet.httpservletdispatcher.service(httpservletdispatcher.java:51) [resteasy-jaxrs-3.0.6.final.jar:] @ javax.servlet.http.httpservlet.service(httpservlet.java:790) [jboss-servlet-api_3.1_spec-1.0.0.final.jar:1.0.0.final] @ io.undertow.servlet.handlers.servlethandler.handlerequest(servlethandler.java:85) [undertow-servlet-1.0.0.final.jar:1.0.0.final] @ io.undertow.servlet.handlers.security.servletsecurityrolehandler.handlerequest(servletsecurityrolehandler.java:61) [undertow-servlet-1.0.0.final.jar:1.0.0.final] @ io.undertow.servlet.handlers.servletdispatchinghandler.handlerequest(servletdispatchinghandler.java:36) [undertow-servlet-1.0.0.final.jar:1.0.0.final] @ org.wildfly.extension.undertow.security.securitycontextassociationhandler.handlerequest(securitycontextassociationhandler.java:78) @ io.undertow.server.handlers.predicatehandler.handlerequest(predicatehandler.java:25) [undertow-core-1.0.0.final.jar:1.0.0.final] @ io.undertow.servlet.handlers.security.sslinformationassociationhandler.handlerequest(sslinformationassociationhandler.java:113) [undertow-servlet-1.0.0.final.jar:1.0.0.final] @ io.undertow.security.handlers.authenticationcallhandler.handlerequest(authenticationcallhandler.java:52) [undertow-core-1.0.0.final.jar:1.0.0.final] @ io.undertow.security.handlers.abstractconfidentialityhandler.handlerequest(abstractconfidentialityhandler.java:45) [undertow-core-1.0.0.final.jar:1.0.0.final] @ io.undertow.servlet.handlers.security.servletconfidentialityconstrainthandler.handlerequest(servletconfidentialityconstrainthandler.java:61) [undertow-servlet-1.0.0.final.jar:1.0.0.final] @ io.undertow.servlet.handlers.security.cachedauthenticatedsessionhandler.handlerequest(cachedauthenticatedsessionhandler.java:70) [undertow-servlet-1.0.0.final.jar:1.0.0.final] @ io.undertow.security.handlers.securityinitialhandler.handlerequest(securityinitialhandler.java:76) [undertow-core-1.0.0.final.jar:1.0.0.final] @ io.undertow.server.handlers.predicatehandler.handlerequest(predicatehandler.java:25) [undertow-core-1.0.0.final.jar:1.0.0.final] @ org.wildfly.extension.undertow.security.jacc.jacccontextidhandler.handlerequest(jacccontextidhandler.java:61) @ io.undertow.server.handlers.predicatehandler.handlerequest(predicatehandler.java:25) [undertow-core-1.0.0.final.jar:1.0.0.final] @ io.undertow.server.handlers.predicatehandler.handlerequest(predicatehandler.java:25) [undertow-core-1.0.0.final.jar:1.0.0.final] @ io.undertow.servlet.handlers.servletinitialhandler.handlefirstrequest(servletinitialhandler.java:240) [undertow-servlet-1.0.0.final.jar:1.0.0.final] @ io.undertow.servlet.handlers.servletinitialhandler.dispatchrequest(servletinitialhandler.java:227) [undertow-servlet-1.0.0.final.jar:1.0.0.final] @ io.undertow.servlet.handlers.servletinitialhandler.access$000(servletinitialhandler.java:73) [undertow-servlet-1.0.0.final.jar:1.0.0.final] @ io.undertow.servlet.handlers.servletinitialhandler$1.handlerequest(servletinitialhandler.java:146) [undertow-servlet-1.0.0.final.jar:1.0.0.final] @ io.undertow.server.connectors.executeroothandler(connectors.java:168) [undertow-core-1.0.0.final.jar:1.0.0.final] @ io.undertow.server.httpserverexchange$1.run(httpserverexchange.java:687) [undertow-core-1.0.0.final.jar:1.0.0.final] @ java.util.concurrent.threadpoolexecutor.runworker(unknown source) [rt.jar:1.7.0_51] @ java.util.concurrent.threadpoolexecutor$worker.run(unknown source) [rt.jar:1.7.0_51] @ java.lang.thread.run(unknown source) [rt.jar:1.7.0_51]
here postrequest() method:
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 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(); } }
here error log client:
04-06 09:18:18.634: i/system.out(1479): sending 'post' request 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): @ libcore.net.http.httpurlconnectionimpl.getinputstream(httpurlconnectionimpl.java:186) 04-06 09:18:18.755: w/system.err(1479): @ services.jsonrequest.postrequest(jsonrequest.java:96) 04-06 09:18:18.755: w/system.err(1479): @ com.example.slidingmenu.addpostfragment$requestitemsservicetask.doinbackground(addpostfragment.java:65) 04-06 09:18:18.755: w/system.err(1479): @ com.example.slidingmenu.addpostfragment$requestitemsservicetask.doinbackground(addpostfragment.java:1) 04-06 09:18:18.827: w/system.err(1479): @ android.os.asynctask$2.call(asynctask.java:287) 04-06 09:18:18.827: w/system.err(1479): @ java.util.concurrent.futuretask.run(futuretask.java:234) 04-06 09:18:18.835: w/system.err(1479): @ android.os.asynctask$serialexecutor$1.run(asynctask.java:230) 04-06 09:18:18.867: w/system.err(1479): @ java.util.concurrent.threadpoolexecutor.runworker(threadpoolexecutor.java:1080) 04-06 09:18:18.935: w/system.err(1479): @ java.util.concurrent.threadpoolexecutor$worker.run(threadpoolexecutor.java:573) 04-06 09:18:18.935: w/system.err(1479): @ java.lang.thread.run(thread.java:841)
edit -- server side code:
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(); } }
and 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 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(); } }
move @produces("application/json; charset-utf-8")
, @consumes("application/json; charset-utf-8")
annotations class level or put both annotations in both methods. error occurs because resteasy default tries parse response
of insertpost
application/octet-stream
, not have provider
it.
also, should consider using client library in android project retrofit. make code cleaner.
Comments
Post a Comment