首页 > 解决方案 > 快速 Android 网络 cookie 问题

问题描述

我在我的 Android 项目中使用Fast Android Networking,我想在其中使用 Cookie。当我在没有 Cookies 类的情况下设置库时,请求正常并且工作顺利,但是当我添加自定义 Cookies 处理程序时,请求突然停止工作,没有抛出任何错误,也没有做任何其他事情。

可能是什么问题?我使用的代码如下:

CookieJar cookieJar = new CookieJar()
    {
        @Override
        public void saveFromResponse(HttpUrl url, List<Cookie> cookies)
        {
            appCtx.setCookies(url.host(), cookies); //custom storage based on SharedPrefs
        }

        @Override
        public List<Cookie> loadForRequest(HttpUrl url)
        {
            List<Cookie> cookieList = appCtx.getCookies(url.host());
            return cookieList;
        }
    };

//if I use the below, the client hangs without doing anything
OkHttpClient httpClient = new OkHttpClient().newBuilder()
    .cookieJar(cookieJar)
    .build();
AndroidNetworking.initialize(ctx, httpClient);

//if I skip the above and simply apply the below, requests go through smoothly. 
// Note, am only using one at a time, and commenting out the other
AndroidNetworking.initialize(ctx);

//then continue as usual
AndroidNetworking.setParserFactory(new JacksonParserFactory());
... //other code

标签: androidandroid-networking

解决方案


推荐阅读