Skip to content

Commit 62c190c

Browse files
generic type parsing done
1 parent 724d318 commit 62c190c

File tree

15 files changed

+275
-133
lines changed

15 files changed

+275
-133
lines changed

android-networking/src/main/java/com/androidnetworking/common/ANData.java

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,27 @@
11
/*
2-
* Copyright (C) 2016 Amit Shekhar
3-
* Copyright (C) 2011 Android Open Source Project
42
*
5-
* Licensed under the Apache License, Version 2.0 (the "License");
6-
* you may not use this file except in compliance with the License.
7-
* You may obtain a copy of the License at
3+
* * Copyright (C) 2016 Amit Shekhar
4+
* * Copyright (C) 2011 Android Open Source Project
5+
* *
6+
* * Licensed under the Apache License, Version 2.0 (the "License");
7+
* * you may not use this file except in compliance with the License.
8+
* * You may obtain a copy of the License at
9+
* *
10+
* * http://www.apache.org/licenses/LICENSE-2.0
11+
* *
12+
* * Unless required by applicable law or agreed to in writing, software
13+
* * distributed under the License is distributed on an "AS IS" BASIS,
14+
* * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* * See the License for the specific language governing permissions and
16+
* * limitations under the License.
817
*
9-
* http://www.apache.org/licenses/LICENSE-2.0
10-
*
11-
* Unless required by applicable law or agreed to in writing, software
12-
* distributed under the License is distributed on an "AS IS" BASIS,
13-
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14-
* See the License for the specific language governing permissions and
15-
* limitations under the License.
1618
*/
1719

1820
package com.androidnetworking.common;
1921

2022
import okhttp3.Headers;
2123
import okhttp3.HttpUrl;
22-
import okio.Source;
24+
import okhttp3.ResponseBody;
2325

2426
/**
2527
* Created by amitshekhar on 22/03/16.
@@ -32,7 +34,7 @@ public class ANData {
3234

3335
public long length;
3436

35-
public Source source;
37+
public ResponseBody body;
3638

3739
public HttpUrl url;
3840

android-networking/src/main/java/com/androidnetworking/common/ANRequest.java

Lines changed: 45 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -28,17 +28,19 @@
2828
import com.androidnetworking.interfaces.DownloadProgressListener;
2929
import com.androidnetworking.interfaces.JSONArrayRequestListener;
3030
import com.androidnetworking.interfaces.JSONObjectRequestListener;
31+
import com.androidnetworking.interfaces.ParsedRequestListener;
3132
import com.androidnetworking.interfaces.StringRequestListener;
3233
import com.androidnetworking.interfaces.UploadProgressListener;
3334
import com.androidnetworking.internal.ANRequestQueue;
35+
import com.androidnetworking.internal.GsonParserFactory;
3436
import com.androidnetworking.utils.Utils;
37+
import com.google.gson.reflect.TypeToken;
3538

3639
import org.json.JSONArray;
37-
import org.json.JSONException;
3840
import org.json.JSONObject;
3941

4042
import java.io.File;
41-
import java.io.IOException;
43+
import java.lang.reflect.Type;
4244
import java.util.HashMap;
4345
import java.util.concurrent.Executor;
4446
import java.util.concurrent.Future;
@@ -97,6 +99,7 @@ public class ANRequest<T extends ANRequest> {
9799
private JSONObjectRequestListener mJSONObjectRequestListener;
98100
private StringRequestListener mStringRequestListener;
99101
private BitmapRequestListener mBitmapRequestListener;
102+
private ParsedRequestListener mParsedRequestListener;
100103
private DownloadProgressListener mDownloadProgressListener;
101104
private UploadProgressListener mUploadProgressListener;
102105
private DownloadListener mDownloadListener;
@@ -110,6 +113,7 @@ public class ANRequest<T extends ANRequest> {
110113
private Executor mExecutor = null;
111114
private OkHttpClient mOkHttpClient = null;
112115
private String mUserAgent = null;
116+
private Type mType = null;
113117

114118
public ANRequest(GetRequestBuilder builder) {
115119
this.mRequestType = RequestType.SIMPLE;
@@ -212,6 +216,13 @@ public void getAsBitmap(BitmapRequestListener requestListener) {
212216
ANRequestQueue.getInstance().addRequest(this);
213217
}
214218

219+
public void getAsParsed(TypeToken typeToken, ParsedRequestListener parsedRequestListener) {
220+
this.mType = typeToken.getType();
221+
this.mResponseAs = RESPONSE.PARSED;
222+
this.mParsedRequestListener = parsedRequestListener;
223+
ANRequestQueue.getInstance().addRequest(this);
224+
}
225+
215226
public T setDownloadProgressListener(DownloadProgressListener downloadProgressListener) {
216227
this.mDownloadProgressListener = downloadProgressListener;
217228
return (T) this;
@@ -297,6 +308,14 @@ public String getUserAgent() {
297308
return mUserAgent;
298309
}
299310

311+
public Type getType() {
312+
return mType;
313+
}
314+
315+
public void setType(Type type) {
316+
this.mType = type;
317+
}
318+
300319
public DownloadProgressListener getDownloadProgressListener() {
301320
return new DownloadProgressListener() {
302321
@Override
@@ -420,6 +439,7 @@ public void destroy() {
420439
mJSONArrayRequestListener = null;
421440
mStringRequestListener = null;
422441
mBitmapRequestListener = null;
442+
mParsedRequestListener = null;
423443
mDownloadProgressListener = null;
424444
mUploadProgressListener = null;
425445
mDownloadListener = null;
@@ -435,32 +455,38 @@ public ANResponse parseResponse(ANData data) {
435455
switch (mResponseAs) {
436456
case JSON_ARRAY:
437457
try {
438-
JSONArray json = new JSONArray(Okio.buffer(data.source).readUtf8());
458+
JSONArray json = new JSONArray(Okio.buffer(data.body.source()).readUtf8());
439459
return ANResponse.success(json);
440-
} catch (JSONException | IOException e) {
460+
} catch (Exception e) {
441461
return ANResponse.failed(new ANError(e));
442462
}
443463
case JSON_OBJECT:
444464
try {
445-
JSONObject json = new JSONObject(Okio.buffer(data.source).readUtf8());
465+
JSONObject json = new JSONObject(Okio.buffer(data.body.source()).readUtf8());
446466
return ANResponse.success(json);
447-
} catch (JSONException | IOException e) {
467+
} catch (Exception e) {
448468
return ANResponse.failed(new ANError(e));
449469
}
450470
case STRING:
451471
try {
452-
return ANResponse.success(Okio.buffer(data.source).readUtf8());
453-
} catch (IOException e) {
472+
return ANResponse.success(Okio.buffer(data.body.source()).readUtf8());
473+
} catch (Exception e) {
454474
return ANResponse.failed(new ANError(e));
455475
}
456476
case BITMAP:
457477
synchronized (sDecodeLock) {
458478
try {
459479
return Utils.decodeBitmap(data, mMaxWidth, mMaxHeight, mDecodeConfig, mScaleType);
460-
} catch (OutOfMemoryError e) {
480+
} catch (Exception e) {
461481
return ANResponse.failed(new ANError(e));
462482
}
463483
}
484+
case PARSED:
485+
try {
486+
return ANResponse.success(GsonParserFactory.getInstance().responseBodyParser(mType).convert(data.body));
487+
} catch (Exception e) {
488+
return ANResponse.failed(new ANError(e));
489+
}
464490
case PREFETCH:
465491
return ANResponse.success(ANConstants.PREFETCH);
466492
}
@@ -469,8 +495,8 @@ public ANResponse parseResponse(ANData data) {
469495

470496
public ANError parseNetworkError(ANError anError) {
471497
try {
472-
if (anError.getData() != null && anError.getData().source != null) {
473-
anError.setErrorBody(Okio.buffer(anError.getData().source).readUtf8());
498+
if (anError.getData() != null && anError.getData().body != null && anError.getData().body.source() != null) {
499+
anError.setErrorBody(Okio.buffer(anError.getData().body.source()).readUtf8());
474500
}
475501
} catch (Exception e) {
476502
e.printStackTrace();
@@ -495,6 +521,8 @@ public synchronized void deliverError(ANError anError) {
495521
mBitmapRequestListener.onError(anError);
496522
} else if (mDownloadListener != null) {
497523
mDownloadListener.onError(anError);
524+
} else if (mParsedRequestListener != null) {
525+
mParsedRequestListener.onError(anError);
498526
}
499527
ANLog.d("Delivering anError : " + toString());
500528
}
@@ -520,6 +548,8 @@ public void run() {
520548
mStringRequestListener.onResponse((String) response.getResult());
521549
} else if (mBitmapRequestListener != null) {
522550
mBitmapRequestListener.onResponse((Bitmap) response.getResult());
551+
} else if (mParsedRequestListener != null) {
552+
mParsedRequestListener.onResponse(response.getResult());
523553
}
524554
finish();
525555
}
@@ -535,6 +565,8 @@ public void run() {
535565
mStringRequestListener.onResponse((String) response.getResult());
536566
} else if (mBitmapRequestListener != null) {
537567
mBitmapRequestListener.onResponse((Bitmap) response.getResult());
568+
} else if (mParsedRequestListener != null) {
569+
mParsedRequestListener.onResponse(response.getResult());
538570
}
539571
finish();
540572
}
@@ -553,6 +585,8 @@ public void run() {
553585
mStringRequestListener.onError(anError);
554586
} else if (mBitmapRequestListener != null) {
555587
mBitmapRequestListener.onError(anError);
588+
} else if (mParsedRequestListener != null) {
589+
mParsedRequestListener.onError(anError);
556590
}
557591
finish();
558592
ANLog.d("Delivering cancelled : " + toString());

android-networking/src/main/java/com/androidnetworking/common/RESPONSE.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,5 +25,6 @@ public enum RESPONSE {
2525
JSON_OBJECT,
2626
JSON_ARRAY,
2727
BITMAP,
28-
PREFETCH
28+
PREFETCH,
29+
PARSED
2930
}

android-networking/src/main/java/com/androidnetworking/internal/InternalNetworking.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -121,8 +121,8 @@ public static ANData performSimpleRequest(ANRequest request) throws ANError {
121121
data.url = okResponse.request().url();
122122
data.code = okResponse.code();
123123
data.headers = okResponse.headers();
124-
data.source = okResponse.body().source();
125-
data.length = okResponse.body().contentLength();
124+
data.body = okResponse.body();
125+
data.length = data.body.contentLength();
126126
final long timeTaken = System.currentTimeMillis() - startTime;
127127
if (okResponse.cacheResponse() == null) {
128128
final long finalBytes = TrafficStats.getTotalRxBytes();
@@ -276,8 +276,8 @@ public static ANData performUploadRequest(ANRequest request) throws ANError {
276276
data.url = okResponse.request().url();
277277
data.code = okResponse.code();
278278
data.headers = okResponse.headers();
279-
data.source = okResponse.body().source();
280-
data.length = okResponse.body().contentLength();
279+
data.body = okResponse.body();
280+
data.length = data.body.contentLength();
281281
final long timeTaken = System.currentTimeMillis() - startTime;
282282
if (request.getAnalyticsListener() != null) {
283283
if (okResponse.cacheResponse() == null) {

android-networking/src/main/java/com/androidnetworking/internal/InternalRunnable.java

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,6 @@
2626
import com.androidnetworking.core.Core;
2727
import com.androidnetworking.error.ANError;
2828

29-
import java.io.IOException;
30-
3129
import static com.androidnetworking.common.RequestType.DOWNLOAD;
3230
import static com.androidnetworking.common.RequestType.MULTIPART;
3331
import static com.androidnetworking.common.RequestType.SIMPLE;
@@ -37,7 +35,6 @@
3735
*/
3836
public class InternalRunnable implements Runnable {
3937

40-
private static final String TAG = InternalRunnable.class.getSimpleName();
4138
private final Priority priority;
4239
public final int sequence;
4340
public final ANRequest request;
@@ -100,10 +97,10 @@ private void goForSimpleRequest() {
10097
deliverError(request, se);
10198

10299
} finally {
103-
if (data != null && data.source != null) {
100+
if (data != null && data.body != null && data.body.source() != null) {
104101
try {
105-
data.source.close();
106-
} catch (IOException ignored) {
102+
data.body.source().close();
103+
} catch (Exception e) {
107104
ANLog.d("Unable to close source data");
108105
}
109106
}
@@ -168,10 +165,10 @@ private void goForUploadRequest() {
168165
se.setErrorCode(0);
169166
deliverError(request, se);
170167
} finally {
171-
if (data != null && data.source != null) {
168+
if (data != null && data.body != null && data.body.source() != null) {
172169
try {
173-
data.source.close();
174-
} catch (IOException ignored) {
170+
data.body.source().close();
171+
} catch (Exception e) {
175172
ANLog.d("Unable to close source data");
176173
}
177174
}

android-networking/src/main/java/com/androidnetworking/utils/Utils.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ public static String getMimeType(String path) {
6565
public static ANResponse<Bitmap> decodeBitmap(ANData response, int maxWidth, int maxHeight, Bitmap.Config decodeConfig, ImageView.ScaleType scaleType) {
6666
byte[] data = new byte[0];
6767
try {
68-
data = Okio.buffer(response.source).readByteArray();
68+
data = Okio.buffer(response.body.source()).readByteArray();
6969
} catch (IOException e) {
7070
e.printStackTrace();
7171
}

0 commit comments

Comments
 (0)