你好我试图从HTTP调用中获取他们的数据后,在谷歌地图上绘制多边形。我总是得到同样的错误:安卓在谷歌地图上绘制多边形后请求HTTP请求
FATAL EXCEPTION: OkHttp Dispatcher
Process: com.example.rtuya.secmerev2, PID: 1011
java.lang.IllegalStateException: Not on the main thread
这里是我做我的HTTP调用,然后我怎么试着画出我的领域:
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();
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();
};
}
});
}
下面是我如何努力吸引我的领域和错误总是发生在包含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++;
}
}
}
2017-04-06
Rémi