title: Multiple AsyncTask, ConcurrentModificationException. Help! - Android
body: <p>I'm making a photo streaming app for Android. In order to avoid memory issues, I'm running two AsyncTasks to pull down data. One gets and parses a JSON object into a custom object (shown here:) </p>
  
  <pre><code>    package edu.iastate.its.webdev.training.photostream;
  
  import java.io.IOException;
  import java.io.InputStream;
  import java.io.InputStreamReader;
  import java.io.Reader;
  import java.io.StringReader;
  import java.io.UnsupportedEncodingException;
  
  import org.apache.http.HttpResponse;
  import org.apache.http.client.ClientProtocolException;
  import org.apache.http.client.methods.HttpGet;
  import org.apache.http.impl.client.DefaultHttpClient;
  import org.json.JSONArray;
  import org.json.JSONException;
  import org.json.JSONObject;
  
  import android.os.AsyncTask;
  import android.util.JsonReader;
  
  public class StringAsync extends AsyncTask&lt;String, Integer, String&gt;
  {
  
  
      @Override
      protected String doInBackground(String...urls) 
      {
  
          InputStream content = null;
          DefaultHttpClient temp = new DefaultHttpClient();
  
          HttpGet temp2 = new HttpGet(urls[0]);
  
          try {
              HttpResponse temp3 = temp.execute(temp2);
              content = temp3.getEntity().getContent();
          } catch (ClientProtocolException e) {
              // TODO Auto-generated catch block
              e.printStackTrace();
          } catch (IOException e) {
              // TODO Auto-generated catch block
              e.printStackTrace();
          }
  
          Reader reader = null;
  
          try {
              reader = new InputStreamReader(content, "UTF-8");
          } catch (UnsupportedEncodingException e) {
              // TODO Auto-generated catch block
              e.printStackTrace();
          }
  
  
          char[] buffer = new char[Integer.MAX_VALUE/200];
          try {
              reader.read(buffer);
          } catch (IOException e) {
              // TODO Auto-generated catch block
              e.printStackTrace();
          }
          return new String(buffer);
      }
  
  
      @Override
      protected void onPostExecute(String result) 
      {
          if(result == null) throw new IllegalStateException("No result");
          if(result.substring(0, 3).equals("null")) result = result.substring(4, result.length()-1);
  
          super.onPostExecute(result);
  
  
  
          JsonReader temp = new JsonReader(new StringReader(result));
          try {
              temp.beginArray();
          } catch (IOException e) {
              // TODO Auto-generated catch block
              e.printStackTrace();
          }
  
  
          temp.setLenient(true);
          try {
              while(temp.hasNext())
              {
                  PhotoItem tempItem = new PhotoItem("null", 0, 0, "null", "null", "null");
                  temp.beginObject();
                  temp.skipValue();
  
                  tempItem.setId(temp.nextInt());
                  temp.skipValue();
  
                  tempItem.setName(temp.nextString());
                  temp.skipValue();
  
                  tempItem.setDescription(temp.nextString());
                  temp.skipValue();
  
                  temp.skipValue();
                  temp.skipValue();
  
                  temp.skipValue();
                  temp.skipValue();
  
                  temp.skipValue();
                  temp.skipValue();
  
                  tempItem.setThumbImageURL(temp.nextString());
                  temp.skipValue();
  
                  temp.skipValue();
                  temp.skipValue();
  
                  tempItem.setFullImageURL(temp.nextString());
                  temp.skipValue();
  
                  tempItem.setViewsNumber(temp.nextInt());
                  temp.skipValue();
  
                  temp.skipValue();
                  temp.skipValue();
  
                  temp.skipValue();
                  //temp.skipValue();
  
                  temp.endObject();
                  MainActivity.photos.add(tempItem);
              }
              //temp.endArray();
              temp.close();
          } catch (IOException e) {
              // TODO Auto-generated catch block
              e.printStackTrace();
          }
      }
  }
  </code></pre>
  
  <p>The other pulls down the images from the URLs in the JSON:</p>
  
  <pre><code>package edu.iastate.its.webdev.training.photostream;
  
  import java.io.IOException;
  import java.io.InputStream;
  import java.net.HttpURLConnection;
  import java.util.ArrayList;
  
  import android.graphics.Bitmap;
  import android.graphics.BitmapFactory;
  import android.os.AsyncTask;
  
  public class PhotoAsync extends AsyncTask&lt;ArrayList&lt;PhotoItem&gt;, Integer, Bitmap&gt;
  {
  
      private ArrayList&lt;PhotoItem&gt; temp = null;
      @Override
      protected synchronized Bitmap doInBackground(ArrayList&lt;PhotoItem&gt;...idArg) 
      {
  
  
  
          temp = idArg[0];
          String tempURL = "null";
  
          for(PhotoItem p : temp)
          {
              p.setThumbImageBM(getBitmapFromURL(p.getThumbURL()));
              p.setFullImageBM(getBitmapFromURL(p.getFullURL()));
          }
  
          if(temp.size() &gt; 0)
          return temp.get(0).getLargeImage();
          else
          return null;
      }
  
      @Override
      protected synchronized void onPostExecute(Bitmap result)
      {
  
      }
  
      private Bitmap getBitmapFromURL(String src) 
      {
          try {
              java.net.URL url = new java.net.URL(src);
              HttpURLConnection connection = (HttpURLConnection) url
                      .openConnection();
              connection.setDoInput(true);
              connection.connect();
              InputStream input = connection.getInputStream();
  
              BitmapFactory.Options option = new BitmapFactory.Options();
              option.inSampleSize = 8;
  
              Bitmap myBitmap = BitmapFactory.decodeStream(input);
              return myBitmap;
          } catch (IOException e) {
              e.printStackTrace();
              return null;
          }
      }
  }
  </code></pre>
  
  <p>I keep getting this error:</p>
  
  <pre><code>04-15 17:52:04.539: W/dalvikvm(23235): threadid=14: thread exiting with uncaught exception (group=0x417c0da0)
  04-15 17:52:04.539: E/AndroidRuntime(23235): FATAL EXCEPTION: AsyncTask #4
  04-15 17:52:04.539: E/AndroidRuntime(23235): Process: edu.iastate.its.webdev.training.photostream, PID: 23235
  04-15 17:52:04.539: E/AndroidRuntime(23235): java.lang.RuntimeException: An error occured while executing doInBackground()
  04-15 17:52:04.539: E/AndroidRuntime(23235):    at android.os.AsyncTask$3.done(AsyncTask.java:300)
  04-15 17:52:04.539: E/AndroidRuntime(23235):    at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
  04-15 17:52:04.539: E/AndroidRuntime(23235):    at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
  04-15 17:52:04.539: E/AndroidRuntime(23235):    at java.util.concurrent.FutureTask.run(FutureTask.java:242)
  04-15 17:52:04.539: E/AndroidRuntime(23235):    at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
  04-15 17:52:04.539: E/AndroidRuntime(23235):    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
  04-15 17:52:04.539: E/AndroidRuntime(23235):    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
  04-15 17:52:04.539: E/AndroidRuntime(23235):    at java.lang.Thread.run(Thread.java:841)
  04-15 17:52:04.539: E/AndroidRuntime(23235): Caused by: java.util.ConcurrentModificationException
  04-15 17:52:04.539: E/AndroidRuntime(23235):    at java.util.ArrayList$ArrayListIterator.next(ArrayList.java:573)
  04-15 17:52:04.539: E/AndroidRuntime(23235):    at edu.iastate.its.webdev.training.photostream.PhotoAsync.doInBackground(PhotoAsync.java:25)
  04-15 17:52:04.539: E/AndroidRuntime(23235):    at edu.iastate.its.webdev.training.photostream.PhotoAsync.doInBackground(PhotoAsync.java:1)
  04-15 17:52:04.539: E/AndroidRuntime(23235):    at android.os.AsyncTask$2.call(AsyncTask.java:288)
  04-15 17:52:04.539: E/AndroidRuntime(23235):    at java.util.concurrent.FutureTask.run(FutureTask.java:237)
  04-15 17:52:04.539: E/AndroidRuntime(23235):    ... 4 more
  </code></pre>
  
  <p>I'm a newb to networking code in general, and a newb to JSON. Any help is appreciated! </p>