Android Jersey Client com.sun.jersey.api.client.Cl

2019-09-06 14:55发布

问题:

I am using jersey client from my android app to connect to a web service. The version of android is 1.6 (api level 4)

I have referenced jersey-core-1.12.jar and jersey-client-1.12.jar libs.

When I call a request with MediaType.APPLICATION_FORM_URLENCODED (application/x-www-form-urlencoded) I am getting the following exception:

com.sun.jersey.api.client.ClientHandlerException: A message body writer for Java type, class com.sun.jersey.core.util.MultivaluedMapImpl, and MIME media type, application/x-www-form-urlencoded, was not found

Using the same code with JDK 1.6 update 04 - all works fine.

Here is the sample of my server code for the request:

@Path("/" + RequestNames.LOGIN)
public class Login {

    @POST
    @Consumes(MediaType.APPLICATION_FORM_URLENCODED)
    public Response login(
            @FormParam(RequestParams.USER_NAME_PARAM) String userName,
            @FormParam(RequestParams.PASSWORD_PARAM) String password) {
...

Here is the sample of my client code for the request:

MultivaluedMap<String, String> formData = new MultivaluedMapImpl();
        formData.add(RequestParams.USER_NAME_PARAM, userName);
        formData.add(RequestParams.PASSWORD_PARAM, password);
        ClientResponse response =
                service.path(REST_PATH).path(RequestNames.LOGIN).type(
                        MediaType.APPLICATION_FORM_URLENCODED).post(ClientResponse.class, formData);

回答1:

I had the same problem with the

A message body writer for Java type, class com.sun.jersey.core.util.MultivaluedMapImpl

error and it ended up to be a maven dependency issue. Instead of using the single jersey-* artifacts I included jersey-bundle and it worked. E.g.

    <dependency>
        <groupId>com.sun.jersey</groupId>
        <artifactId>jersey-bundle</artifactId>
        <version>1.14</version>
    </dependency>