I am developing an application in Android Studio
(api min is 10). In this application I charge the data I get through a url. When the device in which I test it is larger than api 10, it performs correctly. However, when it is api 10, nothing is received. How can I solve this?
The code of the task in background is the following:
private class DownloadJsonTask extends AsyncTask<String, Void, ArrayList<String>> {
private ArrayList<String> parseJsonCentrosFile(String jsonLibrosInformation)
throws JSONException {
//Parseo de los datos
}
@Override
protected ArrayList<String> doInBackground(String... urls) {
try {
return downloadUrl(urls[0]);
} catch (IOException e) {
return null;
}
}
private ArrayList<String> downloadUrl(String myUrl) throws IOException {
InputStream is = null;
try {
is = openHttpInputStream(myUrl);
try {
return parseJsonCentrosFile(streamToString(is));
} catch (JSONException e) {
e.printStackTrace();
}
} finally {
if (is != null) {
is.close();
}
}
return null;
}
public String streamToString(InputStream stream) throws IOException,
UnsupportedEncodingException {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
int bufferSize = 1024;
byte[] buffer = new byte[bufferSize];
int length = 0;
do {
length = stream.read(buffer);
if (length != -1) {
baos.write(buffer, 0, length);
}
} while (length != -1);
return baos.toString("UTF-8");
}
@Override
protected void onPostExecute(ArrayList<String> result) {
//Código de onPostExecute
}
}
Get connection code:
private InputStream openHttpInputStream(String myUrl)
throws MalformedURLException, IOException, ProtocolException {
InputStream is;
URL url = new URL(myUrl);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.connect();
is = conn.getInputStream();
return is;
}
The file that is accessed by the url does not download it, so I think the error is that. How could I download the json and then read it from the file that I downloaded?
The logCat error is as follows:
04-14 18:36:53.281 793-807/
es.libros/AndroidRuntime:FATAL EXCEPTION: AsyncTask #1
java.lang.RuntimeException: An error occured while executing doInBackground()
at android.os.AsyncTask$3.done(AsyncTask.java:278)
at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
at java.util.concurrent.FutureTask.run(FutureTask.java:137)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:208)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
at java.lang.Thread.run(Thread.java:856)
Caused by: java.lang.OutOfMemoryError
at java.io.ByteArrayOutputStream.expand(ByteArrayOutputStream.java:91)
at java.io.ByteArrayOutputStream.write(ByteArrayOutputStream.java:201)
at es.user.bares_restaurantes.BarListFragment$DownloadJsonTask.streamToString(BarListFragment.java:703)
at es.user.bares_restaurantes.BarListFragment$DownloadJsonTask.downloadUrl(BarListFragment.java:677)
at es.user.bares_restaurantes.BarListFragment$DownloadJsonTask.doInBackground(BarListFragment.java:664)
at es.bares_restaurantes.BarListFragment$DownloadJsonTask.doInBackground(BarListFragment.java:328)
at android.os.AsyncTask$2.call(AsyncTask.java:264)
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
at java.util.concurrent.FutureTask.run(FutureTask.java:137)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:208)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
at java.lang.Thread.run(Thread.java:856)