将 HttpUrlConnection 与凌空一起使用
Use HttpUrlConnection with volley
我到处都读到 volley 将使用 HttpUrlConnection 用于新版本 api 或 HttpClient 用于旧版本,我试图告诉 volley 只使用 HttpUrlConnection。
我的主要目标是设置 volley 以使用我存储的 cookie 执行请求,为此我知道我需要使用 cookie 设置 HttpUrlConnection,然后将其传递给 volley 以用作默认实现.
到目前为止一切顺利,但我不知道如何启动 HttpUrlConnection 并将 cookie 添加到其中。
谁能给我一个小例子,说明如何初始化 HttpUrlConnection 并向其添加 cookie,然后将其传递给 volley?
我能够在 HttpUrlConnection 本身上执行这样的请求并且它有效但我如何设置它以与 volley 一起使用?
URL urlLink = new URL(url2);
HttpURLConnection conenction = (HttpURLConnection)urlLink.openConnection();
conenction.setRequestProperty("Cookie", cookie);
I read everywhere that volley will use either HttpUrlConnection for newer versions api or HttpClient for older version and I'm trying to tell volley to only use the HttpUrlConnection.
这是正确的。请参阅 Volley
source 的第 54-60 行。如果您的应用在使用 Gingerbread(API 级别 9)或更高版本的设备上 运行,则它已在对所有请求使用 HttpUrlConnection
。
如果您真的想为您的请求使用自己的 HttpUrlConnection
实例,您将需要实现自己的 HttpStack
(请参阅 Volley 的 HurlStack
示例)。您可以使用 Volley# newRequestQueue(Context, HttpStack)
告诉 Volley 使用您的自定义堆栈。
然而,发送 cookie 有多种选择。我建议查看 this question 以了解其中一些替代方案。