Skip to content

Commit

Permalink
Check HTTP-codes
Browse files Browse the repository at this point in the history
Fixed a bug of crashing due to lack of a HTTP-code checker to Retrofit's OnResponse(..)
  • Loading branch information
SungjunApp authored Jan 28, 2020
1 parent ecc8807 commit 575035c
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 50 deletions.
49 changes: 35 additions & 14 deletions pixleesdk/src/main/java/com/pixlee/pixleesdk/PXLAlbum.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import java.util.ArrayList;
import java.util.HashMap;

import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
Expand Down Expand Up @@ -36,6 +37,34 @@ public class PXLAlbum implements RequestCallbacks {
protected RequestHandlers handlers;
protected Context context;

/**
* This deals with network responses
* when HTTP-code is in between 200 and 299, this method returns the body
* when HTTP-code is not in between 200 and 299, this method returns the error body
*
* @param response Retrofit's string body
*/
protected void processResponse(Response<String> response) {
try {
String result = response.body();
if (response.isSuccessful()) {
JSONObject json = new JSONObject(result);
JsonReceived(json);
} else {
ResponseBody errorBody = response.errorBody();
if (errorBody != null) {
if (handlers != null) {
handlers.DataLoadFailedHandler(errorBody.string());
}
} else {
Log.e(TAG, "no response data");
}
}

} catch (Exception e) {
e.printStackTrace();
}
}

/***
* Callback for a successful call to the api. Parses the response and converts the json data
Expand Down Expand Up @@ -142,12 +171,7 @@ public boolean loadNextPageOfPhotos(final RequestHandlers handlers) {
new Callback<String>() {
@Override
public void onResponse(Call<String> call, Response<String> response) {
try {
JSONObject json = new JSONObject(response.body());
JsonReceived(json);
} catch (JSONException e) {
e.printStackTrace();
}
processResponse(response);
}

@Override
Expand Down Expand Up @@ -199,21 +223,18 @@ public boolean uploadImage(String title, String email, String username, String p
.postMedia(
PXLClient.apiKey,
body
)
)
.enqueue(new Callback<String>() {
@Override
public void onResponse(Call<String> call, Response<String> response) {
try {
JSONObject json = new JSONObject(response.body());
JsonReceived(json);
} catch (JSONException e) {
e.printStackTrace();
}
processResponse(response);
}

@Override
public void onFailure(Call<String> call, Throwable t) {

if (handlers != null) {
handlers.DataLoadFailedHandler(t.toString());
}
}
});
} catch (Exception e) {
Expand Down
12 changes: 2 additions & 10 deletions pixleesdk/src/main/java/com/pixlee/pixleesdk/PXLPdpAlbum.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,9 @@
import android.content.Context;
import android.util.Log;

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

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;

import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
Expand Down Expand Up @@ -76,11 +71,8 @@ public boolean loadNextPageOfPhotos(final RequestHandlers handlers) {
@Override
public void onResponse(Call<String> call, Response<String> response) {
try {
String result = response.body();
Log.e("retrofit result","retrofit result:" + result);
JSONObject json = new JSONObject(result);
JsonReceived(json);
} catch (JSONException e) {
processResponse(response);
} catch (Exception e) {
e.printStackTrace();
}
}
Expand Down
64 changes: 38 additions & 26 deletions pixleesdk/src/main/java/com/pixlee/pixleesdk/PXLPhoto.java
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,42 @@ public interface PhotoLoadHandlers {
void photoLoadFailed(String error);
}

/**
* This deals with network responses
* when HTTP-code is in between 200 and 299, this method returns the body
* when HTTP-code is not in between 200 and 299, this method returns the error body
*
* @param response Retrofit's string body
*/
protected static void processResponse(Response<String> response, PhotoLoadHandlers callback) {
try {
String result = response.body();
if (response.isSuccessful()) {
JSONObject json = new JSONObject(result);
JSONObject data = json.optJSONObject("data");
if(data!=null) {
if (callback != null) {
callback.photoLoaded(PXLPhoto.fromJsonObj(data));
}
}else{
Log.e(TAG, "no data from successful api call");
}
} else {
ResponseBody errorBody = response.errorBody();
if (errorBody != null) {
if (callback != null) {
callback.photoLoadFailed(errorBody.toString());
}
} else {
Log.e(TAG, "no data from failed api call");
}
}

} catch (Exception e) {
e.printStackTrace();
}
}

/***
* Generates an ArrayList of PXLPhoto from the given JSON array.
* @param data - JSONArray of Pixlee photos
Expand Down Expand Up @@ -122,19 +158,7 @@ public static void getPhotoWithId(Context ctx, String identifier, final PhotoLoa
new Callback<String>() {
@Override
public void onResponse(Call<String> call, Response<String> response) {
try {
JSONObject json = new JSONObject(response.body());
JSONObject data = json.optJSONObject("data");
if (data == null) {
Log.e(TAG, "no data from successful api call");
} else {
if (callback != null) {
callback.photoLoaded(PXLPhoto.fromJsonObj(data));
}
}
} catch (JSONException e) {
e.printStackTrace();
}
processResponse(response, callback);
}

@Override
Expand Down Expand Up @@ -166,19 +190,7 @@ public void loadFromId(final PhotoLoadHandlers callback) {
new Callback<String>() {
@Override
public void onResponse(Call<String> call, Response<String> response) {
try {
JSONObject json = new JSONObject(response.body());
JSONObject data = json.optJSONObject("data");
if (data == null) {
Log.e(TAG, "no data from successful api call");
} else {
if (callback != null) {
callback.photoLoaded(PXLPhoto.fromJsonObj(data));
}
}
} catch (JSONException e) {
e.printStackTrace();
}
processResponse(response, callback);
}

@Override
Expand Down

0 comments on commit 575035c

Please sign in to comment.