Skip to content

Commit e5ea001

Browse files
authored
Merge pull request #5 from codepath/rogerh/add-timeout
Add connect and read timeout
2 parents aab9e8c + 86bec82 commit e5ea001

File tree

6 files changed

+29
-18
lines changed

6 files changed

+29
-18
lines changed

README.md

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
# AsyncHttpClient library
22

3-
This is a lightweight asynchronous HTTP client powered by [OkHttp](https://square.github.io/okhttp/) but with a significantly simplified and easier to use API design.
3+
This is a lightweight asynchronous HTTP client powered by [OkHttp](https://square.github.io/okhttp/) but with a significantly simplified and easier to use API design.
44

55
The goal of this library is to have an API that clearly and cleanly supports the following features:
66

77
* Asynchronous network requests without any need for manual thread handling
88
* Response `onSuccess` callbacks run on the mainthread (by default)
99
* Easy way to catch all errors and failures and handle them
1010
* Easy pluggable Text, JSON, and Bitmap response handlers to parse the response
11-
11+
1212
This client tries to follow a similar API inspired by this [older now deprecated android-async-http library](https://github.com/android-async-http/android-async-http).
1313

1414
## Setup
@@ -17,7 +17,7 @@ To use this library, add the following to your `.gradle` file:
1717

1818
```gradle
1919
dependencies {
20-
implementation 'com.codepath.libraries:asynchttpclient:2.0.1'
20+
implementation 'com.codepath.libraries:asynchttpclient:2.1.0'
2121
}
2222
```
2323

@@ -30,29 +30,29 @@ dependencies {
3030
```xml
3131
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
3232
package="com.codepath.example">
33-
33+
3434
<uses-permission android:name="android.permission.INTERNET" />```
3535
```
3636

3737
3. Add any networking calls by leveraging the `AsyncHttpClient`
38-
38+
3939
```java
4040
AsyncHttpClient client = new AsyncHttpClient();
4141
client.get("https://api.thecatapi.com/v1/images/search", new TextHttpResponseHandler() {
4242
@Override
4343
public void onSuccess(int statusCode, Headers headers, String response) {
4444
Log.d("DEBUG", response);
4545
}
46-
46+
4747
@Override
4848
public void onFailure(int statusCode, @Nullable Headers headers, String errorResponse, @Nullable Throwable throwable) {
4949
Log.d("DEBUG", errorResponse);
5050
}
5151
});
5252
```
53-
54-
This example uses `TextHttpResponseHandler` which presents the response as raw text. We could use the `JsonHttpResponseHandler` instead to have the API response automatically parsed for us into JSON. See [other example calls here](https://github.com/codepath/AsyncHttpClient/blob/master/example/src/main/java/com/codepath/example/TestActivity.java).
55-
53+
54+
This example uses `TextHttpResponseHandler` which presents the response as raw text. We could use the `JsonHttpResponseHandler` instead to have the API response automatically parsed for us into JSON. See [other example calls here](https://github.com/codepath/AsyncHttpClient/blob/master/example/src/main/java/com/codepath/example/TestActivity.java).
55+
5656
For more detailed usages, check out the [CodePath Async Http Client Guide](https://guides.codepath.com/android/Using-CodePath-Async-Http-Client).
5757

5858

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ buildscript {
77

88
}
99
dependencies {
10-
classpath 'com.android.tools.build:gradle:4.0.1'
10+
classpath 'com.android.tools.build:gradle:4.0.2'
1111
// NOTE: Do not place your application dependencies here; they belong
1212
// in the individual module build.gradle files
1313
}

example/build.gradle

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,10 @@ android {
2626

2727
dependencies {
2828
implementation fileTree(dir: 'libs', include: ['*.jar'])
29-
implementation 'androidx.appcompat:appcompat:1.0.2'
30-
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
31-
testImplementation 'junit:junit:4.12'
32-
androidTestImplementation 'androidx.test:runner:1.2.0'
33-
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
29+
implementation 'androidx.appcompat:appcompat:1.2.0'
30+
implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
31+
testImplementation 'junit:junit:4.13.1'
32+
androidTestImplementation 'androidx.test:runner:1.3.0'
33+
androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
3434
implementation project(':library')
3535
}

example/src/main/java/com/codepath/example/TestActivity.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@ public void onFailure(int statusCode, @Nullable Headers headers, String errorRes
5757
}
5858
});
5959

60+
client.setReadTimeout(10);
61+
client.setConnectTimeout(10);
6062
RequestParams params = new RequestParams();
6163
params.put("limit", "5");
6264
params.put("page", 0);

library/build.gradle

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ ext {
1111
POM_DESC = "Async Http Client wrapper using OkHttp"
1212
POM_SCM_URL = 'https://github.com/codepath/AsyncHttpClient'
1313
BASE_VERSION = 2
14-
VERSION_NAME = "2.0.1"
14+
VERSION_NAME = "2.1.0"
1515
}
1616

1717
version = VERSION_NAME
@@ -43,11 +43,11 @@ dependencies {
4343
implementation fileTree(dir: 'libs', include: ['*.jar'])
4444

4545
implementation 'androidx.appcompat:appcompat:1.2.0'
46-
api "com.squareup.okhttp3:okhttp:4.5.0"
46+
api "com.squareup.okhttp3:okhttp:4.9.0"
4747
api 'com.facebook.stetho:stetho-okhttp3:1.5.1'
4848

4949
testImplementation 'junit:junit:4.12'
50-
androidTestImplementation 'androidx.test:runner:1.2.0'
50+
androidTestImplementation 'androidx.test:runner:1.3.0'
5151
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
5252
}
5353

library/src/main/java/com/codepath/asynchttpclient/AsyncHttpClient.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
import java.io.File;
88
import java.util.Map;
9+
import java.util.concurrent.TimeUnit;
910

1011
import okhttp3.HttpUrl;
1112
import okhttp3.MediaType;
@@ -32,6 +33,14 @@ public AsyncHttpClient(OkHttpClient client) {
3233
okHttpClient = client;
3334
}
3435

36+
public void setReadTimeout(int timeout) {
37+
okHttpClient.newBuilder().readTimeout(timeout, TimeUnit.SECONDS);
38+
}
39+
40+
public void setConnectTimeout(int timeout) {
41+
okHttpClient.newBuilder().connectTimeout(timeout, TimeUnit.SECONDS);
42+
}
43+
3544
protected Request.Builder createBuilderWithHeaders(
3645
String url, @Nullable RequestHeaders requestHeaders) {
3746
Request.Builder requestBuilder = new Request.Builder().url(url);

0 commit comments

Comments
 (0)