So my object is simple and need to know if I can either make my code better all the way around or remove sleep from my code and somehow wait for the async task to finish with getting data from a database before loading maps.
After fighting with trying to figure out why my code wasn't working as it should I put 5 different LOG.i in labeled STEP 1 to STEP 5 and in the order I expected things to run.
Well after looking at the logs when I did this, it was running in this order, STEP 4, then 1, 5 , 2, then 3. So I added a temp sleep timer in and now it is showing in logcat the steps in order 1 through 5.
I know a 10 second sleep is too long. Is there a way to do a sleep on a flag instead of a predetermined amount of time, like in a while loop. Something like while vara = 0 and varb = 0 sleep a milisecond or something like that?
Or is there just a better way to write my code so that way it is more efficient and not have to use sleep at all?
Below is my current code:
import android.os.AsyncTask;
import android.os.Bundle;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.app.restaurant.bowzers.R;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
import org.json.JSONArray;
import org.json.JSONObject;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.concurrent.TimeUnit;
public class TruckFragment2 extends Fragment{
double mylocationLong, mylocationLat;
String mySB;
GoogleMap googleMap;
GoogleMap gMap;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Initialize view
View view=inflater.inflate(R.layout.fragment_truck, container, false);
mylocationLat = 39.9;
mylocationLong = -82.83;
getJSON("https://bowzershotdogstand.com/app/get_location.php");
try {
TimeUnit.SECONDS.sleep(10);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
Log.i("STEP 4",mylocationLong + "," + mylocationLat);
LatLng latLng = new LatLng(mylocationLat,mylocationLong);
// Initialize map fragment
SupportMapFragment supportMapFragment=(SupportMapFragment)
getChildFragmentManager().findFragmentById(R.id.truck_map);
// Async map
supportMapFragment.getMapAsync(new OnMapReadyCallback() {
@Override
public void onMapReady(@NonNull GoogleMap googleMap) {
gMap=googleMap;
Log.i("STEP 5", mylocationLong + "," + mylocationLat);
MarkerOptions markerOptions=new MarkerOptions();
// Set position of marker
markerOptions.position(latLng);
// Set title of marker
markerOptions.title(latLng.latitude+" : "+latLng.longitude);
// Remove all marker
// gMap.clear();
// Animating to zoom the marker
gMap.animateCamera(CameraUpdateFactory.newLatLngZoom(latLng,16));
// Add marker on map
gMap.addMarker(markerOptions);
// }
// });
}
});
// Return view
return view;
}
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
}
public void getJSON(final String urlWebService) {
class GetJSON extends AsyncTask<Void, Void, String> {
protected void onPreExecute() {
super.onPreExecute();
}
protected void onPostExecute(String s) {
super.onPostExecute(s);
}
protected String doInBackground(Void... Voids) {
Log.i("STEP 1", urlWebService);
try {
URL url = new URL(urlWebService);
HttpURLConnection con = (HttpURLConnection) url.openConnection();
StringBuilder sb = new StringBuilder();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(con.getInputStream()));
String json;
while ((json = bufferedReader.readLine()) != null) {
sb.append((json + "\n"));
}
Log.i("STEP 2", String.valueOf(sb));
mySB = String.valueOf(sb);
// JSONArray JA = new JSONArray(mySB);
JSONArray JA = new JSONArray(mySB);
for(int i =0 ;i <JA.length(); i++){
JSONObject JO = (JSONObject) JA.get(i);
mylocationLong = Double.parseDouble(JO.getString("longitude"));
mylocationLat = Double.parseDouble(JO.getString("lattitude"));
Log.i("STEP 3", mylocationLat + "," + mylocationLong);
LatLng marker = new LatLng(mylocationLong,mylocationLat);
gMap.clear();
gMap.addMarker(new MarkerOptions().position(marker).title("Marker Somewhere"));
gMap.moveCamera(CameraUpdateFactory.newLatLng(marker));
break;
}
return sb.toString().trim();
} catch (Exception e) {
return null;
}
}
}
GetJSON getJSON = new GetJSON();
getJSON.execute();
}
}
AsyncTaskits been deprecated for years now.. You can use any of alternate .