diff --git a/app/src/main/java/com/readrops/app/utils/HtmlParser.java b/app/src/main/java/com/readrops/app/utils/HtmlParser.java index 51f2f2a9..f0414d0d 100644 --- a/app/src/main/java/com/readrops/app/utils/HtmlParser.java +++ b/app/src/main/java/com/readrops/app/utils/HtmlParser.java @@ -5,9 +5,9 @@ import android.util.Log; import androidx.annotation.NonNull; import androidx.annotation.Nullable; +import com.readrops.readropslibrary.utils.HttpManager; import com.readrops.readropslibrary.utils.LibUtils; -import org.jsoup.Connection; import org.jsoup.Jsoup; import org.jsoup.nodes.Document; import org.jsoup.nodes.Element; @@ -18,6 +18,9 @@ import java.util.ArrayList; import java.util.List; import java.util.regex.Pattern; +import okhttp3.Request; +import okhttp3.Response; + public final class HtmlParser { private static final String TAG = HtmlParser.class.getSimpleName(); @@ -81,7 +84,7 @@ public final class HtmlParser { } @Nullable - public static String getFaviconLink(@NonNull String url) throws IOException { + public static String getFaviconLink(@NonNull String url) { String favUrl = null; String head = getHTMLHeadFromUrl(url); @@ -102,20 +105,28 @@ public final class HtmlParser { } @Nullable - private static String getHTMLHeadFromUrl(@NonNull String url) throws IOException { + private static String getHTMLHeadFromUrl(@NonNull String url) { long start = System.currentTimeMillis(); - Connection.Response response = Jsoup.connect(url).execute(); - if (response.contentType().contains(LibUtils.HTML_CONTENT_TYPE)) { - String body = response.body(); - String head = body.substring(body.indexOf("")); + try { + Response response = HttpManager.getInstance().getOkHttpClient() + .newCall(new Request.Builder().url(url).build()).execute(); - long end = System.currentTimeMillis(); - Log.d(TAG, "parsing time : " + (end - start)); + if (response.header("Content-Type").contains(LibUtils.HTML_CONTENT_TYPE)) { + String body = response.body().string(); + String head = body.substring(body.indexOf("")); - return head; - } else + long end = System.currentTimeMillis(); + Log.d(TAG, "parsing time : " + (end - start)); + + return head; + } else { + return null; + } + } catch (Exception e) { return null; + } + } public static String getDescImageLink(String description, String url) { diff --git a/app/src/test/java/com/readrops/app/HtmlParserTest.java b/app/src/test/java/com/readrops/app/HtmlParserTest.java index bf071b5a..bf8f6e47 100644 --- a/app/src/test/java/com/readrops/app/HtmlParserTest.java +++ b/app/src/test/java/com/readrops/app/HtmlParserTest.java @@ -6,7 +6,6 @@ import com.readrops.app.utils.ParsingResult; import org.junit.Assert; import org.junit.Test; -import java.io.IOException; import java.util.ArrayList; import java.util.List; @@ -28,7 +27,7 @@ public class HtmlParserTest { } @Test - public void getFaviconLinkTest() throws IOException { + public void getFaviconLinkTest() { String url = "https://github.com/readrops/Readrops"; assertEquals("https://github.com/fluidicon.png", HtmlParser.getFaviconLink(url));