Jersey Client API Problem
I am writing an Android client which calls a web service. The service is written using JAX-RS / Jersey API. I'm also trying to use Jersey-Client API on the android side.
Client client = Client.create();
WebResource webResource = client.resource(server + "/api/restaurant_info/update");
MultivaluedMap formData = new MultivaluedMapImpl();
formData.add("id", id);
formData.add("f", Boolean.toString(f));
formData.add("fId", fId);
ClientResponse response = webResource.type(MediaType.APPLICATION_FORM_URLENCODED).accept(MediaType.APPLICATION_JSON).post(ClientResponse.class, formData);
RestInfo restInfo = response.getEntity(RestInfo.class);
return restInfo;
I have tested the web service externally, and it seems to be working fine. When I try to call it from within my application, I'm getting a NullPointerException at the ClientResponse line.
03-26 14:15:43.735: WARN/System.err(1060): java.lang.NullPointerException
03-26 14:15:43.745: WARN/System.err(1060): at javax.ws.rs.core.MediaType.valueOf(MediaType.java:119)
03-26 14:15:43.745: WARN/System.err(1060): at com.sun.jersey.api.client.PartialRequestBuilder.type(PartialRequestBuilder.java:92)
03-26 14:15:43.755: WARN/System.err(1060): at com.sun.jersey.api.client.WebResource.type(WebResource.java:309)
03-26 14:15:43.755: WARN/System.err(1060): at com.era.external.era.ERAService.updateRestaurantFranchise(ERAService.java:80)
03-26 14:15:43.755: WARN/System.err(1060): at com.era.android.RestaurantActivity$4.onItemSelected(RestaurantActivity.java:201)
03-26 14:15:43.755: WARN/System.err(1060): at android.widget.AdapterView.fireOnSelected(AdapterView.java:856)
03-26 14:15:43.766: WARN/System.err(1060): at android.widget.AdapterView.access$200(AdapterView.java:41)
03-26 14:15:43.766: WARN/System.err(1060): at android.widget.AdapterView$SelectionNotifier.run(AdapterView.java:827)
03-26 14:15:43.776: WARN/System.err(1060): at android.os.Handler.handleCallback(Handler.java:587)
03-26 14:15:43.776: WARN/System.err(1060): at android.os.Handler.dispatchMessage(Handler.java:92)
03-26 14:15:43.776: WARN/System.err(1060): at android.os.Looper.loop(Looper.java:123)
03-26 14:15:43.786: WARN/System.err(1060): at android.app.ActivityThread.main(ActivityThread.java:3948)
03-26 14:15:43.786: WARN/System.err(1060): at java.lang.reflect.Me开发者_如何转开发thod.invokeNative(Native Method)
03-26 14:15:43.796: WARN/System.err(1060): at java.lang.reflect.Method.invoke(Method.java:521)
03-26 14:15:43.796: WARN/System.err(1060): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:782)
03-26 14:15:43.796: WARN/System.err(1060): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:540)
03-26 14:15:43.806: WARN/System.err(1060): at dalvik.system.NativeStart.main(Native Method)
Any ideas on what I'm doing wrong?
Judging by the stack trace, it's probably something happing at either
ERAService line
80 in theupdateRestaurantFranchise
methodRestaurantActivity
line 201 in theonItemSelected
method
Android has Dalvik Virtual Machine, not original JVM. I think this the reason why Jersey Client API does not work in Android OS. but I'am nog sure!
I am also a Jersey newbie, but I think that the reason your post code does not work is that formData must be in one of the types accepted. I'm not totally sure on the difference between the type and the accept commands in this contexts, but I am pretty sure that formData must be of one of the types that your invocation builder supports. E.g. if you serialised formData as a JSON object and set type/accept to JSON then this code would work. As it is it is probably calling some bodymethodwriter to serialise it into an acceptable type but formData doesnt have one so the method call returns null. In fact, it looks like its calling ValueOf to try to obtain the data out of formData for serialisation, but that returns null.
精彩评论