2016-05-24 13 views
-5

コードを解決できませんがokhttp HttpUrlアンドロイドSDK 23シンボル私は問題を抱えてい

public class FaviconFetcher { 
    private static FaviconFetcher INSTANCE = new FaviconFetcher(); 



     private OkHttpClient client = new OkHttpClient(); 

    private final String[] htmlIconCssQueries = { 
     "meta[property=\"og:image\"]", 
     "meta[name=\"msapplication-TileImage\"]", 
     "link[rel=\"icon\"]", 
     "link[rel=\"shortcut icon\"]", 
     "link[rel=\"apple-touch-icon\"]", 
     "link[rel=\"apple-touch-icon-precomposed\"]", 
     "img[alt=\"Logo\"]", 
     "img[alt=\"logo\"]" 
    }; 

    private final String[] hardcodedIconPaths = { 
     "/favicon.ico", 
     "/apple-touch-icon.png", 
     "/apple-touch-icon-precomposed.png", 
    }; 

    private FaviconFetcher() {} 

    public static FaviconFetcher getInstance() { 
     return INSTANCE; 
    } 

    public String getFaviconUrl (Document document) { 
     List <String> potentialIcons = getPotentialFaviconUrls(document); 
     return pickBestIconUrl(potentialIcons); 
    } 


    public List<String> getPotentialFaviconUrls (Document document) { 
     List<String> iconUrls = new ArrayList<String>(); 
     HttpUrl base = HttpUrl.parse(document.baseUri()); 

     for (String cssQuery : htmlIconCssQueries) { 
      for (Element e : document.select(cssQuery)) { 
       if (e.hasAttr("href")) { 
        iconUrls.add(e.attr("href")); 
       } 

       if (e.hasAttr("content")) { 
        iconUrls.add(e.attr("content")); 
       } 

       if (e.hasAttr("src")) { 
        iconUrls.add(e.attr("src")); 
       } 
      } 
     } 

     for (String path : hardcodedIconPaths) { 
      HttpUrl url = HttpUrl.parse("http://" + HttpUrl.parse(document.baseUri()).host() + path); 
      iconUrls.add(url.toString()); 
     } 

     for (ListIterator<String> i = iconUrls.listIterator(); i.hasNext();) { 
      HttpUrl httpUrl = base.resolve(i.next()); 
      if (httpUrl != null) { 
       i.set(httpUrl.toString()); 
      } else { 
       i.remove(); 
      } 
     } 

     return iconUrls; 

    } 


    public String pickBestIconUrl (List<String> urls) { 
     String bestIconUrl = null; 
     int currentBestWidth = 0; 

     for (String url : urls) { 
      BitmapFactory.Options options = getBitmapDimensFromUrl(url); 
      if (options != null && options.outHeight == options.outHeight) { 
       if ((bestIconUrl != null) && (currentBestWidth <= options.outWidth)) { 
        bestIconUrl = url; 
        currentBestWidth = options.outWidth;  
       } else if (bestIconUrl == null) { 
        bestIconUrl = url; 
        currentBestWidth = options.outWidth; 
       } 
      } 
     } 

     return bestIconUrl; 
    } 

    private BitmapFactory.Options getBitmapDimensFromUrl (String url) { 
     BitmapFactory.Options options = new BitmapFactory.Options(); 
     options.inJustDecodeBounds = true; 

     Request request = new Request.Builder() 
      .url(url) 
      .build(); 

     try { 
      Response response = client.newCall(request).execute(); 
      InputStream is = response.body().byteStream(); 

      BitmapFactory.decodeStream(is, null, options); 

      response.body().close(); 
      is.close(); 

      return options; 

     } catch (IllegalArgumentException | IOException e) { 
      e.printStackTrace(); 
      return null; 
     } 
    } 
} 

下回っている私はこれについていくつかの研究を行っているが、私はこの問題に定義された解決策を得るていません。私はandroid-sdkを変更したくありません。 助けていただければ幸いです。ありがとう。

+0

あなたの輸入... – Selvin

答えて

0

輸入パーフェクト

ちょうどあなたのプロジェクトあなたのbuild.gradle

android { 
    useLibrary 'org.apache.http.legacy' 
} 

その後クリーン再構築同期でこれを追加します。

どのバージョンをお使いですか?

  1. compile 'com.squareup.okhttp3:okhttp:3.2.0'または
  2. compile 'com.squareup.okhttp:okhttp:2.5.0'

読む

https://developer.android.com/about/versions/marshmallow/android-6.0-changes.html

+1

コンパイル「com.squareup.okhttp修正:okhttp:2.5。 0 ' – Webln

+0

私のdepen com.squareup.okhttp:okhttp:2.5.0をコンパイルします。私は、HttpUrlではなくHttpUrlConnectionのために動作するようにコードを再フォーマットする方法の手がかりを必要としています – Webln

+0

* useLibrary 'org.apache.http.legacy' *何のために?彼はApache HTTPクライアントを使用していません... * [変更点へのリンク]を読んでください*なぜですか? okhttpライブラリは影響を受けません – Selvin

関連する問題