-1

Hello I am trying to draw polygones on a google map after I get the data for them from an HTTP call. I always get the same error:

FATAL EXCEPTION: OkHttp Dispatcher
Process: com.example.rtuya.secmerev2, PID: 1011                                                                    
java.lang.IllegalStateException: Not on the main thread

Here is how I do my HTTP call and then how I try to draw my areas:

 private void getZones() throws JSONException {
        Request request = new Request.Builder().url(getString(R.string.main_url) + "/api/getZones")
                .headers(buildStandardHeaders(Stormpath.accessToken()))
                .get()
                .build();

        okHttpClient.newCall(request).enqueue(new Callback() {
            @Override public
            void onFailure(Call call, IOException e) {
                Log.d("DEBUG", "ERROR");
            }
            @Override public void onResponse(Call call, Response response)throws IOException {
                try {
                    JSONArray responseArray = new JSONArray(response.body().string());
                    zones = new ArrayList<ZoneData>();
                    for (int i=0; i < responseArray.length(); i++) {
                        db.addZone(new ZoneData(responseArray.getJSONObject(i)));
                        zones.add(new ZoneData(responseArray.getJSONObject(i)));
                    }
                    isTrackingServiceRunning = startService(new Intent(ActivityMain.this, ServiceTracking.class));
                    bindService(new Intent(ActivityMain.this, ServiceTracking.class), trackerServiceConnection, Context.BIND_AUTO_CREATE);
                    drawAreasOnDashboard();
                } catch (JSONException e) {
                    e.printStackTrace();
                };
            }
        });
    }

Below is how I try to draw my Areas and the error always happens on the line that contains areaMap.drawPolygones():

public void drawAreas() {
        int polygoneFillingIndex = 1;
        if(ActivityMain.zones != null) {
            for (ZoneData zone : ActivityMain.zones) {
                int color;
                if ((polygoneFillingIndex % 2) == 0) {
                    color = R.color.polygoneFillingBlue;
                } else {
                    color = R.color.polygoneFilingGreen;
                }
                areasMap.addPolygon(new PolygonOptions()
                        .add(zone.getP1(), zone.getP2(), zone.getP3(), zone.getP4())
                        .strokeColor(ResourcesCompat.getColor(getResources(), R.color.polygoneStroke, null))
                        .fillColor(ResourcesCompat.getColor(getResources(), color, null))
                        .zIndex(Float.valueOf(zone.getPosititionInArray()))
                        .clickable(true));

                polygoneFillingIndex++;
            }
        }
    }
0xtuytuy
  • 1,494
  • 6
  • 26
  • 51

1 Answers1

1

That error logs says exactly where you are wrong. Android does not allow you to run http requests on the main thread so that it won't be blocked by it. You have to encapsulate those http requests in AsyncTasks, you can find tons of examples here on SO or anywhere in the web

  • right I got that but I have never used asynch taks so how could I do it ? – 0xtuytuy Apr 06 '17 at 20:51
  • Here's an example: https://www.google.it/amp/s/androidresearch.wordpress.com/2012/03/17/understanding-asynctask-once-and-forever/amp/ the onPostExecute() function is executed on the main thread and you can perform you drawing there – Alberto Brambilla Apr 06 '17 at 20:54