Android OkHttp:如何获取身份验证令牌,然后在后续调用中使用

时间:2016-11-30 05:24:11

标签: android okhttp auth-token

这应该很简单但我必须遗漏一些东西。所以有一个授权网址,我需要将用户名和密码传递给:

 OkHttpClient client = new OkHttpClient.Builder()
                    .cookieJar(new CookieJar() {
                        private final HashMap<HttpUrl, List<Cookie>> cookieStore = new HashMap<>();

                        @Override
                        public void saveFromResponse(HttpUrl url, List<Cookie> cookies) {
                            cookieStore.put(url, cookies);
                        }

                        @Override
                        public List<Cookie> loadForRequest(HttpUrl url) {
                            List<Cookie> cookies = cookieStore.get(url);
                            return cookies != null ? cookies : new ArrayList<Cookie>();
                        }
                    })
                    .build();

HttpUrl.Builder urlBuilder = HttpUrl.parse(AUTHTOKEN_URL).newBuilder();
                            urlBuilder.addQueryParameter("username", AUTHTOKEN_USERNAME);
                            urlBuilder.addQueryParameter("password", AUTHTOKEN_PASSWORD);
                            String url = urlBuilder.build().toString();

                            Request request = new Request.Builder()
                                    .url(url)
                                    .build();

                            Response response = client.newCall(request).execute();

当我致电AUTHTOKEN_URL时,结果为&#34; OK&#34;但是看起来似乎没有调用saveFromResponse,所以我认为有些事情是不正确的,因为当我对另一台依赖于此身份验证令牌的服务器进行下一次http调用时,它不会返回任何数据。

enter image description here

enter image description here

0 个答案:

没有答案