Example #1
0
 // Uses the mobile-specific version of Product Search, which is formatted for small screens.
 final void openProductSearch(String upc) {
   Uri uri =
       Uri.parse(
           "http://www.google."
               + LocaleManager.getProductSearchCountryTLD(activity)
               + "/m/products?q="
               + upc
               + "&source=zxing");
   launchIntent(new Intent(Intent.ACTION_VIEW, uri));
 }
final class ProductResultInfoRetriever extends SupplementalInfoRetriever {

  private static final String BASE_PRODUCT_URI =
      "http://www.google."
          + LocaleManager.getProductSearchCountryTLD()
          + "/m/products?ie=utf8&oe=utf8&scoring=p&source=zxing&q=";
  private static final Pattern PRODUCT_NAME_PRICE_PATTERN =
      Pattern.compile("owb63p\">([^<]+).+zdi3pb\">([^<]+)");

  private final String productID;
  private final String source;

  ProductResultInfoRetriever(
      TextView textView,
      String productID,
      Handler handler,
      HistoryManager historyManager,
      Context context) {
    super(textView, handler, historyManager);
    this.productID = productID;
    this.source = context.getString(R.string.msg_google_product);
  }

  @Override
  void retrieveSupplementalInfo() throws IOException, InterruptedException {

    String encodedProductID = URLEncoder.encode(productID, "UTF-8");
    String uri = BASE_PRODUCT_URI + encodedProductID;
    String content = HttpHelper.downloadViaHttp(uri, HttpHelper.ContentType.HTML);

    Matcher matcher = PRODUCT_NAME_PRICE_PATTERN.matcher(content);
    if (matcher.find()) {
      append(
          productID,
          source,
          new String[] {unescapeHTML(matcher.group(1)), unescapeHTML(matcher.group(2))},
          uri);
    }
  }

  private static String unescapeHTML(String raw) {
    return Html.fromHtml(raw).toString();
  }
}
  @Override
  void retrieveSupplementalInfo() throws IOException {

    String encodedProductID = URLEncoder.encode(productID, "UTF-8");
    String uri =
        "http://www.google."
            + LocaleManager.getProductSearchCountryTLD(context)
            + "/m/products?ie=utf8&oe=utf8&scoring=p&source=zxing&q="
            + encodedProductID;
    CharSequence content = HttpHelper.downloadViaHttp(uri, HttpHelper.ContentType.HTML);

    for (Pattern p : PRODUCT_NAME_PRICE_PATTERNS) {
      Matcher matcher = p.matcher(content);
      if (matcher.find()) {
        append(
            productID,
            source,
            new String[] {unescapeHTML(matcher.group(1)), unescapeHTML(matcher.group(2))},
            uri);
        break;
      }
    }
  }