Skip to content

Commit

Permalink
fix PersistentCookieStore
Browse files Browse the repository at this point in the history
  • Loading branch information
hongyangAndroid committed Apr 12, 2016
1 parent 990e811 commit ede7714
Show file tree
Hide file tree
Showing 11 changed files with 58 additions and 14 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@
.DS_Store
/build
/captures
/okhttp3*
1 change: 1 addition & 0 deletions .idea/gradle.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 25 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@
或者

```
compile 'com.zhy:okhttputils:2.3.8'
compile 'com.zhy:okhttputils:2.3.9'
```

* Eclipse

下载最新jar:[okhttputils-2_3_8.jar](okhttputils-2_3_8.jar?raw=true)
下载最新jar:[okhttputils-2_3_9.jar](okhttputils-2_3_9.jar?raw=true)

注:需要同时导入okhttp和okio的jar,下载见:[https://github.com/square/okhttp](https://github.com/square/okhttp).

Expand Down
Binary file added okhttputils-2_3_9.jar
Binary file not shown.
4 changes: 2 additions & 2 deletions okhttputils/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ apply plugin: 'com.android.library'
apply plugin: 'com.github.dcendents.android-maven'
apply plugin: 'com.jfrog.bintray'

version = "2.3.8"
version = "2.3.9"

android {
compileSdkVersion 23
Expand Down Expand Up @@ -123,7 +123,7 @@ task makeJar(type: Copy) {
from('build/intermediates/bundles/release/')
into('build/outputs/')
include('classes.jar')
rename ('classes.jar', 'okhttputils-2_3_8.jar')
rename ('classes.jar', 'okhttputils-2_3_9.jar')
}

makeJar.dependsOn(clearJar, build)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
Expand Down Expand Up @@ -89,18 +90,12 @@ protected void add(HttpUrl uri, Cookie cookie)
{
String name = getCookieToken(cookie);

// Save cookie into local store, or remove if expired
if (!cookie.persistent())
if (cookie.persistent())
{
if (!cookies.containsKey(uri.host()))
cookies.put(uri.host(), new ConcurrentHashMap<String, Cookie>());
cookies.get(uri.host()).put(name, cookie);
} else
{
if (cookies.containsKey(uri.toString()))
cookies.get(uri.host()).remove(name);
}

// Save cookie into persistent store
SharedPreferences.Editor prefsWriter = cookiePrefs.edit();
prefsWriter.putString(uri.host(), TextUtils.join(",", cookies.get(uri.host()).keySet()));
Expand All @@ -127,10 +122,28 @@ public List<Cookie> get(HttpUrl uri)
{
ArrayList<Cookie> ret = new ArrayList<Cookie>();
if (cookies.containsKey(uri.host()))
ret.addAll(cookies.get(uri.host()).values());
{
Collection<Cookie> cookies = this.cookies.get(uri.host()).values();
for (Cookie cookie : cookies)
{
if (isCookieExpired(cookie))
{
remove(uri, cookie);
} else
{
ret.add(cookie);
}
}
}

return ret;
}

private static boolean isCookieExpired(Cookie cookie)
{
return cookie.expiresAt() < System.currentTimeMillis();
}

@Override
public boolean removeAll()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ public void onAfter()
@Override
public void onError(Call call, Exception e)
{
e.printStackTrace();
mTv.setText("onError:" + e.getMessage());
}

Expand Down Expand Up @@ -91,9 +92,11 @@ protected void onCreate(Bundle savedInstanceState)
public void getHtml(View view)
{
String url = "http://sec.mobile.tiancity.com/server/mobilesecurity/version.xml";
// url="http://www.391k.com/api/xapi.ashx/info.json?key=bd_hyrzjjfb4modhj&size=10&page=1";
OkHttpUtils
.get()
.url(url)
// .addHeader("Accept-Encoding","")
.build()
.execute(new MyStringCallback());

Expand Down
2 changes: 1 addition & 1 deletion settings.gradle
Original file line number Diff line number Diff line change
@@ -1 +1 @@
include ':okhttputils', ':sample-okhttp'
include ':okhttputils', ':sample-okhttp', ':okhttp3-persistent-cookiejar'

0 comments on commit ede7714

Please sign in to comment.