示例#1
0
  public Result getDetails(String upc) {
    Result res = null;
    try {
      res = new HttpTask().execute(upc).get();

      Toast.makeText(mc, res.brand, Toast.LENGTH_LONG).show();
    } catch (Exception e) {
      MainActivity.log_exception(mc, e, "HttpTask");
    }
    return res;
  }
示例#2
0
    protected Result doInBackground(String... upcs) {
      Result res = new Result();
      try {
        String query = PREFIX + upcs[0];

        HttpClient httpclient = new DefaultHttpClient();

        HttpGet request = new HttpGet();
        URI website = new URI(query);
        request.setURI(website);
        HttpResponse response = httpclient.execute(request);

        BufferedReader in =
            new BufferedReader(new InputStreamReader(response.getEntity().getContent()));

        // dump header
        String line = in.readLine();
        Pattern ph = Pattern.compile("/head");
        Matcher mh = ph.matcher(line);
        while (!mh.find()) mh = ph.matcher(in.readLine());

        Cursor ts = mdb.getTypes();
        Cursor bs = mdb.getAllBrands();
        int[] bcount = new int[bs.getCount()];

        int[] tcount = new int[ts.getCount()];
        int tpos = 0;
        int bpos = 0;
        line = in.readLine();
        String allRes = "";
        String brand = "";
        boolean brandFound = false;
        int bestCount = 0;
        while (line != null) {
          allRes += line;
          tpos = 0;
          ts.moveToFirst();
          do {
            Pattern p = Pattern.compile(ts.getString(1));
            Matcher m = p.matcher(line);
            int findCount = 0;
            while (m.find()) findCount += 1;
            tcount[tpos] += findCount;
            tpos += 1;
          } while (ts.moveToNext());

          if (!brandFound) {
            bpos = 0;
            bs.moveToFirst();
            do {
              String testBrand = bs.getString(1);
              String s = testBrand.split("'")[0]; // simple throw away of ' not found in html
              if (!reserved(s)) {
                Pattern p1 = Pattern.compile(s);
                Matcher m1 = p1.matcher(line);
                int brandCount = 0;
                while (m1.find()) brandCount += 1;
                bcount[bpos] += brandCount;
                if (bcount[bpos] > 10) // good enough
                {
                  brand = bs.getString(1);
                  brandFound = true;
                }

                if (bcount[bpos] > bestCount) {
                  bestCount = bcount[bpos];
                  brand = bs.getString(1);
                }
                bpos += 1;
              }
            } while (!brandFound && bs.moveToNext());
          }

          line = in.readLine();
        }

        int c = 0;
        ts.moveToFirst();
        for (int x : tcount) {
          if (x > c) res.type = ts.getString(1);
          ts.moveToNext();
        }

        res.brand = brand;

      } catch (Exception e) {
        MainActivity.log_exception(mc, e, "HttpTask");
      }

      return res;
    }