0

我的问题是status != 200,当我运行此脚本 (3) 不同时间时,它将打印出if{}(1) 次、else{}(1) 次和catch{}另一个的值。

我只是想在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 200 错误更好的方法来检查 HTTP 错误代码?

编码:

 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);
            }
     }
}
4

1 回答 1

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
    }
于 2013-06-20T15:40:21.330 回答