在android中的Jersey客户端与协议缓冲区

时间:2011-12-16 22:05:51

标签: android

我想使用协议缓冲区在Android中创建Jersey客户端。 我使用以下库: 新泽西州的客户 - 1.8.jar 球衣核心 - 1.8.jar 的protobuf-java的2.4.0a.jar

我写的代码:

import com.sun.jersey.api.client.ClientRequest;
import com.sun.jersey.api.client.ClientResponse;
import com.sun.jersey.api.client.WebResource;
import com.sun.jersey.api.client.WebResource.Builder;
import com.sun.jersey.api.client.filter.ClientFilter;
import com.sun.jersey.api.client.filter.HTTPBasicAuthFilter;

BaseRestClient client = BaseRestClient.create("", "");

 HTTPBasicAuthFilter authenticationFilter =
     new    HTTPBasicAuthFilter(username, password);

client.addFilter(authenticationFilter);
..........
..........
WebResource webResourceGetMea = client.resource(url);
webResourceGetMea = webResourceGetMea.path("/accounts").path("/login");
ClientResponse responseGetMea = webResourceGetMea.type("application/x-protobuf").get(ClientResponse.class);

上面的代码作为Java main()应用程序成功运行,但是当我在Android上运行它时,responseGetMea()对象为null(最后一行代码)。

我正在使用“application / x-protobuf”,因为它以这种方式在服务器端定义。 我在Android应用程序中添加了INTERNET权限。 我还检查了Android浏览器中的URL,当我点击它时,它会提示我输入用户名和密码(预期的行为)。

我真的很感激任何帮助。

此致 kalgik

2 个答案:

答案 0 :(得分:1)

另外一个...当尝试在上一个链接上使用Buscador版本进行POST时,它抱怨了一些错误。适用于POST的新版Buscador如下,

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;

import com.sun.jersey.spi.service.ServiceFinder;
import com.sun.jersey.spi.service.ServiceFinder.DefaultServiceIteratorProvider;
import com.sun.jersey.spi.service.ServiceFinder.ServiceIteratorProvider;

public class Buscador<T> extends ServiceIteratorProvider<T>
{
private static final HashMap<String, String[]> SERVICES = new HashMap<String, String[]>();

private static final String[] com_sun_jersey_spi_HeaderDelegateProvider = new String[] {
        "com.sun.jersey.core.impl.provider.header.LocaleProvider",
        "com.sun.jersey.core.impl.provider.header.EntityTagProvider",
        "com.sun.jersey.core.impl.provider.header.MediaTypeProvider",
        "com.sun.jersey.core.impl.provider.header.CacheControlProvider",
        "com.sun.jersey.core.impl.provider.header.NewCookieProvider",
        "com.sun.jersey.core.impl.provider.header.CookieProvider",
        "com.sun.jersey.core.impl.provider.header.URIProvider",
        "com.sun.jersey.core.impl.provider.header.DateProvider",
        "com.sun.jersey.core.impl.provider.header.StringProvider"

};
private static final String[] com_sun_jersey_spi_inject_InjectableProvider = new String[] {
        "com.sun.jersey.core.impl.provider.xml.SAXParserContextProvider",
        "com.sun.jersey.core.impl.provider.xml.XMLStreamReaderContextProvider",
        "com.sun.jersey.core.impl.provider.xml.DocumentBuilderFactoryProvider",
        "com.sun.jersey.core.impl.provider.xml.TransformerFactoryProvider"
};
private static final String[] javax_ws_rs_ext_MessageBodyReader = new String[] {
        "com.sun.jersey.core.impl.provider.entity.StringProvider",
        "com.sun.jersey.core.impl.provider.entity.ByteArrayProvider",
        "com.sun.jersey.core.impl.provider.entity.FileProvider",
        "com.sun.jersey.core.impl.provider.entity.InputStreamProvider",
        "com.sun.jersey.core.impl.provider.entity.DataSourceProvider",
        "com.sun.jersey.core.impl.provider.entity.RenderedImageProvider",
        "com.sun.jersey.core.impl.provider.entity.MimeMultipartProvider",
        "com.sun.jersey.core.impl.provider.entity.FormProvider",
        "com.sun.jersey.core.impl.provider.entity.FormMultivaluedMapProvider",
        "com.sun.jersey.core.impl.provider.entity.XMLRootElementProvider$App",
        "com.sun.jersey.core.impl.provider.entity.XMLRootElementProvider$Text",
        "com.sun.jersey.core.impl.provider.entity.XMLRootElementProvider$General",
        "com.sun.jersey.core.impl.provider.entity.XMLJAXBElementProvider$App",
        "com.sun.jersey.core.impl.provider.entity.XMLJAXBElementProvider$Text",
        "com.sun.jersey.core.impl.provider.entity.XMLJAXBElementProvider$General",
        "com.sun.jersey.core.impl.provider.entity.XMLListElementProvider$App",
        "com.sun.jersey.core.impl.provider.entity.XMLListElementProvider$Text",
        "com.sun.jersey.core.impl.provider.entity.XMLListElementProvider$General",
        "com.sun.jersey.core.impl.provider.entity.ReaderProvider",
        "com.sun.jersey.core.impl.provider.entity.DocumentProvider",
        "com.sun.jersey.core.impl.provider.entity.SourceProvider$StreamSourceReader",
        "com.sun.jersey.core.impl.provider.entity.SourceProvider$SAXSourceReader",
        "com.sun.jersey.core.impl.provider.entity.SourceProvider$DOMSourceReader",
        "com.sun.jersey.core.impl.provider.entity.XMLRootObjectProvider$App",
        "com.sun.jersey.core.impl.provider.entity.XMLRootObjectProvider$Text",
        "com.sun.jersey.core.impl.provider.entity.XMLRootObjectProvider$General",
        "com.sun.jersey.core.impl.provider.entity.EntityHolderReader"
};
private static final String[] javax_ws_rs_ext_MessageBodyWriter = new String[] {
        "com.sun.jersey.core.impl.provider.entity.StringProvider",
        "com.sun.jersey.core.impl.provider.entity.ByteArrayProvider",
        "com.sun.jersey.core.impl.provider.entity.FileProvider",
        "com.sun.jersey.core.impl.provider.entity.InputStreamProvider",
        "com.sun.jersey.core.impl.provider.entity.DataSourceProvider",
        "com.sun.jersey.core.impl.provider.entity.RenderedImageProvider",
        "com.sun.jersey.core.impl.provider.entity.MimeMultipartProvider",
        "com.sun.jersey.core.impl.provider.entity.FormProvider",
        "com.sun.jersey.core.impl.provider.entity.FormMultivaluedMapProvider",
        "com.sun.jersey.core.impl.provider.entity.XMLRootElementProvider$App",
        "com.sun.jersey.core.impl.provider.entity.XMLRootElementProvider$Text",
        "com.sun.jersey.core.impl.provider.entity.XMLRootElementProvider$General",
        "com.sun.jersey.core.impl.provider.entity.XMLJAXBElementProvider$App",
        "com.sun.jersey.core.impl.provider.entity.XMLJAXBElementProvider$Text",
        "com.sun.jersey.core.impl.provider.entity.XMLJAXBElementProvider$General",
        "com.sun.jersey.core.impl.provider.entity.XMLListElementProvider$App",
        "com.sun.jersey.core.impl.provider.entity.XMLListElementProvider$Text",
        "com.sun.jersey.core.impl.provider.entity.XMLListElementProvider$General",
        "com.sun.jersey.core.impl.provider.entity.ReaderProvider",
        "com.sun.jersey.core.impl.provider.entity.DocumentProvider",
        "com.sun.jersey.core.impl.provider.entity.StreamingOutputProvider",
        "com.sun.jersey.core.impl.provider.entity.SourceProvider$SourceWriter"
};

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);
}

DefaultServiceIteratorProvider defaultServiceIteratorProvider = new ServiceFinder.DefaultServiceIteratorProvider();

@SuppressWarnings("unchecked")
@Override
public Iterator<Class<T>> createClassIterator(Class<T> service, String serviceName,
    ClassLoader loader, boolean ignoreOnClassNotFound)
{
    String[] classesNames = SERVICES.get(serviceName);

    System.out.println("!!!!!!!!!!!! serviceName: " + serviceName + " !!!!!!!!!!!!!!!!!!!");

    if(classesNames==null)
    {
        return defaultServiceIteratorProvider.createClassIterator(service, serviceName, loader, ignoreOnClassNotFound);
    }

    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)
        {
            e.printStackTrace();
        }
    }
    //return null;
    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)
        {
            e.printStackTrace();
        } catch (InstantiationException e)
        {
            e.printStackTrace();
        } catch (ClassNotFoundException e)
        {
            e.printStackTrace();
        }
    }
    return classes.iterator();
}

}

答案 1 :(得分:0)

我使用了

提供的解决方案

java.lang.NullPointerException on Android

提出的

  

Lucas Ventura,2010年8月25日;上午9:15

它就像一个魅力。

[编辑] 好吧,小评论/纠正。在HTC手机中运行,导致应用程序在我最小化时不会破坏/创建(不确定这是否正常)并重新打开。建议的解决方案要求Jersey客户端对象和ServiceFinder设置应在静态上下文中进行。这应该给出一个线索,

private static final BaseRestClient client; 
static { 
    client = BaseRestClient.create("", ""); // just a helper class
    ServiceFinder.setIteratorProvider(new Buscador());
} 

否则,类加载修复以一种相当奇怪的方式抱怨。希望这有助于某人...

干杯!