2013-06-20 36 views
0

我的問題是status != 200,當我在不同的時間運行此腳本(3)時,它將打印出if{}(1)次,else{}(1)時間的值以及另一個是catch{}檢查HTTP 500錯誤的缺陷HttpUrlConnection Java

我只是試圖打印出在if(status != 200 || showTitleAttr == null || showTitleAttr == "") system.out.println();消息如果HTTP錯誤不是HTTP 200錯誤代碼。 這個邏輯是有道理的,但它仍然不是一直工作,並落入catch{}塊。我開始認爲這是變量status的問題。

謝謝。

小更新 即使我嘗試:conn.getResponseCode() != HttpURLConnection.HTTP_OK它似乎沒有改變其阻止它落入..(1)時間分別用於if{}else{}catch{}

我收到的錯誤當它擊中catch{}塊是:

java.io.IOException: Server returned HTTP response code: 500 for URL 

問:有沒有更好的方式來檢查HTTP錯誤代碼是什麼比一個HTTP 200錯誤?

代碼:

public static void main(String args[]) { 
     HttpException HttpExc = new HttpException(); 
     try { 
       // setup Show Title Parser 
       DOMParser parser = new DOMParser(); 

       // Set the Url to Parse and assign to object 
       String UrlToParse = "urlishere.com"; 
        URL obj = new URL(UrlToParse); 

       // Try opening a http connection the the url above 
       HttpURLConnection conn = (HttpURLConnection) obj.openConnection(); 
       int status = conn.getResponseCode();  

       // Setup parser 
       parser.parse(UrlToParse); 
       Document doc = parser.getDocument(); 

       // Get the document's root XML node 
       NodeList PGRoot = doc.getChildNodes(); 

       // Navigate down the hierarchy to get to the program node 
       Node comp = getNode("ProgramGuideWCSResponse", PGRoot); 
       Node PG = getNode("programGuide", comp.getChildNodes()); 
       Node Programs = getNode("programs", PG.getChildNodes()); 
       Node IndivdualProgram = getNode("program", Programs.getChildNodes()); 
       NodeList nodes = IndivdualProgram.getChildNodes(); 
       //String execType = getNodeAttr("programTitle", exec); 

       // Assign the Show Title to the NodeValue from traversing 
       String showTitleAttr = getNodeValue("programTitle", nodes); 

       // check to if the fetched Show Title isn't: 1) Returned bad error code 2) is null or 3) is empty 
        // if it is... display the default value for title and exit. 
        // otherwise, print the Program Title. 

       if(status != 200 || showTitleAttr == null || showTitleAttr == ""){ 
        System.out.println("You’re watching XX Plus"); 
        System.exit(0); 
       } 

       else{ 
        System.out.println("Program title is: " + showTitleAttr); 
       } 

     } 
      catch(HttpException e){ 
       e.getStackTrace(); 
      } 

      catch (Exception e) { 
       //e.printStackTrace(); 
       System.out.println("You’re watching XX Plus"); 
       System.exit(0); 
      } 
    } 
} 
+0

嘗試改變這種'showTitleAttr == 「」'本'showTitleAttr.equals ( 「」)'。不知道這是否導致您的問題,雖然 – cmbaxter

+0

感謝您的答覆,這並不能解決問題,雖然與上述檢測Http錯誤。 – CodeTalk

+0

什麼是異常堆棧跟蹤當它碰到catch塊? – cmbaxter

回答

2

你需要開始解析之前檢查狀態...

URL url = new URL(UrlToParse); 
    HttpURLConnection con = (HttpURLConnection)url.openConnection(); 
    int status = con.getResponseCode(); 
    if (status == 200){ 
     parser.parse(UrlToParse); 
     .... 
    } else { 
     // status is not 200 
    }