Google Autocomplete - duplicate requests



  • The program uses the auto filling. There's a lot of code below, but I hope someone has faced such a problem or will force it.

    In order to save the number of requests, I only follow the request for the server on the second letter and each, in the fourth letter, and also if the last label is in place.

    The problem is, at the right moments, there's not a single request but two.

    Installed two Loga PlaceProvider.java (at the bottom):

    Log.e("AUTOCOMPLETE", String.valueOf(resultList));
    

    and GooglePlacesAutocompleteAdapter-state getFilter()-State performFiltering(CharSequence constraint)

    Log.e("AUTOCOMPLETE", "___Last: " + lastSym + " " + String.valueOf(constraint) + constraint.length());
    

    Introducing four letters:

    введите сюда описание изображения

    Leg result

    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: [Магнитогорск, Челябинская область, Россия, Магнитка, Челябинская область, Россия, Магнитный, Курская область, Россия, Магнитский, Челябинская область, Россия, Магна, Республика Калмыкия, Россия]
    

    The log shows that the request is sent twice, the result of which is different. What does that have to do with it?

    Annex code:

    Activations:

        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&lt;String&gt;(jsonArray.length());
                    for (int i = 0; i &lt; 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&lt;String&gt;();
        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&lt;String&gt; 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 &amp;&amp; results.count &gt; 0) {
                    resultList = (List&lt;String&gt;) results.values;
                    notifyDataSetChanged();
                } else {
                    notifyDataSetInvalidated();
                }
            }
        };
    }
    

    }



  • The code was another developer, it's the heat of the old code.




Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2