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

有些话、适合烂在心里 提交于 2019-12-11 19:54:29

问题


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>


来源:https://stackoverflow.com/questions/11996866/android-jersey-client-com-sun-jersey-api-client-clienthandlerexception

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!