0

I would like to create a Jersey client in Android using protocol buffer. I am using the following libraries: jersey-client-1.8.jar jersey-core-1.8.jar protobuf-java-2.4.0a.jar

The code I have written:

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

The above code is running successfully as Java main() application but when I am running it on Android the responseGetMea() object is null (last line of code).

I am using the "application/x-protobuf" because in such way it was defined on server side. I have added the INTERNET permission in my Android application. I also checked the URL from Android browser and when I click it, it prompts me to input the user name and the password(the expected behaviour).

I really appreciate any help.

Regards, kalgik

kalgik
  • 1
  • 2

2 Answers2

1

An addition... When trying to POST with the version of Buscador at the previous link it complained with some errors. The new version of Buscador that works for POSTing is the following,

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

}

cpet
  • 11
  • 1
0

I employed the solution offered at,

java.lang.NullPointerException on Android

as proposed by

Lucas Ventura, Aug 25, 2010; 9:15am

and it worked like a charm.

[EDIT] Well, small comment/correction. Running in an HTC phone, caused the app to destroy/create when i minimised (not sure if this is normal) and re-opened. The solution proposed mandates that the Jersey client object and the ServiceFinder setting should occur in a static context. This should give a clue,

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

Otherwise, the class loading fix complains in a rather strange way. Hope this helps someone...

Cheers!

RivieraKid
  • 5,923
  • 4
  • 38
  • 47
cpet
  • 1
  • 1