Skip to content

Commit

Permalink
fix #237
Browse files Browse the repository at this point in the history
  • Loading branch information
hongyangAndroid committed Jun 20, 2016
1 parent b514bf9 commit e1ce7e5
Show file tree
Hide file tree
Showing 12 changed files with 76 additions and 50 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@
* Android Studio

```
compile 'com.zhy:okhttputils:2.6.1'
compile 'com.zhy:okhttputils:2.6.2'
```

* Eclipse

下载最新jar:[okhttputils-2\_6\_1.jar](okhttputils-2_6_1.jar?raw=true)
下载最新jar:[okhttputils-2\_6\_2.jar](okhttputils-2_6_2.jar?raw=true)

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

Expand Down
Binary file removed okhttputils-2_6_1.jar
Binary file not shown.
Binary file added okhttputils-2_6_2.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 @@ -34,7 +34,7 @@ task makeJar(type: Copy) {
from('build/intermediates/bundles/release/')
into('build/outputs/')
include('classes.jar')
rename ('classes.jar', 'okhttputils-2_6_1.jar')
rename ('classes.jar', 'okhttputils-2_6_2.jar')
}

makeJar.dependsOn(clearJar, build)
Expand All @@ -51,7 +51,7 @@ publish {
userOrg = 'hongyangandroid'//bintray.com用户名
groupId = 'com.zhy'//jcenter上的路径
artifactId = 'okhttputils'//项目名称
publishVersion = '2.6.1'//版本号
publishVersion = '2.6.2'//版本号
desc = 'Oh hi, this is a nice description for a project, right?'
website = 'https://github.com/hongyangAndroid/okhttp-utils'
}
Expand Down
16 changes: 8 additions & 8 deletions okhttputils/okhttputils.iml
Original file line number Diff line number Diff line change
Expand Up @@ -65,14 +65,6 @@
<sourceFolder url="file://$MODULE_DIR$/src/main/jni" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/src/main/rs" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/src/main/shaders" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/res" type="java-test-resource" />
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/resources" type="java-test-resource" />
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/assets" type="java-test-resource" />
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/aidl" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/java" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/jni" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/rs" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/shaders" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/src/test/res" type="java-test-resource" />
<sourceFolder url="file://$MODULE_DIR$/src/test/resources" type="java-test-resource" />
<sourceFolder url="file://$MODULE_DIR$/src/test/assets" type="java-test-resource" />
Expand All @@ -81,6 +73,14 @@
<sourceFolder url="file://$MODULE_DIR$/src/test/jni" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/src/test/rs" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/src/test/shaders" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/res" type="java-test-resource" />
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/resources" type="java-test-resource" />
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/assets" type="java-test-resource" />
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/aidl" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/java" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/jni" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/rs" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/shaders" isTestSource="true" />
<excludeFolder url="file://$MODULE_DIR$/build/docs" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/assets" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/blame" />
Expand Down
29 changes: 16 additions & 13 deletions okhttputils/src/main/java/com/zhy/http/okhttp/OkHttpUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -130,26 +130,29 @@ public void onFailure(Call call, final IOException e)
@Override
public void onResponse(final Call call, final Response response)
{
if (call.isCanceled())
{
sendFailResultCallback(call, new IOException("Canceled!"), finalCallback, id);
return;
}

if (!finalCallback.validateReponse(response, id))
{
sendFailResultCallback(call, new IOException("request failed , reponse's code is : " + response.code()), finalCallback, id);
return;
}

try
{
if (call.isCanceled())
{
sendFailResultCallback(call, new IOException("Canceled!"), finalCallback, id);
return;
}

if (!finalCallback.validateReponse(response, id))
{
sendFailResultCallback(call, new IOException("request failed , reponse's code is : " + response.code()), finalCallback, id);
return;
}

Object o = finalCallback.parseNetworkResponse(response, id);
sendSuccessResultCallback(o, finalCallback, id);
} catch (Exception e)
{
sendFailResultCallback(call, e, finalCallback, id);

} finally
{
if (response.body() != null)
response.body().close();
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,21 +21,29 @@ public void add(HttpUrl url, List<Cookie> cookies)
{
List<Cookie> oldCookies = allCookies.get(url.host());

Iterator<Cookie> itNew = cookies.iterator();
Iterator<Cookie> itOld = oldCookies.iterator();
while (itNew.hasNext())
if (oldCookies != null)
{
String va = itNew.next().name();
while (va != null && itOld.hasNext())
Iterator<Cookie> itNew = cookies.iterator();
Iterator<Cookie> itOld = oldCookies.iterator();
while (itNew.hasNext())
{
String v = itOld.next().name();
if (v != null && va.equals(v))
String va = itNew.next().name();
while (va != null && itOld.hasNext())
{
itOld.remove();
String v = itOld.next().name();
if (v != null && va.equals(v))
{
itOld.remove();
}
}
}
oldCookies.addAll(cookies);
} else
{
allCookies.put(url.host(), cookies);
}
oldCookies.addAll(cookies);


}

@Override
Expand Down Expand Up @@ -74,7 +82,7 @@ public List<Cookie> getCookies()
@Override
public boolean remove(HttpUrl uri, Cookie cookie)
{
List<Cookie> cookies = allCookies.get(uri);
List<Cookie> cookies = allCookies.get(uri.host());
if (cookie != null)
{
return cookies.remove(cookie);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ protected RequestBody buildRequestBody()
{
FormBody.Builder builder = new FormBody.Builder();
addParams(builder);
return builder.build();
FormBody formBody = builder.build();
return formBody;
} else
{
MultipartBody.Builder builder = new MultipartBody.Builder()
Expand Down
4 changes: 2 additions & 2 deletions sample-okhttp/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ android {
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:22.2.1'
// compile project(':okhttputils')
compile 'com.zhy:okhttputils:2.6.1'
compile project(':okhttputils')
// compile 'com.zhy:okhttputils:2.6.1'
compile 'com.google.code.gson:gson:2.3.1'
compile 'com.squareup.okhttp3:okhttp-urlconnection:3.2.0'
compile 'com.github.franmontiel:PersistentCookieJar:v0.9.3'
Expand Down
3 changes: 1 addition & 2 deletions sample-okhttp/sample-okhttp.iml
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,6 @@
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/exploded-aar/com.android.support/appcompat-v7/22.2.1/jars" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/exploded-aar/com.android.support/support-v4/22.2.1/jars" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/exploded-aar/com.github.franmontiel/PersistentCookieJar/v0.9.3/jars" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/exploded-aar/com.zhy/okhttputils/2.6.1/jars" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/incremental" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/incremental-safeguard" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/instant-run-support" />
Expand All @@ -109,13 +108,13 @@
<orderEntry type="jdk" jdkName="Android API 22 Platform" jdkType="Android SDK" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" exported="" name="gson-2.3.1" level="project" />
<orderEntry type="library" exported="" name="okhttputils-2.6.1" level="project" />
<orderEntry type="library" exported="" name="support-v4-22.2.1" level="project" />
<orderEntry type="library" exported="" name="okhttp-urlconnection-3.2.0" level="project" />
<orderEntry type="library" exported="" name="PersistentCookieJar-v0.9.3" level="project" />
<orderEntry type="library" exported="" name="support-annotations-22.2.1" level="project" />
<orderEntry type="library" exported="" name="okio-1.8.0" level="project" />
<orderEntry type="library" exported="" name="appcompat-v7-22.2.1" level="project" />
<orderEntry type="library" exported="" name="okhttp-3.3.1" level="project" />
<orderEntry type="module" module-name="okhttputils" exported="" />
</component>
</module>
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
public class MainActivity extends AppCompatActivity
{

private String mBaseUrl = "http://192.168.56.1:8080/okHttpServer/";
private String mBaseUrl = "http://192.168.31.242:8888/okHttpServer/";

private static final String TAG = "MainActivity";

Expand Down Expand Up @@ -104,18 +104,17 @@ protected void onCreate(Bundle savedInstanceState)

public void getHtml(View view)
{
String url = "http://www.baidu.com";
// url="http://www.391k.com/api/xapi.ashx/info.json?key=bd_hyrzjjfb4modhj&size=10&page=1";
String url = "http://www.zhiyun-tech.com/App/Rider-M/changelog-zh.txt";
url="http://www.391k.com/api/xapi.ashx/info.json?key=bd_hyrzjjfb4modhj&size=10&page=1";
OkHttpUtils
.get()
.url(url)
.id(100)
// .addHeader("Accept-Encoding","")
.build()
.execute(new MyStringCallback());

}


public void postString(View view)
{
String url = mBaseUrl + "user!postString";
Expand Down Expand Up @@ -205,6 +204,8 @@ public void getHttpsHtml(View view)
{
String url = "https://kyfw.12306.cn/otn/";

// "https://12
// url =3.125.219.144:8443/mobileConnect/MobileConnect/authLogin.action?systemid=100009&mobile=13260284063&pipe=2&reqtime=1422986580048&ispin=2";
OkHttpUtils
.get()//
.url(url)//
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,19 @@

import android.app.Application;

import com.franmontiel.persistentcookiejar.ClearableCookieJar;
import com.franmontiel.persistentcookiejar.PersistentCookieJar;
import com.franmontiel.persistentcookiejar.cache.SetCookieCache;
import com.franmontiel.persistentcookiejar.persistence.SharedPrefsCookiePersistor;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.cookie.CookieJarImpl;
import com.zhy.http.okhttp.cookie.store.MemoryCookieStore;
import com.zhy.http.okhttp.https.HttpsUtils;
import com.zhy.http.okhttp.log.LoggerInterceptor;

import java.util.concurrent.TimeUnit;

import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.SSLSession;

import okhttp3.OkHttpClient;

/**
Expand Down Expand Up @@ -37,16 +43,24 @@ public void onCreate()
{
super.onCreate();

// ClearableCookieJar cookieJar1 = new PersistentCookieJar(new SetCookieCache(), new SharedPrefsCookiePersistor(getApplicationContext()));
ClearableCookieJar cookieJar1 = new PersistentCookieJar(new SetCookieCache(), new SharedPrefsCookiePersistor(getApplicationContext()));

HttpsUtils.SSLParams sslParams = HttpsUtils.getSslSocketFactory(null, null, null);

CookieJarImpl cookieJar1 = new CookieJarImpl(new MemoryCookieStore());
// CookieJarImpl cookieJar1 = new CookieJarImpl(new MemoryCookieStore());
OkHttpClient okHttpClient = new OkHttpClient.Builder()
.connectTimeout(10000L, TimeUnit.MILLISECONDS)
.readTimeout(10000L, TimeUnit.MILLISECONDS)
// .addInterceptor(new LoggerInterceptor("TAG"))
.addInterceptor(new LoggerInterceptor("TAG"))
.cookieJar(cookieJar1)
.hostnameVerifier(new HostnameVerifier()
{
@Override
public boolean verify(String hostname, SSLSession session)
{
return true;
}
})
.sslSocketFactory(sslParams.sSLSocketFactory, sslParams.trustManager)
.build();
OkHttpUtils.initClient(okHttpClient);
Expand Down

0 comments on commit e1ce7e5

Please sign in to comment.