Skip to content

Commit dff7221

Browse files
committed
Update for OkHttp3
1 parent 036b0cb commit dff7221

File tree

4 files changed

+48
-45
lines changed

4 files changed

+48
-45
lines changed

Parse/build.gradle

+3-3
Original file line numberDiff line numberDiff line change
@@ -42,13 +42,13 @@ android {
4242
dependencies {
4343
compile 'com.parse.bolts:bolts-tasks:1.4.0'
4444

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

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

5454
android.libraryVariants.all { variant ->

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

+23-20
Original file line numberDiff line numberDiff line change
@@ -8,22 +8,12 @@
88
*/
99
package com.parse;
1010

11-
import android.net.SSLCertificateSocketFactory;
1211
import android.net.SSLSessionCache;
1312

1413
import com.parse.http.ParseHttpBody;
1514
import com.parse.http.ParseHttpRequest;
1615
import com.parse.http.ParseHttpResponse;
1716
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;
2717

2818
import java.io.IOException;
2919
import java.io.InputStream;
@@ -33,6 +23,15 @@
3323
import java.util.concurrent.TimeUnit;
3424

3525
import bolts.Capture;
26+
import okhttp3.Call;
27+
import okhttp3.Headers;
28+
import okhttp3.Interceptor;
29+
import okhttp3.MediaType;
30+
import okhttp3.OkHttpClient;
31+
import okhttp3.Request;
32+
import okhttp3.RequestBody;
33+
import okhttp3.Response;
34+
import okhttp3.ResponseBody;
3635
import okio.BufferedSink;
3736
import okio.BufferedSource;
3837
import okio.Okio;
@@ -48,17 +47,18 @@
4847

4948
public ParseOkHttpClient(int socketOperationTimeout, SSLSessionCache sslSessionCache) {
5049

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

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

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

60-
okHttpClient.setSslSocketFactory(SSLCertificateSocketFactory.getDefault(
61-
socketOperationTimeout, sslSessionCache));
59+
// Broken until OkHttp3 can expose trust managers -- https://github.com/square/okhttp/commit/85f74e2004eaf0d4ff339e8644df3d8e716361e5
60+
// builder.sslSocketFactory(SSLCertificateSocketFactory.getDefault(socketOperationTimeout, sslSessionCache));
61+
okHttpClient = builder.build();
6262
}
6363

6464
@Override
@@ -183,7 +183,7 @@ private ParseHttpRequest getParseHttpRequest(Request okHttpRequest) {
183183
}
184184

185185
// Set url
186-
parseRequestBuilder.setUrl(okHttpRequest.urlString());
186+
parseRequestBuilder.setUrl(okHttpRequest.url().toString());
187187

188188
// Set Header
189189
for (Map.Entry<String, List<String>> entry : okHttpRequest.headers().toMultimap().entrySet()) {
@@ -206,7 +206,8 @@ private ParseHttpRequest getParseHttpRequest(Request okHttpRequest) {
206206
*/
207207
@Override
208208
/* package */ void addExternalInterceptor(final ParseNetworkInterceptor parseNetworkInterceptor) {
209-
okHttpClient.networkInterceptors().add(new Interceptor() {
209+
OkHttpClient.Builder builder = okHttpClient.newBuilder();
210+
builder.networkInterceptors().add(new Interceptor() {
210211
@Override
211212
public Response intercept(final Chain okHttpChain) throws IOException {
212213
Request okHttpRequest = okHttpChain.request();
@@ -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.
@@ -276,6 +277,8 @@ public BufferedSource source() throws IOException {
276277
return newOkHttpResponseBuilder.build();
277278
}
278279
});
280+
281+
okHttpClient = builder.build();
279282
}
280283

281284
private static class ParseOkHttpRequestBody extends RequestBody {

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

+6-6
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;
@@ -108,7 +108,7 @@ private void doSingleParseHttpClientExecuteWithResponse(int responseCode, String
108108
Map<String, String> requestHeaders = new HashMap<>();
109109
requestHeaders.put("User-Agent", "Parse Android SDK");
110110

111-
String requestUrl = server.getUrl("/").toString();
111+
String requestUrl = server.url("/").toString();
112112
JSONObject json = new JSONObject();
113113
json.put("key", "value");
114114
String requestContent = json.toString();
@@ -183,7 +183,7 @@ private void doSingleParseHttpClientExecuteWithGzipResponse(
183183
server.start();
184184

185185
// We do not need to add Accept-Encoding header manually, httpClient library should do that.
186-
String requestUrl = server.getUrl("/").toString();
186+
String requestUrl = server.url("/").toString();
187187
ParseHttpRequest parseRequest = new ParseHttpRequest.Builder()
188188
.setUrl(requestUrl)
189189
.setMethod(ParseHttpRequest.Method.GET)

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

+16-16
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.getUrl());
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)