2012-04-12 13 views
0

サーバーが停止すると、このスニペットはサーバーを繰り返し呼び出しています。要求が失敗した場合、繰り返し呼び出しを停止するにはどうすればよいですか?たぶんcatch/errorブロックですか?しかし、私はどのようにそれを実装するか分からない。ありがとう!サーバーが停止したら、このスニペットはサーバーを繰り返し呼び出すのを止めますか?

HttpURLConnection httpConn = null; 
InputStream is = null; 
OutputStream os = null; 
String xmlResponse = null; 

try { 
    //Open Connection 

     String CollectionId="123"; 
     String GetHTML="1"; 

     String urlString = "http://tester.com/webservices/ContentWS.asmx/GetCollection?CollectionId="+CollectionId+"&GetHTML="+GetHTML; 

     System.out.println(urlString); 

     //String encodedUrl = URLEncoder.encode(urlString,"UTF-8"); 

    URL url = new URL(urlString); 
    httpConn = (HttpURLConnection)url.openConnection(); 

    //Setup Request 
    httpConn.setRequestMethod("GET"); 
    httpConn.setDoOutput(true); 
    httpConn.setReadTimeout(10000); 
    httpConn.connect(); 

    xmlResponse = convertStreamToString(httpConn.getInputStream()); 
    } 
    catch (IOException e) { 
     e.printStackTrace(); 
    } 

String htmlContent = ""; 
    try { 
     DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); 
     DocumentBuilder db = dbf.newDocumentBuilder(); 
     InputSource input = new InputSource(); 
     input.setCharacterStream(new StringReader(xmlResponse)); 

     Document doc = db.parse(input); 
     NodeList nodes = doc.getElementsByTagName("Item"); 

     // iterate the Items 
       int i = 0; 
       int j = 0; 
       Date now = new Date(); 
     for (i = 0; i < 2; i++) { 
        //Grab HTML to append to htmlContent 
        NodeList teaser = doc.getElementsByTagName("Html"); 
        Element line = (Element) teaser.item(i); 
        htmlContent += getCharacterDataFromElement(line); 
     } 
    } 
    catch (Exception e) { 
     e.printStackTrace(); 
    } 
htmlContent = ... 
+0

コードの一部が欠落しています。このコードは無限に実行されません –

+0

この質問はJSPとは関係ありません。タグを削除しました –

答えて

0

このコードはスタックトレースを出力しますが、エラーをスローしないため、呼び出しコードは問題を認識しません。

catch()ブロックでエラーを再現するのに役立ちますか?

e.printStackTrace(); 
throw e; 
0

すべてが機能しているようです。あなたの接続は開いていて動作していますが、細部は忘れてしまいました。作業が完了したら、HttpURLConnection.disconnect()を使用して、接続から情報を要求し終わったことをプログラムに伝える必要があります。

私はこのようなJavaの観点からネットワークを見ています: 接続の設定 - >接続の要求の送信 - >接続 - >データの取得 - >データで何かを行う - >接続を閉じる

また、HttpURLConnection.disconnect()までの短いメモは、例外がスローされても切断されることを確認してください。あなたが望むなら、最後に} finally {ステートメントを追加することができます。

関連する問題