用于为URI创建Web资源并将过滤器绑定到它以将cookie附加到请求的类
public class TolvenRestClIEnt { private static ClIEnt clIEnt; private static final ThreadLocal<String> instance = new ThreadLocal<String>(); static { ClIEntConfig config = new DefaultClIEntConfig(); clIEnt = ClIEnt.create(config); clIEnt.setFollowRedirects(true); } private ClIEnt getClIEnt() { return clIEnt; } public WebResource getResource(String path){ WebResource webResource = getClIEnt().resource(path); webResource.addFilter(new ClIEntFilter() { @OverrIDe public ClIEntResponse handle(ClIEntRequest clIEntRequest) throws ClIEntHandlerException { if (instance.get() != null) { clIEntRequest.getheaders().putSingle("cookie","SSOcookie=" + instance.get()); } ClIEntResponse clIEntResponse = getNext().handle(clIEntRequest); String cookiename = "SSOcookie"; System.out.println("cookies "+clIEntResponse.getcookies()); for (Newcookie newcookie : clIEntResponse.getcookies()) { if (cookiename.equals(newcookie.getname())) { String extendedSessionID = newcookie.getValue(); instance.set(extendedSessionID); break; } } return clIEntResponse; } }); return webResource; }
然后是一个示例代码来触发webservice调用
RestClIEnt clIEnt = new RestClIEnt(); MultivaluedMap<String,String> formData = new MultivaluedMAPImpl(); formData.add("username","demo"); formData.add("password","1234qwer"); formData.add("realm","tolven"); WebResource webResource = clIEnt.getResource("https://dev.example.com:8443/gatekeeper/rs/authenticate/login"); ClIEntResponse response = webResource.type(MediaType.APPliCATION_FORM_URLENCODED).post(ClIEntResponse.class,formData); //ClIEntResponse response = webResource.accept("text/HTML").get(ClIEntResponse.class); System.out.println("Here ....." + response.toString()); webResource = clIEnt.getResource("https://dev.example.com:8443/API/vestibule/accountList"); response = webResource.get(ClIEntResponse.class); Accounts accounts = response.getEntity(Accounts.class);
从eclipse运行时,此代码工作正常.但是当在androID模拟器中运行时,它无法从客户端响应中读取cookie
我得到以下异常
09-20 19:55:31.067: W/System.err(1172): java.lang.NullPointerException09-20 19:55:31.077: W/System.err(1172): at javax.ws.rs.core.Newcookie.valueOf(Newcookie.java:126)09-20 19:55:31.077: W/System.err(1172): at com.sun.jersey.API.clIEnt.ClIEntResponse.getcookies(ClIEntResponse.java:714)09-20 19:55:31.077: W/System.err(1172): at com.userexample.LoginActivityTolven.handle(LoginActivityTolven.java:71)09-20 19:55:31.088: W/System.err(1172): at com.sun.jersey.API.clIEnt.WebResource.handle(WebResource.java:680)09-20 19:55:31.088: W/System.err(1172): at com.sun.jersey.API.clIEnt.WebResource.access0(WebResource.java:74)09-20 19:55:31.097: W/System.err(1172): at com.sun.jersey.API.clIEnt.WebResource$Builder.post(WebResource.java:568)09-20 19:55:31.097: W/System.err(1172): at com.userexample.LoginActivityTolven.onClick(LoginActivityTolven.java:86)09-20 19:55:31.097: W/System.err(1172): at androID.vIEw.VIEw.performClick(VIEw.java:4240)09-20 19:55:31.097: W/System.err(1172): at androID.vIEw.VIEw$PerformClick.run(VIEw.java:17721)09-20 19:55:31.107: W/System.err(1172): at androID.os.Handler.handleCallback(Handler.java:730)09-20 19:55:31.107: W/System.err(1172): at androID.os.Handler.dispatchMessage(Handler.java:92)09-20 19:55:31.107: W/System.err(1172): at androID.os.Looper.loop(Looper.java:137)09-20 19:55:31.107: W/System.err(1172): at androID.app.ActivityThread.main(ActivityThread.java:5103)09-20 19:55:31.107: W/System.err(1172): at java.lang.reflect.Method.invokeNative(Native Method)09-20 19:55:31.117: W/System.err(1172): at java.lang.reflect.Method.invoke(Method.java:525)09-20 19:55:31.117: W/System.err(1172): at com.androID.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)09-20 19:55:31.117: W/System.err(1172): at com.androID.internal.os.ZygoteInit.main(ZygoteInit.java:553)09-20 19:55:31.117: W/System.err(1172): at dalvik.system.NativeStart.main(Native Method)
有没有人试过在AndroID上使用Jersey客户端并看到这个问题?
解决方法 看起来您正在使用Jersey 1.x,并且Newcookie中的JAX-RS运行时委托为空.如果找不到Jersey meta-inf / services文件,就会发生这种情况.
如果不重新打包meta-inf / services文件,AndroID重新打包过程可能是您的问题的根源,请务必检查.
一种方法是通过提供自己的ServiceIteratorProvider来覆盖服务定位算法.
见:
> a similar question here on SO
> a similar thread on Jersey mailing-list
作为参考,这里是示例代码:
import java.util.ArrayList;import java.util.HashMap;import java.util.Iterator;import com.sun.jersey.spi.service.ServiceFinder.ServiceIteratorProvIDer;public class YourOwnServiceIteratorProvIDer<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.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); } @SuppressWarnings("unchecked") @OverrIDe public Iterator<Class<T>> createClassIterator(Class<T> service,String servicename,ClassLoader loader,boolean ignoreOnClassNotFound) { String[] classesnames = SERVICES.get(servicename); 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; } @OverrIDe public Iterator<T> createIterator(Class<T> service,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(); }}
可以这样使用:
// ...ClIEnt clIEnt = ClIEnt.create();// ...ServiceFinder.setIteratorProvIDer(new YourOwnServiceIteratorProvIDer());// ...
HTH
总结以上是内存溢出为你收集整理的无法从Android上的Jersey clientresponse读取cookie全部内容,希望文章能够帮你解决无法从Android上的Jersey clientresponse读取cookie所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)