Jersey客户端在Android上抛出NullPointerException

时间:2012-01-21 00:02:24

标签: android nullpointerexception jersey

我正在尝试使用jersey-client库消耗原子进给,但没有太多运气。这是我正在使用的代码:

    Client client = Client.create();
    ClientResponse response = client.resource("http://192.168.1.65:9998/tweets").accept(MediaType.APPLICATION_ATOM_XML).get(ClientResponse.class);
    String responseString = response.getEntity(String.class);

最初导致我遇到此异常的原因是:

  

E / AndroidRuntime(13126):java.lang.NullPointerException   E / AndroidRuntime(13126):at javax.ws.rs.core.MediaType.valueOf(MediaType.java:119)   E / AndroidRuntime(13126):at> com.sun.jersey.api.client.ClientResponse.getType(ClientResponse.java:614)   E / AndroidRuntime(13126):at com.sun.jersey.api.client.ClientResponse.getEntity(ClientResponse.java:531)   E / AndroidRuntime(13126):at com.sun.jersey.api.client.ClientResponse.getEntity(ClientResponse.java:506)   E / AndroidRuntime(13126):at com.pivotallabs.tracker.TweetsService.onHandleIntent(TweetsService.java:55)   E / AndroidRuntime(13126):在android.app.IntentService $ ServiceHandler.handleMessage(IntentService.java:59)   E / AndroidRuntime(13126):在android.os.Handler.dispatchMessage(Handler.java:99)   E / AndroidRuntime(13126):在android.os.Looper.loop(Looper.java:130)   E / AndroidRuntime(13126):在android.os.HandlerThread.run(HandlerThread.java:60)

我尝试使用此问题的解决方案 - Jersey client in android with protocol buffer - 其中涉及编写自定义ServiceIteratorProvider并解决了第一个问题但随后创建了一个新问题,其中ServiceIteratorProvider正在寻找com的实现。 sun.jersey.client.proxy.ViewProxyProvider。

我创建了一个虚假的实现:

public class FakeViewProvider implements com.sun.jersey.client.proxy.ViewProxyProvider {

    public <T> ViewProxy<T> proxy(Client client, Class<T> tClass) {
        Class[] interfaces = new Class[] {tClass};
        return (ViewProxy<T>) Proxy.newProxyInstance(tClass.getClassLoader(), interfaces, new Delegator(interfaces, new Object[]{}));
    }
}

在调用Client.create之前插入它:

ServiceFinder.setIteratorProvider(new Buscador());

...但现在我得到了这个错误:

  

E / AndroidRuntime(12789):java.lang.NullPointerException   E / AndroidRuntime(12789):at com.sun.jersey.spi.service.ServiceFinder.toClassArray(ServiceFinder.java:595)   E / AndroidRuntime(12789):at com.sun.jersey.core.spi.component.ProviderServices.getServiceClasses(ProviderServices.java:318)   E / AndroidRuntime(12789):at com.sun.jersey.core.spi.component.ProviderServices.getServiceClasses(ProviderServices.java:311)   E / AndroidRuntime(12789):at com.sun.jersey.core.spi.component.ProviderServices.getServices(ProviderServices.java:159)   E / AndroidRuntime(12789):at com.sun.jersey.api.client.Client.init(Client.java:249)   E / AndroidRuntime(12789):at com.sun.jersey.api.client.Client.access $ 000(Client.java:118)   E / AndroidRuntime(12789):at com.sun.jersey.api.client.Client $ 1.f(Client.java:191)   E / AndroidRuntime(12789):at com.sun.jersey.api.client.Client $ 1.f(Client.java:187)   E / AndroidRuntime(12789):at com.sun.jersey.spi.inject.Errors.processWithErrors(Errors.java:193)   E / AndroidRuntime(12789):at com.sun.jersey.api.client.Client。(Client.java:187)   E / AndroidRuntime(12789):at com.sun.jersey.api.client.Client。(Client.java:159)   E / AndroidRuntime(12789):at com.sun.jersey.api.client.Client.create(Client.java:669)

我几乎陷入困境,如果有人有任何想法/解决了这个问题,那就太酷了!

干杯,马克

2 个答案:

答案 0 :(得分:0)

你应该将你的代码包装在中 if(response.getStatus() == Response.Status.OK && response.hasEntity()){...} 您确定请求是否返回非空响应?

答案 1 :(得分:0)

我在Asus Memopad和galaxyS4测试设备上进行测试时遇到了同样的问题。这就是我修复它的方法。

 Client client = Client.create();
ClientResponse response = client.resource("http://192.168.1.65:9998/tweets").accept(MediaType.APPLICATION_ATOM_XML).get(ClientResponse.class);

//change to this. pull the input stream from the response and use apache commons to form the input stream into a string
        StringWriter writer = new StringWriter();
    try {
        IOUtils.copy(response.getEntityInputStream(), writer, Charset.defaultCharset());
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    String theString = writer.toString();
    String output = theString;

你需要Apache commons http://commons.apache.org/proper/commons-io/download_io.cgi来实现这一点。我希望它有所帮助。