Skip to content

Commit

Permalink
generic type parsing done
Browse files Browse the repository at this point in the history
  • Loading branch information
amitshekhariitbhu committed Jul 31, 2016
1 parent 724d318 commit 62c190c
Show file tree
Hide file tree
Showing 15 changed files with 275 additions and 133 deletions.
Original file line number Diff line number Diff line change
@@ -1,25 +1,27 @@
/*
* Copyright (C) 2016 Amit Shekhar
* Copyright (C) 2011 Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* * Copyright (C) 2016 Amit Shekhar
* * Copyright (C) 2011 Android Open Source Project
* *
* * Licensed under the Apache License, Version 2.0 (the "License");
* * you may not use this file except in compliance with the License.
* * You may obtain a copy of the License at
* *
* * http://www.apache.org/licenses/LICENSE-2.0
* *
* * Unless required by applicable law or agreed to in writing, software
* * distributed under the License is distributed on an "AS IS" BASIS,
* * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* * See the License for the specific language governing permissions and
* * limitations under the License.
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.androidnetworking.common;

import okhttp3.Headers;
import okhttp3.HttpUrl;
import okio.Source;
import okhttp3.ResponseBody;

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

public long length;

public Source source;
public ResponseBody body;

public HttpUrl url;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,19 @@
import com.androidnetworking.interfaces.DownloadProgressListener;
import com.androidnetworking.interfaces.JSONArrayRequestListener;
import com.androidnetworking.interfaces.JSONObjectRequestListener;
import com.androidnetworking.interfaces.ParsedRequestListener;
import com.androidnetworking.interfaces.StringRequestListener;
import com.androidnetworking.interfaces.UploadProgressListener;
import com.androidnetworking.internal.ANRequestQueue;
import com.androidnetworking.internal.GsonParserFactory;
import com.androidnetworking.utils.Utils;
import com.google.gson.reflect.TypeToken;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.File;
import java.io.IOException;
import java.lang.reflect.Type;
import java.util.HashMap;
import java.util.concurrent.Executor;
import java.util.concurrent.Future;
Expand Down Expand Up @@ -97,6 +99,7 @@ public class ANRequest<T extends ANRequest> {
private JSONObjectRequestListener mJSONObjectRequestListener;
private StringRequestListener mStringRequestListener;
private BitmapRequestListener mBitmapRequestListener;
private ParsedRequestListener mParsedRequestListener;
private DownloadProgressListener mDownloadProgressListener;
private UploadProgressListener mUploadProgressListener;
private DownloadListener mDownloadListener;
Expand All @@ -110,6 +113,7 @@ public class ANRequest<T extends ANRequest> {
private Executor mExecutor = null;
private OkHttpClient mOkHttpClient = null;
private String mUserAgent = null;
private Type mType = null;

public ANRequest(GetRequestBuilder builder) {
this.mRequestType = RequestType.SIMPLE;
Expand Down Expand Up @@ -212,6 +216,13 @@ public void getAsBitmap(BitmapRequestListener requestListener) {
ANRequestQueue.getInstance().addRequest(this);
}

public void getAsParsed(TypeToken typeToken, ParsedRequestListener parsedRequestListener) {
this.mType = typeToken.getType();
this.mResponseAs = RESPONSE.PARSED;
this.mParsedRequestListener = parsedRequestListener;
ANRequestQueue.getInstance().addRequest(this);
}

public T setDownloadProgressListener(DownloadProgressListener downloadProgressListener) {
this.mDownloadProgressListener = downloadProgressListener;
return (T) this;
Expand Down Expand Up @@ -297,6 +308,14 @@ public String getUserAgent() {
return mUserAgent;
}

public Type getType() {
return mType;
}

public void setType(Type type) {
this.mType = type;
}

public DownloadProgressListener getDownloadProgressListener() {
return new DownloadProgressListener() {
@Override
Expand Down Expand Up @@ -420,6 +439,7 @@ public void destroy() {
mJSONArrayRequestListener = null;
mStringRequestListener = null;
mBitmapRequestListener = null;
mParsedRequestListener = null;
mDownloadProgressListener = null;
mUploadProgressListener = null;
mDownloadListener = null;
Expand All @@ -435,32 +455,38 @@ public ANResponse parseResponse(ANData data) {
switch (mResponseAs) {
case JSON_ARRAY:
try {
JSONArray json = new JSONArray(Okio.buffer(data.source).readUtf8());
JSONArray json = new JSONArray(Okio.buffer(data.body.source()).readUtf8());
return ANResponse.success(json);
} catch (JSONException | IOException e) {
} catch (Exception e) {
return ANResponse.failed(new ANError(e));
}
case JSON_OBJECT:
try {
JSONObject json = new JSONObject(Okio.buffer(data.source).readUtf8());
JSONObject json = new JSONObject(Okio.buffer(data.body.source()).readUtf8());
return ANResponse.success(json);
} catch (JSONException | IOException e) {
} catch (Exception e) {
return ANResponse.failed(new ANError(e));
}
case STRING:
try {
return ANResponse.success(Okio.buffer(data.source).readUtf8());
} catch (IOException e) {
return ANResponse.success(Okio.buffer(data.body.source()).readUtf8());
} catch (Exception e) {
return ANResponse.failed(new ANError(e));
}
case BITMAP:
synchronized (sDecodeLock) {
try {
return Utils.decodeBitmap(data, mMaxWidth, mMaxHeight, mDecodeConfig, mScaleType);
} catch (OutOfMemoryError e) {
} catch (Exception e) {
return ANResponse.failed(new ANError(e));
}
}
case PARSED:
try {
return ANResponse.success(GsonParserFactory.getInstance().responseBodyParser(mType).convert(data.body));
} catch (Exception e) {
return ANResponse.failed(new ANError(e));
}
case PREFETCH:
return ANResponse.success(ANConstants.PREFETCH);
}
Expand All @@ -469,8 +495,8 @@ public ANResponse parseResponse(ANData data) {

public ANError parseNetworkError(ANError anError) {
try {
if (anError.getData() != null && anError.getData().source != null) {
anError.setErrorBody(Okio.buffer(anError.getData().source).readUtf8());
if (anError.getData() != null && anError.getData().body != null && anError.getData().body.source() != null) {
anError.setErrorBody(Okio.buffer(anError.getData().body.source()).readUtf8());
}
} catch (Exception e) {
e.printStackTrace();
Expand All @@ -495,6 +521,8 @@ public synchronized void deliverError(ANError anError) {
mBitmapRequestListener.onError(anError);
} else if (mDownloadListener != null) {
mDownloadListener.onError(anError);
} else if (mParsedRequestListener != null) {
mParsedRequestListener.onError(anError);
}
ANLog.d("Delivering anError : " + toString());
}
Expand All @@ -520,6 +548,8 @@ public void run() {
mStringRequestListener.onResponse((String) response.getResult());
} else if (mBitmapRequestListener != null) {
mBitmapRequestListener.onResponse((Bitmap) response.getResult());
} else if (mParsedRequestListener != null) {
mParsedRequestListener.onResponse(response.getResult());
}
finish();
}
Expand All @@ -535,6 +565,8 @@ public void run() {
mStringRequestListener.onResponse((String) response.getResult());
} else if (mBitmapRequestListener != null) {
mBitmapRequestListener.onResponse((Bitmap) response.getResult());
} else if (mParsedRequestListener != null) {
mParsedRequestListener.onResponse(response.getResult());
}
finish();
}
Expand All @@ -553,6 +585,8 @@ public void run() {
mStringRequestListener.onError(anError);
} else if (mBitmapRequestListener != null) {
mBitmapRequestListener.onError(anError);
} else if (mParsedRequestListener != null) {
mParsedRequestListener.onError(anError);
}
finish();
ANLog.d("Delivering cancelled : " + toString());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,6 @@ public enum RESPONSE {
JSON_OBJECT,
JSON_ARRAY,
BITMAP,
PREFETCH
PREFETCH,
PARSED
}
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,8 @@ public static ANData performSimpleRequest(ANRequest request) throws ANError {
data.url = okResponse.request().url();
data.code = okResponse.code();
data.headers = okResponse.headers();
data.source = okResponse.body().source();
data.length = okResponse.body().contentLength();
data.body = okResponse.body();
data.length = data.body.contentLength();
final long timeTaken = System.currentTimeMillis() - startTime;
if (okResponse.cacheResponse() == null) {
final long finalBytes = TrafficStats.getTotalRxBytes();
Expand Down Expand Up @@ -276,8 +276,8 @@ public static ANData performUploadRequest(ANRequest request) throws ANError {
data.url = okResponse.request().url();
data.code = okResponse.code();
data.headers = okResponse.headers();
data.source = okResponse.body().source();
data.length = okResponse.body().contentLength();
data.body = okResponse.body();
data.length = data.body.contentLength();
final long timeTaken = System.currentTimeMillis() - startTime;
if (request.getAnalyticsListener() != null) {
if (okResponse.cacheResponse() == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@
import com.androidnetworking.core.Core;
import com.androidnetworking.error.ANError;

import java.io.IOException;

import static com.androidnetworking.common.RequestType.DOWNLOAD;
import static com.androidnetworking.common.RequestType.MULTIPART;
import static com.androidnetworking.common.RequestType.SIMPLE;
Expand All @@ -37,7 +35,6 @@
*/
public class InternalRunnable implements Runnable {

private static final String TAG = InternalRunnable.class.getSimpleName();
private final Priority priority;
public final int sequence;
public final ANRequest request;
Expand Down Expand Up @@ -100,10 +97,10 @@ private void goForSimpleRequest() {
deliverError(request, se);

} finally {
if (data != null && data.source != null) {
if (data != null && data.body != null && data.body.source() != null) {
try {
data.source.close();
} catch (IOException ignored) {
data.body.source().close();
} catch (Exception e) {
ANLog.d("Unable to close source data");
}
}
Expand Down Expand Up @@ -168,10 +165,10 @@ private void goForUploadRequest() {
se.setErrorCode(0);
deliverError(request, se);
} finally {
if (data != null && data.source != null) {
if (data != null && data.body != null && data.body.source() != null) {
try {
data.source.close();
} catch (IOException ignored) {
data.body.source().close();
} catch (Exception e) {
ANLog.d("Unable to close source data");
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public static String getMimeType(String path) {
public static ANResponse<Bitmap> decodeBitmap(ANData response, int maxWidth, int maxHeight, Bitmap.Config decodeConfig, ImageView.ScaleType scaleType) {
byte[] data = new byte[0];
try {
data = Okio.buffer(response.source).readByteArray();
data = Okio.buffer(response.body.source()).readByteArray();
} catch (IOException e) {
e.printStackTrace();
}
Expand Down
Loading

0 comments on commit 62c190c

Please sign in to comment.