It is not possible to get data from the stream in which I request the coordinates of the location.
public void requestCoordinates(final String input, final ICoordinateReceiver receiver, final boolean updateTimezone) { try { final String URL = PLACE_API_BASE_URL + "?key=" + API_KEY + "&address=" + URLEncoder.encode(input, "utf8"); OkHttpClient client = new OkHttpClient(); Request request = new Request.Builder() .url(URL) .build(); Call call = client.newCall(request); call.enqueue(new Callback() { @Override public void onFailure(Request request, IOException e) { Log.d(TAG, "Request to Google Maps Geocoding API failure"); } @Override public void onResponse(Response response) throws IOException { String jsonData = response.body().string(); try { JSONObject jsonObject = new JSONObject(jsonData); JSONArray jsonArray = jsonObject.getJSONArray("results"); String status = jsonObject.getString("status"); if (!status.equals("OK")) { String log = "GoogleMapsGeocoding: url <" + URL + "> response status <" + status + ">"; throw new JSONException(log); } JSONObject jsonGeometry = new JSONObject(jsonArray.getJSONObject(0).getString("geometry")); JSONObject jsonLocation = new JSONObject(jsonGeometry.getString("location")); Coordinate coordinate = new Coordinate(); coordinate.setLatitude(Double.parseDouble(jsonLocation.getString("lat"))); coordinate.setLongitude(Double.parseDouble(jsonLocation.getString("lng"))); receiver.updatePositionCoordinate(input, coordinate); if (updateTimezone) { GoogleMapsTimezone googleMapsTimezone = new GoogleMapsTimezone(); googleMapsTimezone.requestTimezone(input); } } catch (JSONException e) { Log.e(TAG, e.getLocalizedMessage()); AppUtils.showInfoMessage(MyApplication.getAppContext().getString(R.string.invalid_lang_long_used)).show(); if (Fabric.isInitialized()) { Crashlytics.logException(e); } } } }); } catch (UnsupportedEncodingException e) { AppUtils.showInfoMessage(MyApplication.getAppContext().getString(R.string.invalid_lang_long_used)).show(); Log.e(TAG, e.getLocalizedMessage()); } } I transfer the interface on which it is necessary to return the obtained data. Is it not possible to do this? What is better to do in this case? AsyncTask?
Interface implementation
GoogleMap map; public void deleteAllMarkers() { map.clear(); } @Override public void updatePositionCoordinate(String city, Coordinate coordinate) { deleteAllMarkers(); setNewMarker(coordinate, city); setCameraPosition(coordinate, getDefaultZoom(), 0, 0); } Mistake:
06-13 00:32:14.879 15370-15749/com.khasang.forecast E/UncaughtException: java.lang.IllegalStateException: Not on the main thread at maps.fgb(Unknown Source) at maps.zDa(Unknown Source) at maps.ag.te(Unknown Source) at uz.onTransact(:com.google.android.gms.DynamiteModulesB:205) at android.os.Binder.transact(Binder.java:387) at com.google.android.gms.maps.internal.IGoogleMapDelegate$zza$zza.clear(Unknown Source) at com.google.android.gms.maps.GoogleMap.clear(Unknown Source) at com.khasang.forecast.Maps.deleteAllMarkers(Maps.java:126) at com.khasang.forecast.Maps.updatePositionCoordinate(Maps.java:192) at com.khasang.forecast.api.GoogleMapsGeocoding$1.onResponse(GoogleMapsGeocoding.java:68) at com.squareup.okhttp.Call$AsyncCall.execute(Call.java:168)