BusinessSearch.java :  » Geo » wakemewhere » com » google » android » apps » wakemewhere » mapsearch » Android Open Source

Android Open Source » Geo » wakemewhere 
wakemewhere » com » google » android » apps » wakemewhere » mapsearch » BusinessSearch.java

package com.google.android.apps.wakemewhere.mapsearch;

import java.io.IOException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Logger;
import java.util.regex.Pattern;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;

import android.util.Pair;

public class BusinessSearch {
  
  private static final String STRIP_FROM_JSON_RESPONSE = "while(1);";
  private static final String TAG_BUSINESS_NAME = "gw:businessname";
  private static final String TAG_BUSINESS_ADDRESS = "gw:address";
  //private static final int SEARCH_MAX_RESULTS = 10;
  private static final Logger logger = Logger.getLogger(BusinessSearch.class.getName());

  public interface Callback {
    public void onBusinessSearchResult(Business[] results, Object tag);
  }
  
  public void asyncSearch(final String query, final Callback callback, final Object tag) {
    Runnable searchRunnable = new Runnable() {
      public void run() {
        Business[] result = search(query);
        callback.onBusinessSearchResult(result, tag);
      }};
    new Thread(searchRunnable).start();
  }

  private Business[] search(String query) {
    HttpClient httpClient = new DefaultHttpClient();
    HttpGet httpGet = new HttpGet("http://maps.google.com/maps?output=json&q=" + URLEncoder.encode(query));
    
    try {
      HttpResponse response = httpClient.execute(httpGet);
      HttpEntity entity = response.getEntity();
      if (entity == null) {
        logger.warning("No HTTP entity");
        return new Business[0];
      }
      String responseJson = EntityUtils.toString(entity);
      if (responseJson.startsWith(STRIP_FROM_JSON_RESPONSE)) {
        responseJson = responseJson.substring(STRIP_FROM_JSON_RESPONSE.length());
      }
      return extractBusinessDetails(responseJson).toArray(new Business[0]);
    } catch (IOException e) {
      logger.warning(e.getMessage());
    }
    return new Business[0];
  }
  
  private static List<Business> extractBusinessDetails(String json) {
    List<Business> out = new ArrayList<Business>();
    json = json.replace("\\", "");
    int position = 0;
    while (true) {
      Pair<Integer, String> name = getTagValue(json, TAG_BUSINESS_NAME, position);
      if (name == null) {
        return out;
      }
      Pair<Integer, String> address = getTagValue(json, TAG_BUSINESS_ADDRESS, name.first);
      if (address == null) {
        return out;
      }
      out.add(new Business(name.second, address.second));
      position = address.first;
    }
  }
  
  private static Pair<Integer, String> getTagValue(String json, String tag, int start) {
    int position = json.indexOf(tag, start) + tag.length();
    if (position == -1) {
      return null;
    }
    if (position + 2 >= json.length() || json.charAt(position) != '=' || json.charAt(position + 1) != '"') {
      return null;
    }
    int endQuote = json.indexOf("\"", position + 2);
    if (endQuote == -1) {
      return null;
    }
    String value = json.substring(position + 2, endQuote);
    value = unescapeString(value);
    return new Pair<Integer, String>(endQuote, value);
  }
  
  private static String unescapeString(String in) {
    Pattern foo = Pattern.compile("x26#([0-9]+);");
    String[] a = foo.split(in);
    for (String f : a) {
      Logger.global.warning("got:[" + f + "]");
    }
    return in.replace("x26#39;", "'");
    //return in;
  }
}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.