-1
你好我試圖從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<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();
};
}
});
}
下面是我如何努力吸引我的領域和錯誤總是發生在包含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++;
}
}
}
right我知道了,但我從來沒有使用異步taks,所以我怎麼能做到這一點? –
下面是一個示例:https://www.google.it/amp/s/androidresearch.wordpress.com/2012/03/17/understanding-asynctask-once-and-forever/amp/ onPostExecute()函數被執行在主線程上,你可以執行你在那裏繪圖 –