Jersey 客户端在 Android 上抛出 NullPointerException

发布于 2024-12-28 12:54:24 字数 3421 浏览 0 评论 0原文

我正在尝试使用球衣客户端库来使用原子提要,但运气不佳。这是我正在使用的代码:

    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):在javax.ws.rs.core.MediaType.valueOf(MediaType.java:119) E/AndroidRuntime(13126):位于> com.sun.jersey.api.client.ClientResponse.getType(ClientResponse.java:614) E / AndroidRuntime(13126):在com.sun.jersey.api.client.ClientResponse.getEntity(ClientResponse.java:531) E / AndroidRuntime(13126):在com.sun.jersey.api.client.ClientResponse.getEntity(ClientResponse.java:506) E/AndroidRuntime(13126):位于 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)

我尝试使用这个问题的解决方案 - 带有协议缓冲区的 android 中的泽西客户端 - 其中涉及编写自定义 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):位于 com.sun.jersey.spi.service.ServiceFinder.toClassArray(ServiceFinder.java:595) E / AndroidRuntime(12789):在com.sun.jersey.core.spi.component.ProviderServices.getServiceClasses(ProviderServices.java:318) E / AndroidRuntime(12789):在com.sun.jersey.core.spi.component.ProviderServices.getServiceClasses(ProviderServices.java:311) E / AndroidRuntime(12789):在com.sun.jersey.core.spi.component.ProviderServices.getServices(ProviderServices.java:159) E / AndroidRuntime(12789):在com.sun.jersey.api.client.Client.init(Client.java:249) E/AndroidRuntime(12789):位于 com.sun.jersey.api.client.Client.access$000(Client.java:118) E/AndroidRuntime(12789):位于 com.sun.jersey.api.client.Client$1.f(Client.java:191) E/AndroidRuntime(12789):位于 com.sun.jersey.api.client.Client$1.f(Client.java:187) E / AndroidRuntime(12789):在com.sun.jersey.spi.inject.Errors.processWithErrors(Errors.java:193) E / AndroidRuntime(12789):在com.sun.jersey.api.client.Client。(Client.java:187) E / AndroidRuntime(12789):在com.sun.jersey.api.client.Client。(Client.java:159) E/AndroidRuntime(12789):位于 com.sun.jersey.api.client.Client.create(Client.java:669)

我几乎陷入困境,所以如果有人有任何想法/已经解决了这个问题那么那就是凉爽的!

干杯,马克

I'm trying to consume an atom feed using the jersey-client library but not having much luck. This is the code I'm using:

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

Which originally led to me getting this exception:

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): at android.app.IntentService$ServiceHandler.handleMessage(IntentService.java:59)
E/AndroidRuntime(13126): at android.os.Handler.dispatchMessage(Handler.java:99)
E/AndroidRuntime(13126): at android.os.Looper.loop(Looper.java:130)
E/AndroidRuntime(13126): at android.os.HandlerThread.run(HandlerThread.java:60)

I tried to use the solution from this question - Jersey client in android with protocol buffer - which involved writing a custom ServiceIteratorProvider and that got rid of the first problem but then created a new problem where the ServiceIteratorProvider was looking for an implementation of com.sun.jersey.client.proxy.ViewProxyProvider.

I created a fake implementation of that:

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[]{}));
    }
}

And plugged it in just before the call to Client.create:

ServiceFinder.setIteratorProvider(new Buscador());

...but now I get this error instead:

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)

I'm pretty much stuck so if anyone has any ideas/has solved this problem then that'd be cool!

Cheers, Mark

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

醉南桥 2025-01-04 12:54:24

你应该将你的代码包装在

if(response.getStatus() == Response.Status.OK && response.hasEntity()){...}
您确定请求返回非空响应吗?

you should wrap your code in
if(response.getStatus() == Response.Status.OK && response.hasEntity()){...}
Are you sure the request is returning non-empty response?

人│生佛魔见 2025-01-04 12:54:24

我在 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 才能正常工作。我希望它有帮助。

I ran into the same issue when testing on an Asus Memopad and a galaxyS4 test device. Here's how I fixed it.

 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;

you will need Apache commons http://commons.apache.org/proper/commons-io/download_io.cgi for this to work. I hope it helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文