java
  • token
  • tokenize
  • string-parsing
  • stringtokenizer
  • 2016-08-15 21 views 1 likes 
    1

    我正在嘗試使用Volley庫和Dropbox API編寫自動更新。我如何比較字符串響應的版本與已安裝的應用程序的值

    if (response != null) { 
          boolean resp = response.contains("<div class='post-body entry-content' id='post-body-6791062644900393367' itemprop='description articleBody'>\n" + 
            "1.1.8\n" + 
            "<div style='clear: both;'></div>"); 
    

    使用這個代碼,我可以說,如果響應小於1.1.8然後下載更新:我已經這樣它讀取的博客頁面,發現這個塊的代碼設置。我試圖這樣做使用這種方法:

    if (!resp){My Intent code is here } 
    

    但是這個代碼在下載即使版本是最新的更新。

    我已經添加了下面的代碼來捕獲版本號。但我不能縫我的頭如何與我的版本在我的gradle比較,並說如果不到請更新

    Pattern p = Pattern.compile("<div class='post-body entry-content' id='post-body-6791062644900393367' itemprop='description articleBody'>\n" + 
          "([0-9.]+)\n" + 
          "<div style='clear: both;'></div>"); 
    
    Matcher m = p.matcher(response); 
    if (m.find()) { 
    String version = m.group(); 
    // more code here 
    

    }

    難道我做錯了什麼或做我需要添加更多的代碼?我讀過我不能少用它。這樣我也試過用「< =」比較。如果你認爲它可能是我的代碼的其他部分導致問題讓我知道,我可以張貼更多的給你看。

    +0

    我編輯了我的答案,包括完整的工作代碼,您可以簡單地複製和粘貼以準備繼續。檢查出來,兄弟!我希望這將是你所尋找的答案 –

    回答

    1

    Tokeinze以字符串usin \n作爲分隔符,隔離中間標記(通過索引 - 第二個aka 1 - 或通過用簡單的正則表達式解析標記)。 使用this類。

    - 編輯:以下是完整example--

    public static void main(String[] args) 
    { 
        String response = "blahblah<div class='post-body entry-content' id='post-body-6791062644900393367' itemprop='description articleBody'>\n" 
          + "1.1.8"//try "1.1.7", "1.1.9", etc to see that this does work 
          + "\n<div style='clear: both;'></div>yaddayadda";//this is just to setup the test: you're obviously getting response from somewhere 
        String myVersion = "1.1.8"; 
        System.out.println("My version: "+myVersion); 
        Integer versionOK = isVersionOK(response, myVersion); 
        String s; 
        if (versionOK != null) 
        { 
         switch (versionOK) 
         { 
          case 0: 
           s = "equal"; 
           break; 
          case -1: 
           s = "greater"; 
           break; 
          default: 
           s = "lesser"; 
         } 
         System.out.println("Version is " + s); 
        } 
        else 
         System.out.println("Couldn't detect version!"); 
    } 
    
    /** 
    * 
    * @param response 
    * @return 0 if equal, -1 if greater, 1 if lesser, null if not found or 
    * anything goes unexpectedly- 
    */ 
    static Integer isVersionOK(String response, String myVersion) 
    { 
        String whatToSearchForBefore = "<div class='post-body entry-content' id='post-body-6791062644900393367' itemprop='description articleBody'>\n"; 
        String whatToSearchForAfter = "\n<div style='clear: both;'></div>"; 
        if (response != null) 
        { 
         boolean resp = response.contains(whatToSearchForBefore)&&response.contains(whatToSearchForAfter); 
         System.out.println("response does " + (resp ? "" : "not ") + "contain the version number"); 
         if (resp) 
         { 
          String whatWeFound = response.substring(response.indexOf(whatToSearchForBefore), response.indexOf(whatToSearchForAfter) + whatToSearchForAfter.length()); 
          System.out.println("----\n"+whatWeFound+"\n----"); 
          StringTokenizer st = new StringTokenizer(whatWeFound, "\n"); 
          st.nextToken(); 
          String version = st.nextToken(); 
          System.out.println("Version: " + version); 
          return myVersion.compareToIgnoreCase(version); 
         } 
        } 
        return null; 
    } 
    
    +0

    謝謝你,但這種方式難以做到。 –

    +1

    如果你堅持下去,我會在[意大利]下午後期(即在一對夫婦後)爲你寫實際的代碼 –

    +0

    謝謝@Unai Vavi –

    2

    捕捉使用正則表達式的版本號:

    Pattern p = Pattern.compile("<div class='post-body entry-content' id='post-body-6791062644900393367' itemprop='description articleBody'>\n" + 
           "([0-9.]+)\n" + 
           "<div style='clear: both;'></div>"); 
    Matcher m = p.matcher(response); 
    if (m.find()) { 
        String version = m.group(); 
        // more code here 
    } 
    

    其中([0-9.]+)抓住了數字的組合(0-9)和期間(.)。

    現在你只需要弄清楚如何檢查version是否小於1.1.8。您不能使用字符串比較,因爲"1.1.10"比較小於"1.1.8",即使它是更高的「版本」。
    請參閱How do you compare two version Strings in Java?

    +0

    brill謝謝你現在就來試試這個@Andreas –

    +0

    將比較的字符串放在這裏----> //更多的代碼在這裏 –

    +0

    @MarkHarrop是的,除了使用*版本*比較,不是一個普通的字符串比較。 – Andreas

    相關問題