Google Autocomplete-duplicate requests
I use autofill in the program. Below is a lot of code, but I hope someone has encountered a similar problem, or will master it ;-)
To save the number of requests, I make a request to the server only on the second letter and each multiple of the 4th, as well as if the last character entered is a space or hyphen.
The problem is that at the right moments, not one request is sent, but two.
Inserted two PlaceProvider logs.java (at the bottom):
Log.e("AUTOCOMPLETE", String.valueOf(resultList));
And GooglePlacesAutocompleteAdapter -> getFilter() -> performFiltering(CharSequence constraint)
Log.e("AUTOCOMPLETE", "___Last: " + lastSym + " " + String.valueOf(constraint) + constraint.length());
I enter 4 letters:
The result in the log
05-24 20:44:16.077 24614-25388/com.khasang.forecast E/AUTOCOMPLETE: ___Last: н Магн4
05-24 20:44:16.077 24614-25388/com.khasang.forecast E/AUTOCOMPLETE: [Малое Козино, Нижегородская область, Россия, Магнитогорск, Челябинская область, Россия, Малаховка, Московская область, Россия, Малоярославец, Калужская область, Россия, Мантурово, Костромская область, Россия]
05-24 20:44:16.845 24614-25388/com.khasang.forecast E/AUTOCOMPLETE: ___Last: н Магн4
05-24 20:44:16.845 24614-25388/com.khasang.forecast E/AUTOCOMPLETE: [Магнитогорск, Челябинская область, Россия, Магнитка, Челябинская область, Россия, Магнитный, Курская область, Россия, Магнитский, Челябинская область, Россия, Магна, Республика Калмыкия, Россия]
Judging by the log, it turns out that the request is sent twice, and the result of executing requests is different. What can this be related to?
Application code:
Activiti:
final GooglePlacesAutocompleteAdapter googlePlacesAutocompleteAdapter = new GooglePlacesAutocompleteAdapter(this, R.layout.autocomplete_city_textview_item);
chooseCity = (DelayedAutoCompleteTextView) view.findViewById(R.id.editTextCityName);
chooseCity.setAdapter(googlePlacesAutocompleteAdapter);
PlacePprovider.java
public class PlaceProvider {
private final static String TAG = PlaceProvider.class.getSimpleName();
private final static String PLACE_API_BASE_URL = "https://maps.googleapis.com/maps/api/place/autocomplete/json";
private final static String API_KEY = MyApplication.getAppContext().getString(R.string.place_provider_key);
ArrayList<String> resultList = null;
public ArrayList<String> autocomplete(String input) {
try {
String URL = PLACE_API_BASE_URL + "?key="
+ API_KEY + "&input="
+ URLEncoder.encode(input, "utf8")
+ "&types=(cities)";
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 Place API failure");
}
@Override
public void onResponse(Response response) throws IOException {
String jsonData = response.body().string();
Log.v(TAG, jsonData);
try {
JSONObject jsonObject = new JSONObject(jsonData);
JSONArray jsonArray = jsonObject.getJSONArray("predictions");
resultList = new ArrayList<String>(jsonArray.length());
for (int i = 0; i < jsonArray.length(); i++) {
Log.i(TAG, jsonArray.getJSONObject(i).getString("description"));
resultList.add(jsonArray.getJSONObject(i).getString("description"));
}
} catch (JSONException e) {
Log.e(TAG, e.getLocalizedMessage());
}
}
});
} catch (UnsupportedEncodingException e) {
Log.e(TAG, e.getLocalizedMessage());
}
Log.e("AUTOCOMPLETE", String.valueOf(resultList));
return resultList;
}
}
Adapter
public class GooglePlacesAutocompleteAdapter extends ArrayAdapter
implements Filterable {
private List<String> resultList;
private PlaceProvider mPlaceProvider;
private final static int MAX_RESULT = 10;
private Context mContext;
public GooglePlacesAutocompleteAdapter(Context context, int textViewResourceId) {
super(context, textViewResourceId);
mContext = context;
resultList = new ArrayList<String>();
mPlaceProvider = new PlaceProvider();
}
@Override
public int getCount() {
return resultList.size();
}
@Override
public String getItem(int position) {
return resultList.get(position);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
LayoutInflater inflater = LayoutInflater.from(mContext);
convertView = inflater.inflate(R.layout.autocomplete_city_textview_item, parent, false);
}
TextView description = (TextView) convertView.findViewById(R.id.autocomplete_list_item);
description.setText(getItem(position));
return convertView;
}
@Override
public Filter getFilter() {
return new Filter() {
FilterResults filterResults = new FilterResults();
@Override
protected FilterResults performFiltering(CharSequence constraint) {
if (constraint != null) {
char lastSym = constraint.charAt(constraint.length() - 1);
if ((constraint.length() % 4 == 0) || (lastSym == ' ') || (lastSym == '-') || (constraint.length() == 2)) {
List<String> predictions = mPlaceProvider.autocomplete(constraint.toString());
filterResults.values = predictions;
filterResults.count = predictions.size();
}
}
return filterResults;
}
@Override
protected void publishResults(CharSequence constraint, FilterResults results) {
if (results != null && results.count > 0) {
resultList = (List<String>) results.values;
notifyDataSetChanged();
} else {
notifyDataSetInvalidated();
}
}
};
}
}
1 answers
I figured out: the code was from another developer - these are flaws in the old code