Skip to content

Commit 31614dd

Browse files
committed
Refactor Service Clients to return read response byte[] since it's an exception to attempt to read from Network in Main UI Thread
1 parent 2db1b72 commit 31614dd

File tree

4 files changed

+86
-29
lines changed

4 files changed

+86
-29
lines changed

src/AndroidClient/android/src/main/java/net/servicestack/android/AndroidServiceClient.java

Lines changed: 29 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import net.servicestack.client.AsyncServiceClient;
99
import net.servicestack.client.IReturn;
1010
import net.servicestack.client.JsonServiceClient;
11+
import net.servicestack.client.Utils;
1112

1213
import java.lang.reflect.Type;
1314
import java.net.HttpURLConnection;
@@ -111,24 +112,24 @@ protected void onPostExecute(T response) {
111112
}, path);
112113
}
113114

114-
public void getAsync(String path, final AsyncResult<HttpURLConnection> asyncResult) {
115+
public void getAsync(String path, final AsyncResult<byte[]> asyncResult) {
115116
final AndroidServiceClient client = this;
116-
execTask(new AsyncTask<String, Void, HttpURLConnection>() {
117+
execTask(new AsyncTask<String, Void, byte[]>() {
117118
@Override
118-
protected HttpURLConnection doInBackground(String... params) {
119+
protected byte[] doInBackground(String... params) {
119120
try {
120-
return client.get(params[0]);
121+
HttpURLConnection httpRes = client.get(params[0]);
122+
return Utils.readBytesToEnd(httpRes);
121123
} catch (Exception e) {
122124
asyncResult.setError(e);
123125
return null;
124126
}
125127
}
126128

127129
@Override
128-
protected void onPostExecute(HttpURLConnection response) {
129-
asyncResult.completeResult(response);
130+
protected void onPostExecute(byte[] bytes) {
131+
asyncResult.completeResult(bytes);
130132
}
131-
132133
}, path);
133134
}
134135

@@ -245,22 +246,23 @@ protected void onPostExecute(T response) {
245246
}
246247

247248
@Override
248-
public void postAsync(String path, final byte[] requestBody, final String contentType, final AsyncResult<HttpURLConnection> asyncResult) {
249+
public void postAsync(String path, final byte[] requestBody, final String contentType, final AsyncResult<byte[]> asyncResult) {
249250
final AndroidServiceClient client = this;
250-
execTask(new AsyncTask<String, Void, HttpURLConnection>() {
251+
execTask(new AsyncTask<String, Void, byte[]>() {
251252
@Override
252-
protected HttpURLConnection doInBackground(String... params) {
253+
protected byte[] doInBackground(String... params) {
253254
try {
254-
return client.post(params[0], requestBody, contentType);
255+
HttpURLConnection httpRes = client.post(params[0], requestBody, contentType);
256+
return Utils.readBytesToEnd(httpRes);
255257
} catch (Exception e) {
256258
asyncResult.setError(e);
257259
return null;
258260
}
259261
}
260262

261263
@Override
262-
protected void onPostExecute(HttpURLConnection response) {
263-
asyncResult.completeResult(response);
264+
protected void onPostExecute(byte[] bytes) {
265+
asyncResult.completeResult(bytes);
264266
}
265267

266268
}, path);
@@ -379,22 +381,23 @@ protected void onPostExecute(T response) {
379381
}
380382

381383
@Override
382-
public void putAsync(String path, final byte[] requestBody, final String contentType, final AsyncResult<HttpURLConnection> asyncResult) {
384+
public void putAsync(String path, final byte[] requestBody, final String contentType, final AsyncResult<byte[]> asyncResult) {
383385
final AndroidServiceClient client = this;
384-
execTask(new AsyncTask<String, Void, HttpURLConnection>() {
386+
execTask(new AsyncTask<String, Void, byte[]>() {
385387
@Override
386-
protected HttpURLConnection doInBackground(String... params) {
388+
protected byte[] doInBackground(String... params) {
387389
try {
388-
return client.put(params[0], requestBody, contentType);
390+
HttpURLConnection httpRes = client.put(params[0], requestBody, contentType);
391+
return Utils.readBytesToEnd(httpRes);
389392
} catch (Exception e) {
390393
asyncResult.setError(e);
391394
return null;
392395
}
393396
}
394397

395398
@Override
396-
protected void onPostExecute(HttpURLConnection response) {
397-
asyncResult.completeResult(response);
399+
protected void onPostExecute(byte[] bytes) {
400+
asyncResult.completeResult(bytes);
398401
}
399402

400403
}, path);
@@ -487,22 +490,23 @@ protected void onPostExecute(T response) {
487490
}, path);
488491
}
489492

490-
public void deleteAsync(String path, final AsyncResult<HttpURLConnection> asyncResult) {
493+
public void deleteAsync(String path, final AsyncResult<byte[]> asyncResult) {
491494
final AndroidServiceClient client = this;
492-
execTask(new AsyncTask<String, Void, HttpURLConnection>() {
495+
execTask(new AsyncTask<String, Void, byte[]>() {
493496
@Override
494-
protected HttpURLConnection doInBackground(String... params) {
497+
protected byte[] doInBackground(String... params) {
495498
try {
496-
return client.delete(params[0]);
499+
HttpURLConnection httpRes = client.delete(params[0]);
500+
return Utils.readBytesToEnd(httpRes);
497501
} catch (Exception e) {
498502
asyncResult.setError(e);
499503
return null;
500504
}
501505
}
502506

503507
@Override
504-
protected void onPostExecute(HttpURLConnection response) {
505-
asyncResult.completeResult(response);
508+
protected void onPostExecute(byte[] bytes) {
509+
asyncResult.completeResult(bytes);
506510
}
507511

508512
}, path);
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package net.servicestack.android;
2+
3+
import android.graphics.Bitmap;
4+
import android.graphics.BitmapFactory;
5+
6+
import java.io.IOException;
7+
import java.io.InputStream;
8+
import java.net.HttpURLConnection;
9+
10+
public class AndroidUtils {
11+
public static Bitmap readBitmap(HttpURLConnection response){
12+
try {
13+
return readBitmap(response.getInputStream());
14+
} catch (IOException e) {
15+
throw new RuntimeException(e);
16+
}
17+
}
18+
19+
public static Bitmap readBitmap(InputStream stream) {
20+
return BitmapFactory.decodeStream(stream);
21+
}
22+
23+
public static Bitmap readBitmap(byte[] bytes) {
24+
return BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
25+
}
26+
}

src/AndroidClient/client/src/main/java/net/servicestack/client/AsyncServiceClient.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,25 +9,25 @@ public interface AsyncServiceClient {
99
public <T> void getAsync(IReturn<T> request, final Map<String, String> queryParams, final AsyncResult<T> asyncResult);
1010
public <T> void getAsync(String path, final Class responseType, final AsyncResult<T> asyncResult);
1111
public <T> void getAsync(String path, final Type responseType, final AsyncResult<T> asyncResult);
12-
public void getAsync(String path, final AsyncResult<HttpURLConnection> asyncResult);
12+
public void getAsync(String path, final AsyncResult<byte[]> asyncResult);
1313

1414
public <T> void postAsync(IReturn<T> request, final AsyncResult<T> asyncResult);
1515
public <T> void postAsync(String path, final Object request, final Class responseType, final AsyncResult<T> asyncResult);
1616
public <T> void postAsync(String path, final Object request, final Type responseType, final AsyncResult<T> asyncResult);
1717
public <T> void postAsync(String path, final byte[] requestBody, final String contentType, final Class responseType, final AsyncResult<T> asyncResult);
1818
public <T> void postAsync(String path, final byte[] requestBody, final String contentType, final Type responseType, final AsyncResult<T> asyncResult);
19-
public void postAsync(String path, final byte[] requestBody, final String contentType, final AsyncResult<HttpURLConnection> asyncResult);
19+
public void postAsync(String path, final byte[] requestBody, final String contentType, final AsyncResult<byte[]> asyncResult);
2020

2121
public <T> void putAsync(IReturn<T> request, final AsyncResult<T> asyncResult);
2222
public <T> void putAsync(String path, final Object request, final Class responseType, final AsyncResult<T> asyncResult);
2323
public <T> void putAsync(String path, final Object request, final Type responseType, final AsyncResult<T> asyncResult);
2424
public <T> void putAsync(String path, final byte[] requestBody, final String contentType, final Class responseType, final AsyncResult<T> asyncResult);
2525
public <T> void putAsync(String path, final byte[] requestBody, final String contentType, final Type responseType, final AsyncResult<T> asyncResult);
26-
public void putAsync(String path, final byte[] requestBody, final String contentType, final AsyncResult<HttpURLConnection> asyncResult);
26+
public void putAsync(String path, final byte[] requestBody, final String contentType, final AsyncResult<byte[]> asyncResult);
2727

2828
public <T> void deleteAsync(IReturn<T> request, final AsyncResult<T> asyncResult);
2929
public <T> void deleteAsync(IReturn<T> request, final Map<String, String> queryParams, final AsyncResult<T> asyncResult);
3030
public <T> void deleteAsync(String path, final Class responseType, final AsyncResult<T> asyncResult);
3131
public <T> void deleteAsync(String path, final Type responseType, final AsyncResult<T> asyncResult);
32-
public void deleteAsync(String path, final AsyncResult<HttpURLConnection> asyncResult);
32+
public void deleteAsync(String path, final AsyncResult<byte[]> asyncResult);
3333
}

src/AndroidClient/client/src/main/java/net/servicestack/client/Utils.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@
22

33
package net.servicestack.client;
44

5+
import org.apache.http.util.ByteArrayBuffer;
56
import org.json.JSONArray;
67
import org.json.JSONException;
78
import org.json.JSONObject;
89

10+
import java.io.BufferedInputStream;
911
import java.io.BufferedReader;
1012
import java.io.IOException;
1113
import java.io.InputStream;
@@ -430,6 +432,31 @@ public static String readToEnd(InputStream stream, final String charsetName) thr
430432
return text;
431433
}
432434

435+
public static byte[] readBytesToEnd(HttpURLConnection response){
436+
try {
437+
return readBytesToEnd(response.getInputStream());
438+
} catch (IOException e) {
439+
throw new RuntimeException(e);
440+
}
441+
}
442+
443+
public static byte[] readBytesToEnd(InputStream stream) throws IOException {
444+
445+
ByteArrayBuffer bytes = new ByteArrayBuffer(1024);
446+
447+
final BufferedInputStream bufferedStream = new BufferedInputStream(stream, 8192);
448+
try {
449+
final byte[] buffer = new byte[1024];
450+
int bytesRead = 0;
451+
while ((bytesRead = bufferedStream.read(buffer)) > 0) {
452+
bytes.append(buffer, 0, bytesRead);
453+
}
454+
return bytes.toByteArray();
455+
} finally {
456+
bufferedStream.close();
457+
}
458+
}
459+
433460
public static String getUnderlyingContentType(String contentType) {
434461
return splitOnFirst(contentType, ';')[0].trim().toLowerCase();
435462
}

0 commit comments

Comments
 (0)