コード例 #1
0
  public ArrayList<PlacesSuggestionsBean> autocomplete(String input) {

    HttpURLConnection conn = null;
    StringBuilder jsonResults = new StringBuilder();
    try {

      StringBuilder sb = new StringBuilder(PLACES_API_BASE + TYPE_AUTOCOMPLETE + OUT_JSON);
      sb.append("?key=" + API_KEY);
      // sb.append("&components=country:india"); 12,Shikshak Nagar Airport Road,Indore Madhya
      // Pradesh
      sb.append("&input=" + URLEncoder.encode(input, "utf8"));

      URL url = new URL(sb.toString());

      System.out.println("URL: " + url);
      conn = (HttpURLConnection) url.openConnection();
      InputStreamReader in = new InputStreamReader(conn.getInputStream());

      // Load the results into a StringBuilder
      int read;
      char[] buff = new char[1024];
      while ((read = in.read(buff)) != -1) {
        jsonResults.append(buff, 0, read);
      }
    } catch (MalformedURLException e) {
      Log.e("sdf", "Error processing Places API URL", e);
      return resultList;
    } catch (IOException e) {
      Log.e("dsd", "Error connecting to Places API", e);
      return resultList;
    } finally {
      if (conn != null) {
        conn.disconnect();
      }
    }

    try {

      // Create a JSON object hierarchy from the results
      JSONObject jsonObj = new JSONObject(jsonResults.toString());
      JSONArray predsJsonArray = jsonObj.getJSONArray("predictions");

      // Extract the Place descriptions from the results
      resultList = new ArrayList<PlacesSuggestionsBean>(predsJsonArray.length());
      for (int i = 0; i < predsJsonArray.length(); i++) {
        System.out.println(predsJsonArray.getJSONObject(i).getString("description"));
        System.out.println("============================================================");
        PlacesSuggestionsBean placesSuggestionsBean = new PlacesSuggestionsBean();
        placesSuggestionsBean.setDescription(
            predsJsonArray.getJSONObject(i).getString("description"));
        placesSuggestionsBean.setId(predsJsonArray.getJSONObject(i).getString("id"));
        placesSuggestionsBean.setReference(predsJsonArray.getJSONObject(i).getString("reference"));
        resultList.add(placesSuggestionsBean);
      }
    } catch (JSONException e) {
      Log.e("adsd", "Cannot process JSON results", e);
    }

    return resultList;
  }
コード例 #2
0
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

      final ViewHolder holder;
      PlacesSuggestionsBean bean = getItem(position);

      if (convertView == null) {

        holder = new ViewHolder();

        convertView =
            LayoutInflater.from(getContext()).inflate(R.layout.spinner_item, parent, false);

        holder.tvFullName = (TextView) convertView.findViewById(R.id.text);
        convertView.setTag(holder);
      } else {
        holder = (ViewHolder) convertView.getTag();
      }

      holder.tvFullName.setText(bean.getDescription());
      return convertView;
    }