title: Error Parsing Data java.lang.IllegalStateException: Not on the main thread when populating Google map
body: <p>Ok, right now I am trying to populate a Google Map given a Name, Lat and Long. These values are coming in from a database via PHP script. This is all good and working fine. But I've run into a problem: I am populating the map with markers in an AsyncTask. This is here I get the error</p>
  
  <p><code>07-10 14:47:41.035: E/log_tag(21034): Error Parsing Data java.lang.IllegalStateException: Not on the main thread</code></p>
  
  <p>This is the <strong>only</strong> error I get and it doesn't specify where it is. I'm guessing it is because the map isn't being populated on the main thread? But I can't do network stuff on the main thread, so I can't remove it from the AsyncTask. Any ideas on how I can fix this? Thanks</p>
  
  <p>Code:</p>
  
  <pre><code>public class MapActivity extends ActionBarActivity implements LocationListener {
  
  private GoogleMap map;
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_map);
  
      map = ((MapFragment) getFragmentManager().findFragmentById(R.id.map))
              .getMap();
  
      MapLocations myTask = new MapLocations();
      myTask.execute();
  
  }
  
  private class MapLocations extends AsyncTask&lt;Void, Void, Void&gt; {
      String msg = "Done";
      ArrayList&lt;Marker&gt; arrayOfMarkers;
  
      // Decode image in background.
      @Override
      protected Void doInBackground(Void... params) {
  
          String result = "";
          InputStream isr = null;
          try {
              HttpClient httpclient = new DefaultHttpClient();
              HttpPost httppost = new HttpPost(
                      "MYPHPSCRIPT");
              HttpResponse response = httpclient.execute(httppost);
              HttpEntity entity = response.getEntity();
              isr = entity.getContent();
              // resultView.setText("connected");
          } catch (Exception e) {
              Log.e("log_tag", "Error in http connection " + e.toString());
          }
          // convert response to string
          try {
              BufferedReader reader = new BufferedReader(
                      new InputStreamReader(isr, "iso-8859-1"), 8);
              StringBuilder sb = new StringBuilder();
              String line = null;
              while ((line = reader.readLine()) != null) {
                  sb.append(line + "\n");
              }
              isr.close();
  
              result = sb.toString();
          } catch (Exception e) {
              Log.e("log_tag", "Error  converting result " + e.toString());
          }
  
          // parse json data
          try {
              JSONArray jArray = new JSONArray(result);
  
              for (int i = 0; i &lt; jArray.length(); i++) {
                  final JSONObject json = jArray.getJSONObject(i);
                  // counter++;
  
                  map.addMarker(new MarkerOptions().position(
                          new LatLng(Double.parseDouble(json.getString("PlaceLat")), Double.parseDouble(json
                                  .getString("PlaceLong")))).title(
                          json.getString("PlaceTitle")));
              }
  
          } catch (Exception e) {
              // TODO: handle exception
              Log.e("log_tag", "Error Parsing Data " + e.toString());
          }
          return null;
      }
  }
  
  }
  </code></pre>
  
  <p>The error is based around this code
   try {
                  JSONArray jArray = new JSONArray(result);</p>
  
  <pre><code>            for (int i = 0; i &lt; jArray.length(); i++) {
                  final JSONObject json = jArray.getJSONObject(i);
                  // counter++;
  
                  map.addMarker(new MarkerOptions().position(
                          new LatLng(Double.parseDouble(json.getString("PlaceLat")), Double.parseDouble(json
                                  .getString("PlaceLong")))).title(
                          json.getString("PlaceTitle")));
              }
  </code></pre>