2014-10-18 158 views
0

所以,我试图做一个函数,它将访问一个页面,获取一个项目的ID和TYPE,然后访问一个链接并插入ID和TYPE以获得ITEM NAME 。然后它会将所有这些添加到ArrayList中。新列表(id,type,name);Java正则表达式,带变量的多表达式

public static ArrayList<Kad> getDetails(final String strHTML, final String strHTML2) { 
    final ArrayList<Kad> kads = new ArrayList<Kad>(); 
    try { 
     final Pattern regex = Pattern 
       .compile(
         "id=(\\d+)\\&tab=([a-zA-Z]+)", 
         Pattern.CASE_INSENSITIVE | Pattern.UNICODE_CASE); 
     final Pattern regex2 = Pattern 
       .compile(
         "font-weight:bold; font-size:11px;\">([\\w\\s]+)</div>", 
         Pattern.CASE_INSENSITIVE | Pattern.UNICODE_CASE); 

     final Matcher regexMatcher = regex.matcher(strHTML); 
     final Matcher regexMatcher2 = regex2.matcher(strHTML2); 

     String ids = ""; 
     while (regexMatcher.find() && regexMatcher2.find()) { 
      final int id = Integer.parseInt(regexMatcher.group(1)); 
      final String tab = regexMatcher.group(2); 
      final String name = regexMatcher2.group(1); 


      if (!ids.contains(id + "|") && !ignoreList.contains(id)) { 
       kads.add(new Kad(id,tab,name)); 
       ids += id + "|"; 
      } 
     } 
     return kads; 
    } catch (final Exception ex) { 
     ex.printStackTrace(); 
    } 
    return new ArrayList<Kad>(); 
} 

此代码用于获取项目的ID,类型和名称。但是,由于此函数获取ID和名称,我无法获取ID列表。将其分解为两个函数(其中一个获得ID和TYPE),然后另一个获取名称的最佳方法是什么?

此方法使用名为:

data = wrapper.post("LINK1", "tab=" + "food" + "&page=" + "1", ""); 
     data2 = wrapper.post("LINK2", "tab=" + "CURRENT ITEM TYPE IN LIST GOES HERE" + "&id=" + "CURRENT ITEM ID IN LIST GOES HERE", ""); 
     final ArrayList<Kad> kadList = Kad.getDetails(data, data2); 

我试图打破它分成两个不同的方法,但无法弄清楚如何在方法2中找到的名称添加到ID的已创建的列表和TYPE在方法1中。

编辑:好的,所以在下面的建议解决方案后,我现在可以在两个独立的方法中获取id和名称。现在的问题是方法2(获取名称方法)是给每个单一的ID相同的名称(搜索的第一个名称)。我如何解决这个问题?

public static ArrayList<Kad> findItemName2(final int id, final String tab, final String strHTML) { 
    final ArrayList<Kad> names = new ArrayList<Kad>(); 
    try { 
     final Pattern regex = Pattern 
       .compile(
         "font-weight:bold; font-size:11px;\">([\\w\\s]+)</div>", 
         Pattern.CASE_INSENSITIVE | Pattern.UNICODE_CASE); 
     final Matcher regexMatcher = regex.matcher(strHTML); 
     String ids = ""; 
     while (regexMatcher.find()) { 

      final String name2 = regexMatcher.group(1); 

      if (!ids.contains(name2 + "|")) { 
       names.add(new Kad(id,tab, name2)); 
       name = name2; 
       ids += name2 + "|"; 
      } 
     } 
     return names; 
    } catch (final Exception ex) { 
     ex.printStackTrace(); 
    } 
    return new ArrayList<Kad>(); 
} 

回答

1

你可以试试下面

  • 创建2种不同的方法 - method1method2 - 一个意愿,抓住idtype和另一意愿name
  • 将存在于现有getDetails方法中的代码分离并将其移入这些方法中。
  • id,typename创建类别为Kad类的获得者和设置者。
  • 创建Kad类的实例,并调用它method1method2和调用这些method1 & method2内各自制定者,为这些字段中的值。

一次,method1method2执行 - 你有idtypename值设置成可使用的getter通过以下三种不同的领域。

+0

所以,你让我一直到第4步。“在这些方法1和方法2里面”是我不理解的一点。 – user1508174 2014-10-18 06:56:20

+0

此外,我很困惑我将如何拉出ArrayList中的特定“Kad”来追加名称?相当多的方法1将获得所有的ID和类型,并将它们投入ArrayList,它将输出如下所示的内容:{9:food,10:food,11:food},我需要运行method2来追加(for例如ID 9)它在ArrayList中的条目名称,因此它读取:{9:food:NAME} – user1508174 2014-10-18 07:13:53

+0

尝试在方法一和方法二中的两个列表中组装数据。一个'id'和'type'的列表,另外一个'name'。一旦你有这些列表,然后从这些列表中读取数据,以创建'Kads'对象并准备'kads' arraylist。 – Tirath 2014-10-18 07:52:41