1
I have a problem sending an object from a java class to my web service. Using the Restfull web service on glassfish and android using Volley and gson. I get a 500 error when I try to send the object.
Logcat log on android:
04-27 19:24:14.635: I/JSONOBJECT ADD VENDA->(3027): {"Venda":"model.Venda@52dfabc4"}
04-27 19:24:14.695: I/qtaguid(3027): Failed write_ctrl(u 44) res=-1 errno=22
04-27 19:24:14.695: I/qtaguid(3027): Untagging socket 44 failed errno=-22
04-27 19:24:14.695: W/NetworkManagementSocketTagger(3027): untagSocket(44) failed with errno -22
04-27 19:24:14.695: E/Volley(3027): [154] BasicNetwork.performRequest: Unexpected response code 500 for http://10.67.95.223:8080/BMSystem/webresources/venda/realizarvenda/
04-27 19:24:14.695: D/AndroidRuntime(3027): Shutting down VM
04-27 19:24:14.695: W/dalvikvm(3027): threadid=1: thread exiting with uncaught exception (group=0xa4bbe648)
04-27 19:24:14.695: E/AndroidRuntime(3027): FATAL EXCEPTION: main
04-27 19:24:14.695: E/AndroidRuntime(3027): java.lang.NullPointerException: println needs a message
04-27 19:24:14.695: E/AndroidRuntime(3027): at android.util.Log.println_native(Native Method)
04-27 19:24:14.695: E/AndroidRuntime(3027): at android.util.Log.e(Log.java:231)
04-27 19:24:14.695: E/AndroidRuntime(3027): at br.com.appbmsystem.MainActivityInterno$3.onErrorResponse(MainActivityInterno.java:421)
04-27 19:24:14.695: E/AndroidRuntime(3027): at com.android.volley.Request.deliverError(Request.java:563)
04-27 19:24:14.695: E/AndroidRuntime(3027): at com.android.volley.ExecutorDelivery$ResponseDeliveryRunnable.run(ExecutorDelivery.java:101)
04-27 19:24:14.695: E/AndroidRuntime(3027): at android.os.Handler.handleCallback(Handler.java:730)
04-27 19:24:14.695: E/AndroidRuntime(3027): at android.os.Handler.dispatchMessage(Handler.java:92)
04-27 19:24:14.695: E/AndroidRuntime(3027): at android.os.Looper.loop(Looper.java:137)
04-27 19:24:14.695: E/AndroidRuntime(3027): at android.app.ActivityThread.main(ActivityThread.java:5103)
04-27 19:24:14.695: E/AndroidRuntime(3027): at java.lang.reflect.Method.invokeNative(Native Method)
04-27 19:24:14.695: E/AndroidRuntime(3027): at java.lang.reflect.Method.invoke(Method.java:525)
04-27 19:24:14.695: E/AndroidRuntime(3027): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
04-27 19:24:14.695: E/AndroidRuntime(3027): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
04-27 19:24:14.695: E/AndroidRuntime(3027): at dalvik.system.NativeStart.main(Native Method)
Log from the Glassfish Server:
Advertência: StandardWrapperValve[org.netbeans.rest.application.config.ApplicationConfig]: Servlet.service() for servlet org.netbeans.rest.application.config.ApplicationConfig threw exception
java.lang.NullPointerException
at com.sun.common.util.logging.LoggingOutputStream$LoggingPrintStream.println(LoggingOutputStream.java:228)
at org.apache.felix.gogo.runtime.threadio.ThreadPrintStream.println(ThreadPrintStream.java:205)
at service.VendaService.realizarVenda(VendaService.java:58)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:483)
at org.glassfish.jersey.server.model.internal.ResourceMethodInvocationHandlerFactory$1.invoke(ResourceMethodInvocationHandlerFactory.java:81)
at org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher$1.run(AbstractJavaResourceMethodDispatcher.java:151)
at org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher.invoke(AbstractJavaResourceMethodDispatcher.java:171)
at org.glassfish.jersey.server.model.internal.JavaResourceMethodDispatcherProvider$TypeOutInvoker.doDispatch(JavaResourceMethodDispatcherProvider.java:195)
at org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher.dispatch(AbstractJavaResourceMethodDispatcher.java:104)
at org.glassfish.jersey.server.model.ResourceMethodInvoker.invoke(ResourceMethodInvoker.java:387)
at org.glassfish.jersey.server.model.ResourceMethodInvoker.apply(ResourceMethodInvoker.java:331)
at org.glassfish.jersey.server.model.ResourceMethodInvoker.apply(ResourceMethodInvoker.java:103)
at org.glassfish.jersey.server.ServerRuntime$1.run(ServerRuntime.java:271)
at org.glassfish.jersey.internal.Errors$1.call(Errors.java:271)
at org.glassfish.jersey.internal.Errors$1.call(Errors.java:267)
at org.glassfish.jersey.internal.Errors.process(Errors.java:315)
at org.glassfish.jersey.internal.Errors.process(Errors.java:297)
at org.glassfish.jersey.internal.Errors.process(Errors.java:267)
at org.glassfish.jersey.process.internal.RequestScope.runInScope(RequestScope.java:297)
at org.glassfish.jersey.server.ServerRuntime.process(ServerRuntime.java:254)
at org.glassfish.jersey.server.ApplicationHandler.handle(ApplicationHandler.java:1028)
at org.glassfish.jersey.servlet.WebComponent.service(WebComponent.java:372)
at org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:381)
at org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:344)
at org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:221)
at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1682)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:344)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:214)
at org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:393)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:256)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:214)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:316)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:160)
at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:734)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:673)
at com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:99)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:174)
at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:415)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:282)
at com.sun.enterprise.v3.services.impl.ContainerMapper$HttpHandlerCallable.call(ContainerMapper.java:459)
at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:167)
at org.glassfish.grizzly.http.server.HttpHandler.runService(HttpHandler.java:201)
at org.glassfish.grizzly.http.server.HttpHandler.doHandle(HttpHandler.java:175)
at org.glassfish.grizzly.http.server.HttpServerFilter.handleRead(HttpServerFilter.java:235)
at org.glassfish.grizzly.filterchain.ExecutorResolver$9.execute(ExecutorResolver.java:119)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeFilter(DefaultFilterChain.java:284)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeChainPart(DefaultFilterChain.java:201)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.execute(DefaultFilterChain.java:133)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.process(DefaultFilterChain.java:112)
at org.glassfish.grizzly.ProcessorExecutor.execute(ProcessorExecutor.java:77)
at org.glassfish.grizzly.nio.transport.TCPNIOTransport.fireIOEvent(TCPNIOTransport.java:561)
at org.glassfish.grizzly.strategies.AbstractIOStrategy.fireIOEvent(AbstractIOStrategy.java:112)
at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.run0(WorkerThreadIOStrategy.java:117)
at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.access$100(WorkerThreadIOStrategy.java:56)
at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy$WorkerThreadRunnable.run(WorkerThreadIOStrategy.java:137)
at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:565)
at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.run(AbstractThreadPool.java:545)
at java.lang.Thread.run(Thread.java:745)
Function of the web service:
@POST
@Produces(MediaType.APPLICATION_JSON + ";charset=utf-8")
@Consumes(MediaType.APPLICATION_JSON + ";charset=utf-8")
@Path("realizarvenda")
public Venda realizarVenda(@PathParam("Venda")JSONObject json) {
System.out.println(json);
Venda venda = new Venda();
Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd'T'HH:mm:ss-HH:ss").create();
VendaDAO vendDAO = new VendaDAO();
venda = vendDAO.cadastrarMobile(gson.fromJson(json.toString(), Venda.class));
return venda;
}
Sending function on android:
public static void enviarVenda() {
JSONObject jsObject = new JSONObject();
try {
jsObject.put("Venda", vend);
Log.i("JSONOBJECT ADD VENDA->", jsObject.toString());
} catch (JSONException e) {
Log.e("JSONException VENDA->", e.getLocalizedMessage());
}
url = "http://10.67.95.223:8080/BMSystem/webresources/venda/realizarvenda/";
JsonObjectRequest app = new JsonObjectRequest(Request.Method.POST, url,
jsObject, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
Log.i("RESPOSTA DE VENDA->", response.toString());
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.e("ERROR->", error.getLocalizedMessage());
}
});
rq.add(app);
}
Amendment: Android log
04-28 17:15:42.769: I/JSONOBJECT ADD VENDA->(1450): {"Venda":"model.Venda@52df4208"}
04-28 17:15:42.893: D/dalvikvm(1450): GC_FOR_ALLOC freed 1201K, 6% free 21710K/22968K, paused 3ms, total 3ms
04-28 17:15:42.929: I/dalvikvm(1450): threadid=1: stack overflow on call to Ljava/text/FieldPosition;.getField:I
04-28 17:15:42.929: I/dalvikvm(1450): method requires 8+20+0=28 bytes, fp is 0x9ec43308 (8 left)
04-28 17:15:42.929: I/dalvikvm(1450): expanding stack end (0x9ec43300 to 0x9ec43000)
04-28 17:15:42.929: I/dalvikvm(1450): Shrank stack (to 0x9ec43300, curFrame is 0x9ec43544)
04-28 17:15:42.929: D/AndroidRuntime(1450): Shutting down VM
04-28 17:15:42.929: W/dalvikvm(1450): threadid=1: thread exiting with uncaught exception (group=0xa4bb3648)
04-28 17:15:42.953: D/dalvikvm(1450): GC_FOR_ALLOC freed 1607K, 8% free 21786K/23504K, paused 4ms, total 4ms
04-28 17:15:42.953: E/AndroidRuntime(1450): FATAL EXCEPTION: main
04-28 17:15:42.953: E/AndroidRuntime(1450): java.lang.StackOverflowError
04-28 17:15:42.953: E/AndroidRuntime(1450): at libcore.icu.NativeDecimalFormat$FieldPositionIterator.forFieldPosition(NativeDecimalFormat.java:518)
04-28 17:15:42.953: E/AndroidRuntime(1450): at libcore.icu.NativeDecimalFormat.formatLong(NativeDecimalFormat.java:252)
04-28 17:15:42.953: E/AndroidRuntime(1450): at java.text.DecimalFormat.format(DecimalFormat.java:684)
04-28 17:15:42.953: E/AndroidRuntime(1450): at java.text.NumberFormat.format(NumberFormat.java:299)
04-28 17:15:42.953: E/AndroidRuntime(1450): at java.text.DecimalFormat.format(DecimalFormat.java:702)
04-28 17:15:42.953: E/AndroidRuntime(1450): at java.text.SimpleDateFormat.appendNumber(SimpleDateFormat.java:794)
04-28 17:15:42.953: E/AndroidRuntime(1450): at java.text.SimpleDateFormat.append(SimpleDateFormat.java:601)
04-28 17:15:42.953: E/AndroidRuntime(1450): at java.text.SimpleDateFormat.formatImpl(SimpleDateFormat.java:562)
04-28 17:15:42.953: E/AndroidRuntime(1450): at java.text.SimpleDateFormat.format(SimpleDateFormat.java:827)
04-28 17:15:42.953: E/AndroidRuntime(1450): at java.text.DateFormat.format(DateFormat.java:307)
04-28 17:15:42.953: E/AndroidRuntime(1450): at com.google.gson.DefaultDateTypeAdapter.serialize(DefaultDateTypeAdapter.java:72)
04-28 17:15:42.953: E/AndroidRuntime(1450): at com.google.gson.DefaultDateTypeAdapter.serialize(DefaultDateTypeAdapter.java:35)
04-28 17:15:42.953: E/AndroidRuntime(1450): at com.google.gson.TreeTypeAdapter.write(TreeTypeAdapter.java:70)
04-28 17:15:42.953: E/AndroidRuntime(1450): at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.write(TypeAdapterRuntimeTypeWrapper.java:68)
04-28 17:15:42.953: E/AndroidRuntime(1450): at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.write(ReflectiveTypeAdapterFactory.java:89)
04-28 17:15:42.953: E/AndroidRuntime(1450): at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.write(ReflectiveTypeAdapterFactory.java:195)
04-28 17:15:42.953: E/AndroidRuntime(1450): at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.write(TypeAdapterRuntimeTypeWrapper.java:68)
04-28 17:15:42.953: E/AndroidRuntime(1450): at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.write(ReflectiveTypeAdapterFactory.java:89)
04-28 17:15:42.953: E/AndroidRuntime(1450): at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.write(ReflectiveTypeAdapterFactory.java:195)
04-28 17:15:42.953: E/AndroidRuntime(1450): at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.write(TypeAdapterRuntimeTypeWrapper.java:68)
04-28 17:15:42.953: E/AndroidRuntime(1450): at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.write(ReflectiveTypeAdapterFactory.java:89)
04-28 17:15:42.953: E/AndroidRuntime(1450): at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.write(ReflectiveTypeAdapterFactory.java:195)
04-28 17:15:42.953: E/AndroidRuntime(1450): at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.write(TypeAdapterRuntimeTypeWrapper.java:68)
04-28 17:15:42.953: E/AndroidRuntime(1450): at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.write(CollectionTypeAdapterFactory.java:96)
04-28 17:15:42.953: E/AndroidRuntime(1450): at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.write(CollectionTypeAdapterFactory.java:60)
04-28 17:15:42.953: E/AndroidRuntime(1450): at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.write(TypeAdapterRuntimeTypeWrapper.java:68)
04-28 17:15:42.953: E/AndroidRuntime(1450): at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.write(ReflectiveTypeAdapterFactory.java:89)
04-28 17:15:42.953: E/AndroidRuntime(1450): at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.write(ReflectiveTypeAdapterFactory.java:195)
04-28 17:15:42.953: E/AndroidRuntime(1450): at com.google.gson.Gson$FutureTypeAdapter.write(Gson.java:892)
04-28 17:15:42.953: E/AndroidRuntime(1450): at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.write(TypeAdapterRuntimeTypeWrapper.java:68)
04-28 17:15:42.953: E/AndroidRuntime(1450): at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.write(ReflectiveTypeAdapterFactory.java:89)
04-28 17:15:42.953: E/AndroidRuntime(1450): at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.write(ReflectiveTypeAdapterFactory.java:195)
04-28 17:15:42.953: E/AndroidRuntime(1450): at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.write(TypeAdapterRuntimeTypeWrapper.java:68)
04-28 17:15:42.953: E/AndroidRuntime(1450): at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.write(CollectionTypeAdapterFactory.java:96)
04-28 17:15:42.953: E/AndroidRuntime(1450): at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.write(CollectionTypeAdapterFactory.java:60)
04-28 17:15:42.953: E/AndroidRuntime(1450): at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.write(TypeAdapterRuntimeTypeWrapper.java:68)
04-28 17:15:42.953: E/AndroidRuntime(1450): at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.write(ReflectiveTypeAdapterFactory.java:89)
04-28 17:15:42.953: E/AndroidRuntime(1450): at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.write(ReflectiveTypeAdapterFactory.java:195)
04-28 17:15:42.953: E/AndroidRuntime(1450): at com.google.gson.Gson$FutureTypeAdapter.write(Gson.java:892)
04-28 17:15:42.953: E/AndroidRuntime(1450): at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.write(TypeAdapterRuntimeTypeWrapper.java:68)
04-28 17:15:42.953: E/AndroidRuntime(1450): at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.write(ReflectiveTypeAdapterFactory.java:89)
04-28 17:15:42.953: E/AndroidRuntime(1450): at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$
I also changed the sending function to:
public static void enviarVenda() throws JSONException {
Gson gson = new GsonBuilder()
.setDateFormat(
"yyyy-MM-dd'T'HH:mm:ss-HH:ss")
.create();
url = "http://192.168.2.154:8080/BMSystem/webresources/venda/realizarvenda/";
JsonObjectRequest app = new JsonObjectRequest(Request.Method.POST, url,
new JSONObject(gson.toJson(vend)), new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
Log.i("RESPOSTA DE VENDA->", response.toString());
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.e("ERROR->", error.getLocalizedMessage());
}
});
rq.add(app);
}
Web Service
@POST
@Produces(MediaType.APPLICATION_JSON + ";charset=utf-8")
@Consumes(MediaType.APPLICATION_JSON + ";charset=utf-8")
@Path("realizarvenda")
public Venda realizarVenda(JSONObject json) {
System.out.println(json);
Venda venda = new Venda();
Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd'T'HH:mm:ss-HH:ss").create();
VendaDAO vendDAO = new VendaDAO();
venda = vendDAO.cadastrarMobile(gson.fromJson(json.toString(), Venda.class));
return venda;
}
Server log:
NullPointerException
in classVendaService
, methodrealizarVenda
, line 58. By the log there is some null reference there. App log: you are passing some null reference pro log ononErrorResponse
, line 421. Without the code of these excerpts we cannot help more than this.– Bruno César
From what I’ve seen, I’ve also noticed that you’re receiving null, so far so good. In the application this onErrorResponse is of Volley implementation, is with error pq had no response from the server (web service), I believe that the problem is in sending the object or in the web service that is receiving wrong.
– Bruno Nicoletti
What is the line 58 of the method
realizarVenda
. TheJSONObject
is being "injected" correctly? Maybe it’s string there, or an objectVenda
same? If it is by another HTTP client, without being your android application, continues the error?– Bruno César
I did not test for another customer, because I have to send an entire object of sale, I tried by android pass the object, but when I put the type in the web service of receipt for "Sale sale" presented error. This is the first example of POST that I am using on the web service. In my app I am using almost everything with GET, but in this one POST is required.
– Bruno Nicoletti
How could I send a java object otherwise to the server?
– Bruno Nicoletti
So again, what’s the line 58? See if the error is no longer in the method parameter
realizarVenda
, make sure that JSON is being converted correctly to the parameter type. You can make the call by any HTTP client, such as DHC, Advanced REST Client, only send the representation of the object in JSON, as sameContent-Type
expected.– Bruno César
Line 58 is the "System.out.println(json)" where I check if something arrived.
– Bruno Nicoletti
Change the method signature for this:
public Venda realizarVenda(Venda json)
and tell me the result, please. You’re doing a post with no parameter in the URL, but waiting for the object in the URL, it doesn’t make sense.– Bruno César
ok, I’ll change and test, I’m not holding the system in hand now, but later put the result here. Thanks for the help man...
– Bruno Nicoletti
@Brunocésar I changed the code but I put as Jsonobject because the android sending is of this type and presented the error above, it seems now is on android...
– Bruno Nicoletti
it is possible to send the object without converting to json?
– Bruno Nicoletti