Java – Jersey Client API issues

Jersey Client API issues… here is a solution to the problem.

Jersey Client API issues

I’m writing an Android client that calls a web service. The service is written using the JAX-RS/Jersey API. I’m also trying to use the 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’ve tested the web service outside and it seems to work fine. When I try to call it from my application, I get a NullPointerException on 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
03-26 14:15:43.745: WARN/System.err(1060):     at com.sun.jersey.api.client.PartialRequestBuilder.type(
03-26 14:15:43.755: WARN/System.err(1060):     at com.sun.jersey.api.client.WebResource.type(
03-26 14:15:43.755: WARN/System.err(1060):     at com.era.external.era.ERAService.updateRestaurantFranchise(
03-26 14:15:43.755: WARN/System.err(1060):     at$4.onItemSelected(
03-26 14:15:43.755: WARN/System.err(1060):     at android.widget.AdapterView.fireOnSelected(
03-26 14:15:43.766: WARN/System.err(1060):     at android.widget.AdapterView.access$200(
03-26 14:15:43.766: WARN/System.err(1060):     at android.widget.AdapterView$
03-26 14:15:43.776: WARN/System.err(1060):     at android.os.Handler.handleCallback(
03-26 14:15:43.776: WARN/System.err(1060):     at android.os.Handler.dispatchMessage(
03-26 14:15:43.776: WARN/System.err(1060):     at android.os.Looper.loop(
03-26 14:15:43.786: WARN/System.err(1060):     at
03-26 14:15:43.786: WARN/System.err(1060):     at java.lang.reflect.Method.invokeNative(Native Method)
03-26 14:15:43.796: WARN/System.err(1060):     at java.lang.reflect.Method.invoke(
03-26 14:15:43.796: WARN/System.err(1060):     at$
03-26 14:15:43.796: WARN/System.err(1060):     at
03-26 14:15:43.806: WARN/System.err(1060):     at dalvik.system.NativeStart.main(Native Method)

Any ideas about what I’m doing wrong?


From the stack trace, this may be what happened

  • 80 in the ERAService line updateRestaurantFranchise method
  • Line 201 in the RestaurantActivity onItemSelected method

Related Problems and Solutions