Hallo Ich versuche Polygone auf einer Google-Karte zu zeichnen, nachdem ich die Daten für sie von einem HTTP-Aufruf erhalten. Ich bekomme immer den gleichen Fehler:Android Draw Polygone auf Google Map nach HTTP-Anfrage
FATAL EXCEPTION: OkHttp Dispatcher
Process: com.example.rtuya.secmerev2, PID: 1011
java.lang.IllegalStateException: Not on the main thread
Hier ist, wie ich meinen HTTP-Aufruf zu tun und dann, wie ich versuche, meine Bereiche zu zeichnen:
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();
};
}
});
}
Unten ist, wie ich versuche, meine Bereiche zu ziehen und die Fehler treten immer auf der Linie, die areaMap.drawPolygones enthält():
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++;
}
}
}
Recht habe ich das, aber ich habe nie asynch taks so wie kann ich es tun? –
Hier ein Beispiel: https://www.google.it/amp/s/androidresearch.wordpress.com/2012/03/17/understanding-asynctask-once-and-forever/amp/ Die Funktion onPostExecute() wird ausgeführt auf dem Hauptthread und Sie können Sie dort zeichnen ausführen –