Jersey 客户端 API 问题 [英] Jersey Client API Problem

查看:40
本文介绍了Jersey 客户端 API 问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个调用 Web 服务的 Android 客户端.该服务是使用 JAX-RS/Jersey API 编写的.我也在尝试在 android 端使用 Jersey-Client API.

I am writing an Android client which calls a web service. The service is written using JAX-RS / Jersey API. I'm also trying to use Jersey-Client API on the android side.

        Client client = Client.create();
        WebResource webResource = client.resource(server + "/api/restaurant_info/update");
        MultivaluedMap formData = new MultivaluedMapImpl();
        formData.add("id", id);
        formData.add("f", Boolean.toString(f));
        formData.add("fId", fId);
        ClientResponse response = webResource.type(MediaType.APPLICATION_FORM_URLENCODED).accept(MediaType.APPLICATION_JSON).post(ClientResponse.class, formData);
        RestInfo restInfo = response.getEntity(RestInfo.class);
        return restInfo;

我已经在外部测试了 Web 服务,它似乎工作正常.当我尝试从我的应用程序中调用它时,我在 ClientResponse 行收到 NullPointerException.

I have tested the web service externally, and it seems to be working fine. When I try to call it from within my application, I'm getting a NullPointerException at the ClientResponse line.

03-26 14:15:43.735: WARN/System.err(1060): java.lang.NullPointerException
03-26 14:15:43.745: WARN/System.err(1060):     at javax.ws.rs.core.MediaType.valueOf(MediaType.java:119)
03-26 14:15:43.745: WARN/System.err(1060):     at com.sun.jersey.api.client.PartialRequestBuilder.type(PartialRequestBuilder.java:92)
03-26 14:15:43.755: WARN/System.err(1060):     at com.sun.jersey.api.client.WebResource.type(WebResource.java:309)
03-26 14:15:43.755: WARN/System.err(1060):     at com.era.external.era.ERAService.updateRestaurantFranchise(ERAService.java:80)
03-26 14:15:43.755: WARN/System.err(1060):     at com.era.android.RestaurantActivity$4.onItemSelected(RestaurantActivity.java:201)
03-26 14:15:43.755: WARN/System.err(1060):     at android.widget.AdapterView.fireOnSelected(AdapterView.java:856)
03-26 14:15:43.766: WARN/System.err(1060):     at android.widget.AdapterView.access$200(AdapterView.java:41)
03-26 14:15:43.766: WARN/System.err(1060):     at android.widget.AdapterView$SelectionNotifier.run(AdapterView.java:827)
03-26 14:15:43.776: WARN/System.err(1060):     at android.os.Handler.handleCallback(Handler.java:587)
03-26 14:15:43.776: WARN/System.err(1060):     at android.os.Handler.dispatchMessage(Handler.java:92)
03-26 14:15:43.776: WARN/System.err(1060):     at android.os.Looper.loop(Looper.java:123)
03-26 14:15:43.786: WARN/System.err(1060):     at android.app.ActivityThread.main(ActivityThread.java:3948)
03-26 14:15:43.786: WARN/System.err(1060):     at java.lang.reflect.Method.invokeNative(Native Method)
03-26 14:15:43.796: WARN/System.err(1060):     at java.lang.reflect.Method.invoke(Method.java:521)
03-26 14:15:43.796: WARN/System.err(1060):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:782)
03-26 14:15:43.796: WARN/System.err(1060):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:540)
03-26 14:15:43.806: WARN/System.err(1060):     at dalvik.system.NativeStart.main(Native Method)

关于我做错了什么有什么想法吗?

Any ideas on what I'm doing wrong?

推荐答案

我也是泽西新手,但我认为您的邮政编码不起作用的原因是 formData 必须是接受的类型之一.我不完全确定在这种情况下类型和接受命令之间的区别,但我很确定 formData 必须是您的调用构建器支持的类型之一.例如.如果您将 formData 序列化为 JSON 对象并将 type/accept 设置为 JSON,那么此代码将起作用.因为它可能正在调用一些 bodymethodwriter 将其序列化为可接受的类型,但 formData 没有,因此方法调用返回 null.实际上,它看起来像是调用 ValueOf 试图从 formData 中获取数据进行序列化,但返回的是 null.

I am also a Jersey newbie, but I think that the reason your post code does not work is that formData must be in one of the types accepted. I'm not totally sure on the difference between the type and the accept commands in this contexts, but I am pretty sure that formData must be of one of the types that your invocation builder supports. E.g. if you serialised formData as a JSON object and set type/accept to JSON then this code would work. As it is it is probably calling some bodymethodwriter to serialise it into an acceptable type but formData doesnt have one so the method call returns null. In fact, it looks like its calling ValueOf to try to obtain the data out of formData for serialisation, but that returns null.

这篇关于Jersey 客户端 API 问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆