Skip to content

Commit 2d37cd5

Browse files
committed
Update for OkHttp3
1 parent f439b3d commit 2d37cd5

File tree

4 files changed

+44
-43
lines changed

4 files changed

+44
-43
lines changed

Parse/build.gradle

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,13 @@ android {
4141
dependencies {
4242
compile 'com.parse.bolts:bolts-tasks:1.4.0'
4343

44-
provided 'com.squareup.okhttp:okhttp:2.4.0'
45-
provided 'com.facebook.stetho:stetho:1.1.1'
44+
provided 'com.squareup.okhttp3:okhttp:3.2.0'
45+
provided 'com.facebook.stetho:stetho:1.3.0'
4646

4747
testCompile 'org.robolectric:robolectric:3.0'
4848
testCompile 'org.skyscreamer:jsonassert:1.2.3'
4949
testCompile 'org.mockito:mockito-core:1.10.19'
50-
testCompile 'com.squareup.okhttp:mockwebserver:2.4.0'
50+
testCompile 'com.squareup.okhttp3:mockwebserver:3.2.0'
5151
}
5252

5353
android.libraryVariants.all { variant ->

Parse/src/main/java/com/parse/ParseOkHttpClient.java

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -8,22 +8,13 @@
88
*/
99
package com.parse;
1010

11-
import android.net.SSLCertificateSocketFactory;
12-
import android.net.SSLSessionCache;
13-
1411
import com.parse.http.ParseHttpBody;
1512
import com.parse.http.ParseHttpRequest;
1613
import com.parse.http.ParseHttpResponse;
1714
import com.parse.http.ParseNetworkInterceptor;
18-
import com.squareup.okhttp.Call;
19-
import com.squareup.okhttp.Headers;
20-
import com.squareup.okhttp.Interceptor;
21-
import com.squareup.okhttp.MediaType;
22-
import com.squareup.okhttp.OkHttpClient;
23-
import com.squareup.okhttp.Request;
24-
import com.squareup.okhttp.RequestBody;
25-
import com.squareup.okhttp.Response;
26-
import com.squareup.okhttp.ResponseBody;
15+
16+
import android.net.SSLCertificateSocketFactory;
17+
import android.net.SSLSessionCache;
2718

2819
import java.io.IOException;
2920
import java.io.InputStream;
@@ -33,6 +24,15 @@
3324
import java.util.concurrent.TimeUnit;
3425

3526
import bolts.Capture;
27+
import okhttp3.Call;
28+
import okhttp3.Headers;
29+
import okhttp3.Interceptor;
30+
import okhttp3.MediaType;
31+
import okhttp3.OkHttpClient;
32+
import okhttp3.Request;
33+
import okhttp3.RequestBody;
34+
import okhttp3.Response;
35+
import okhttp3.ResponseBody;
3636
import okio.BufferedSink;
3737
import okio.BufferedSource;
3838
import okio.Okio;
@@ -48,17 +48,18 @@
4848

4949
public ParseOkHttpClient(int socketOperationTimeout, SSLSessionCache sslSessionCache) {
5050

51-
okHttpClient = new OkHttpClient();
51+
OkHttpClient.Builder builder = new OkHttpClient.Builder();
5252

53-
okHttpClient.setConnectTimeout(socketOperationTimeout, TimeUnit.MILLISECONDS);
54-
okHttpClient.setReadTimeout(socketOperationTimeout, TimeUnit.MILLISECONDS);
53+
builder.connectTimeout(socketOperationTimeout, TimeUnit.MILLISECONDS);
54+
builder.readTimeout(socketOperationTimeout, TimeUnit.MILLISECONDS);
5555

5656
// Don't handle redirects. We copy the setting from AndroidHttpClient.
5757
// For detail, check https://quip.com/Px8jAxnaun2r
58-
okHttpClient.setFollowRedirects(false);
58+
builder.followRedirects(false);
5959

60-
okHttpClient.setSslSocketFactory(SSLCertificateSocketFactory.getDefault(
60+
builder.sslSocketFactory(SSLCertificateSocketFactory.getDefault(
6161
socketOperationTimeout, sslSessionCache));
62+
okHttpClient = builder.build();
6263
}
6364

6465
@Override
@@ -183,7 +184,7 @@ private ParseHttpRequest getParseHttpRequest(Request okHttpRequest) {
183184
}
184185

185186
// Set url
186-
parseRequestBuilder.setUrl(okHttpRequest.urlString());
187+
parseRequestBuilder.setUrl(okHttpRequest.toString());
187188

188189
// Set Header
189190
for (Map.Entry<String, List<String>> entry : okHttpRequest.headers().toMultimap().entrySet()) {
@@ -257,12 +258,12 @@ public MediaType contentType() {
257258
}
258259

259260
@Override
260-
public long contentLength() throws IOException {
261+
public long contentLength() {
261262
return parseResponse.getTotalSize();
262263
}
263264

264265
@Override
265-
public BufferedSource source() throws IOException {
266+
public BufferedSource source() {
266267
// We need to use the proxy stream from interceptor to replace the origin network
267268
// stream, so when the stream is read by Parse, the network stream is proxyed in the
268269
// interceptor.

Parse/src/test/java/com/parse/ParseHttpClientTest.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@
1010

1111
import com.parse.http.ParseHttpRequest;
1212
import com.parse.http.ParseHttpResponse;
13-
import com.squareup.okhttp.Headers;
14-
import com.squareup.okhttp.mockwebserver.MockResponse;
15-
import com.squareup.okhttp.mockwebserver.MockWebServer;
16-
import com.squareup.okhttp.mockwebserver.RecordedRequest;
13+
import okhttp3.Headers;
14+
import okhttp3.mockwebserver.MockResponse;
15+
import okhttp3.mockwebserver.MockWebServer;
16+
import okhttp3.mockwebserver.RecordedRequest;
1717

1818
import org.json.JSONObject;
1919
import org.junit.Test;

Parse/src/test/java/com/parse/ParseOkHttpClientTest.java

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,15 @@
1111
import com.parse.http.ParseHttpRequest;
1212
import com.parse.http.ParseHttpResponse;
1313
import com.parse.http.ParseNetworkInterceptor;
14-
import com.squareup.okhttp.MediaType;
15-
import com.squareup.okhttp.Protocol;
16-
import com.squareup.okhttp.Request;
17-
import com.squareup.okhttp.RequestBody;
18-
import com.squareup.okhttp.Response;
19-
import com.squareup.okhttp.ResponseBody;
20-
import com.squareup.okhttp.mockwebserver.MockResponse;
21-
import com.squareup.okhttp.mockwebserver.MockWebServer;
22-
import com.squareup.okhttp.mockwebserver.RecordedRequest;
14+
import okhttp3.MediaType;
15+
import okhttp3.Protocol;
16+
import okhttp3.Request;
17+
import okhttp3.RequestBody;
18+
import okhttp3.Response;
19+
import okhttp3.ResponseBody;
20+
import okhttp3.mockwebserver.MockResponse;
21+
import okhttp3.mockwebserver.MockWebServer;
22+
import okhttp3.mockwebserver.RecordedRequest;
2323

2424
import org.json.JSONException;
2525
import org.json.JSONObject;
@@ -116,7 +116,7 @@ public void testGetOkHttpRequest() throws IOException {
116116
// Verify method
117117
assertEquals(ParseHttpRequest.Method.POST.toString(), okHttpRequest.method());
118118
// Verify URL
119-
assertEquals(url, okHttpRequest.urlString());
119+
assertEquals(url, okHttpRequest.url().toString());
120120
// Verify Headers
121121
assertEquals(1, okHttpRequest.headers(headerName).size());
122122
assertEquals(headerValue, okHttpRequest.headers(headerName).get(0));
@@ -173,12 +173,12 @@ public MediaType contentType() {
173173
}
174174

175175
@Override
176-
public long contentLength() throws IOException {
176+
public long contentLength() {
177177
return contentLength;
178178
}
179179

180180
@Override
181-
public BufferedSource source() throws IOException {
181+
public BufferedSource source() {
182182
Buffer buffer = new Buffer();
183183
buffer.write(content.getBytes());
184184
return buffer;
@@ -252,7 +252,7 @@ public ParseHttpResponse intercept(Chain chain) throws IOException {
252252
});
253253

254254
// We do not need to add Accept-Encoding header manually, httpClient library should do that.
255-
String requestUrl = server.getUrl("/").toString();
255+
String requestUrl = server.url("/").toString();
256256
ParseHttpRequest parseRequest = new ParseHttpRequest.Builder()
257257
.setUrl(requestUrl)
258258
.setMethod(ParseHttpRequest.Method.GET)
@@ -359,7 +359,7 @@ private ParseHttpRequest generateClientRequest() throws Exception {
359359
JSONObject json = new JSONObject();
360360
json.put("key", "value");
361361
ParseHttpRequest parseRequest = new ParseHttpRequest.Builder()
362-
.setUrl(server.getUrl("/").toString())
362+
.setUrl(server.url("/").toString())
363363
.setMethod(ParseHttpRequest.Method.POST)
364364
.setBody(new ParseByteArrayHttpBody(json.toString().getBytes(), "application/json"))
365365
.setHeaders(headers)
@@ -370,7 +370,7 @@ private ParseHttpRequest generateClientRequest() throws Exception {
370370
// Verify the request from client, if you change the data in generateClientRequest, make
371371
// sure you also change the condition in this method otherwise tests will fail
372372
private void verifyClientRequest(ParseHttpRequest parseRequest) throws IOException {
373-
assertEquals(server.getUrl("/").toString(), parseRequest.getUrl());
373+
assertEquals(server.url("/").toString(), parseRequest.url());
374374
assertEquals(ParseHttpRequest.Method.POST, parseRequest.getMethod());
375375
assertEquals("requestValue", parseRequest.getHeader("requestKey"));
376376
assertEquals("application/json", parseRequest.getBody().getContentType());
@@ -390,7 +390,7 @@ private ParseHttpRequest generateInterceptorRequest() {
390390
ParseHttpRequest requestAgain =
391391
new ParseHttpRequest.Builder()
392392
.addHeader("requestKeyAgain", "requestValueAgain")
393-
.setUrl(server.getUrl("/test").toString())
393+
.setUrl(server.url("/test").toString())
394394
.setMethod(ParseHttpRequest.Method.GET)
395395
.build();
396396
return requestAgain;

0 commit comments

Comments
 (0)