发布表单参数时,Jersey客户端无法在android上运行

时间:2013-09-20 00:15:02

标签: android jersey-client

这实际上不是一个问题,而是通过几个帖子在本网站上报告的问题的解决方案。自从我通过这个解决方案并找到解决方案后,我认为其他人可以节省时间。以下代码在从eclipse运行时工作正常,但失败并出现错误“com.sun.jersey.api.client.ClientHandlerException:Java类型的消息体编写器....”

MultivaluedMap<String, String> formData = new MultivaluedMapImpl();
    formData.add("username", "demo");
    formData.add("password", "1234qwer");
    WebResource webResource = client.getResource("https://dev.example.com:8443/authenticate/login");
    ClientResponse response = webResource.type(MediaType.APPLICATION_FORM_URLENCODED).post(ClientResponse.class,formData);

我找到了答案中提供的postlink的答案。

基本上,我们的想法是让Jersey通过服务查找器模式了解可用的Message reader / writer。示例如下所示

  

public class AndroidServiceIteratorProvider扩展ServiceIteratorProvider {

private static final String TAG = AndroidServiceIteratorProvider.class.getSimpleName();
private static final String MESSAGE = "Unable to load provider";

private static final HashMap<String, String[]> SERVICES = new HashMap<String, String[]>();

private static final String[] com_sun_jersey_spi_HeaderDelegateProvider = {
        "com.sun.jersey.core.impl.provider.header.MediaTypeProvider",
        "com.sun.jersey.core.impl.provider.header.StringProvider"
};

private static final String[] com_sun_jersey_spi_inject_InjectableProvider = { 
};

private static final String[] javax_ws_rs_ext_MessageBodyReader = {
        "com.sun.jersey.core.impl.provider.entity.StringProvider",
        "com.sun.jersey.core.impl.provider.entity.FormMultivaluedMapProvider",
        "com.sun.jersey.core.impl.provider.entity.ReaderProvider"
};

private static final String[] javax_ws_rs_ext_MessageBodyWriter = {
        "com.sun.jersey.core.impl.provider.entity.StringProvider",
        "com.sun.jersey.core.impl.provider.entity.FormMultivaluedMapProvider",
        "com.sun.jersey.core.impl.provider.entity.ReaderProvider"
};

static {
    SERVICES.put("com.sun.jersey.spi.HeaderDelegateProvider",
            com_sun_jersey_spi_HeaderDelegateProvider);
    SERVICES.put("com.sun.jersey.spi.inject.InjectableProvider",
            com_sun_jersey_spi_inject_InjectableProvider);
    SERVICES.put("javax.ws.rs.ext.MessageBodyReader",
            javax_ws_rs_ext_MessageBodyReader);
    SERVICES.put("javax.ws.rs.ext.MessageBodyWriter",
            javax_ws_rs_ext_MessageBodyWriter);
    SERVICES.put("jersey-client-components", new String[]{});
    SERVICES.put("com.sun.jersey.client.proxy.ViewProxyProvider", new String[]{});
}

@SuppressWarnings("unchecked")
@Override
public Iterator<Class<T>> createClassIterator(Class<T> service,
        String serviceName, ClassLoader loader,
        boolean ignoreOnClassNotFound) {

    String[] classesNames = SERVICES.get(serviceName);
    int length = classesNames.length;
    ArrayList<Class<T>> classes = new ArrayList<Class<T>>(length);
    for (int i = 0; i < length; i++) {
        try {
            classes.add((Class<T>) Class.forName(classesNames[i]));
        } catch (ClassNotFoundException e) {
            Log.v(TAG, MESSAGE,e);
        }
    }
    return classes.iterator();
}

@Override
public Iterator<T> createIterator(Class<T> service, String serviceName,
        ClassLoader loader, boolean ignoreOnClassNotFound) {

    String[] classesNames = SERVICES.get(serviceName);
    int length = classesNames.length;
    ArrayList<T> classes = new ArrayList<T>(length);
        for (int i = 0; i <length; i++) {
        try {
            classes.add(service.cast(Class.forName(classesNames[i])
                    .newInstance()));
        } catch (IllegalAccessException e) {
            Log.v(TAG, MESSAGE,e);
        } catch (InstantiationException e) {
            Log.v(TAG, MESSAGE,e);
        } catch (ClassNotFoundException e) {
            Log.v(TAG, MESSAGE,e);
        }
    }

    return classes.iterator();
}

}

注意在javax_ws_rs_ext_MessageBodyReader和javax_ws_rs_ext_MessageBodyWriter中声明的字符串参数。您需要将相应的读取器/写入器添加到字符串数组中,然后通过API调用将此提供程序与Jersey挂钩,如下所示

 ServiceFinder.setIteratorProvider(new AndroidServiceIteratorProvider()); 

可以在此处找到消息读者和作者列表

0 个答案:

没有答案