Package jsoup

Source Code of jsoup.ConnectionManager

package jsoup;

import java.io.IOException;

import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import common.Utils;

public class ConnectionManager {
 
  private static ConnectionManager instance = null;
 
  public static ConnectionManager getInstance(){
    if (instance == null) {
      instance = new ConnectionManager();
    }
    return instance;
  }
 
  public Connection getConnection(String url){
    return Jsoup.connect(url).timeout(60000);
  }
 
  public Document getDocument(String url) throws Exception{
    Connection conn = getConnection(url);
    if (conn == null) {
      throw new Exception("url connect error..."+url);
    }
    Document doc = null;
    int i = 1;
    while (doc == null) {
      try {
        doc = conn.get();
      } catch (IOException e) {
        i++;
        if (i>20) {
          Utils.print("***********("+e.getMessage()+")Try to read ("+i+") times.");
          return null;
        }
        conn = getConnection(url);
      }
    }
    return doc;
  }
}
TOP

Related Classes of jsoup.ConnectionManager

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.