Trying to consume json response from url runs out of memory, or have to declare android:largeHeap="true".
This is the url
https://www.mtgjson.com/json/AllCards.json
When I enter this url in web browser on my PC, the page is populated within a second. why does it take android such long time to finish this api request? EDIT: I realize the web browser does not load all the data immediately, i see it showing the data as it comes before scrolling down in the window.
Are there ways to make the android device respond as fast as the web browser on pc when accessing this url?
When using emulators, outOfMemoryExeption
unless if I increase RAM size. on samsung S7 device, outOfMemoryException
unless I have largeHeap="true"
in the manifest.
Same url in web browser doesn't take as long.
@Override
protected String doInBackground(String... params) {
HttpURLConnection urlConnection = null;
BufferedReader reader = null;
String apiJsonStr = null;
try {
String baseUrl = "https://www.mtgjson.com/json/AllCards.json";
String uri = Uri.parse(baseUrl).buildUpon().toString();
URL url = new URL(baseUrl);
urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setRequestMethod("GET");
urlConnection.connect();
InputStream inputStream = urlConnection.getInputStream();
StringBuffer buffer = new StringBuffer();
if (inputStream == null) {
return null;
}
reader = new BufferedReader(new InputStreamReader(inputStream));
String line;
while ((line = reader.readLine()) != null) {
buffer.append(line + "\n");
}
if (buffer.length() == 0) {
return null;
}
apiJsonStr = buffer.toString();
} catch (IOException e) {
return null;
} finally {
if (urlConnection != null) {
urlConnection.disconnect();
}
if (reader != null) {
}
}
return apiJsonStr;
}
@Override
protected void onPostExecute(String stringReturn) {
super.onPostExecute(stringReturn);
mListener.allCardsResponse(stringReturn);
}