Last active
March 11, 2019 14:38
-
-
Save caneryilmaz/3ecc0cdceb6af326a2b2440616ffbcde to your computer and use it in GitHub Desktop.
MOCK UTILS
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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