Skip to content

Instantly share code, notes, and snippets.

@caneryilmaz
Last active March 11, 2019 14:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save caneryilmaz/3ecc0cdceb6af326a2b2440616ffbcde to your computer and use it in GitHub Desktop.
Save caneryilmaz/3ecc0cdceb6af326a2b2440616ffbcde to your computer and use it in GitHub Desktop.
MOCK UTILS
import android.content.Context;
import android.util.Log;
import com.caner.fonttext.models.ResponseData;
import com.google.gson.Gson;
import java.io.IOException;
import java.io.InputStream;
public class MockConverterUtils {
private Object responseData;
private String jsonFileName;
private Context context;
ResponseObjListener responseListener;
public interface ResponseObjListener {
void onResponseItem(Object item);
}
public MockConverterUtils(Context context, String jsonFileName, Class className, ResponseObjListener responseListener) {
this.jsonFileName = jsonFileName;
this.context = context;
this.responseListener = responseListener;
String data = getAssetJsonData();
responseData = new Gson().fromJson(data, className);
this.responseListener.onResponseItem(responseData);
}
private String getAssetJsonData() {
String json = null;
try {
InputStream is = context.getAssets().open("mock.api/" + jsonFileName + ".json");
int size = is.available();
byte[] buffer = new byte[size];
is.read(buffer);
is.close();
json = new String(buffer, "UTF-8");
} catch (IOException ex) {
ex.printStackTrace();
return null;
}
Log.e("data", json);
return json;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment