I have created an android
application which gets some data from google place api
. It returns a Json object
in AsyncTask
. Then I put every record into list
of object and on that list I do some operation which needs data from DB
. Because it is simple app I use JDBC
to connect to database
. When I do some operation on above list my UI
blocks for long time. The methods looks like:
private void generateMap(LatLng latLng, String distance) {
List<WashLocation> list = new ArrayList<>();
MyAsyncTask myAsyncTask = new MyAsyncTask();
try {
JSONObject jsonObject = myAsyncTask.execute(latitude, longitude, radius).get();
String lat;
String lng;
String name = "";
String city = "";
String placeId = "";
if (jsonObject != null) {
if (jsonObject.has("results")) {
JSONArray jsonArray = jsonObject.getJSONArray("results");
for (int n = 0; n < jsonArray.length(); n++) {
JSONObject jsonObject1 = jsonArray.getJSONObject(n);
lat = jsonObject1.getJSONObject("geometry").getJSONObject("location").get("lat").toString();
lng = jsonObject1.getJSONObject("geometry").getJSONObject("location").get("lng").toString();
JSONObject oName = jsonArray.getJSONObject(n);
if (oName.has("name")) {
name = oName.getString("name");
}
JSONObject oVicinity = jsonArray.getJSONObject(n);
if (oVicinity.has("vicinity")) {
city = oVicinity.getString("vicinity");
}
JSONObject oPlaceId = jsonArray.getJSONObject(n);
if (oPlaceId.has("place_id")) {
placeId = oPlaceId.getString("place_id");
}
WashLocation w = new WashLocation(name, lat, lng, 0, getCity(city), null, placeId);
list.add(w);
}
}
}
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
}
for(WashLocation washLocation: list) {
try {
WashLocation washLocation1 = new CheckPlaceID(washLocation).execute().get();
washLocations.add(washLocation1);
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
}
}
Inner class:
private class CheckPlaceID extends AsyncTask<Void, Void, WashLocation> {
private WashLocation washLocation;
String placeId;
public CheckPlaceID(WashLocation washLocation) {
this.washLocation = washLocation;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected void onPostExecute(WashLocation washLocation) {
super.onPostExecute(washLocation);
}
@Override
protected WashLocation doInBackground(Void... params) {
DataBaseHelperRemote dataBaseHelperRemote = new DataBaseHelperRemote();
List<WashLocationRemoteDB> allWashLocationFromRemoteDB = dataBaseHelperRemote.getAllWashLocationFromRemoteDB(); //data from DB
WashLocationRemoteDB washLocationRemoteDBByPlaceId = dataBaseHelperRemote.getWashLocationRemoteDBByPlaceId(washLocation.getPlaceId()); //data from DB
if(washLocation != null){
if(washLocationRemoteDBByPlaceId != null){
if(allWashLocationFromRemoteDB != null){
for(WashLocationRemoteDB db : allWashLocationFromRemoteDB){
if(db.getPlaceId().equalsIgnoreCase(washLocation.getPlaceId())){
washLocation.setWashName("tu coś jest!");
break;
}
}
}
}
}
return washLocation;
}
How can I improve the speed of that process and to not bocking the UI
?