Android: fetching a webpage by URL programmatically

I want to get a thumbnail of a webpage at a URL programmatically.

We did some searching and came up with several solutions:

  • using Swing (JEditorPane) - but since I understand and correct, if I'm wrong, it's impossible to use swing with an Android application.
  • using a third-party site with api services, for example, thumbalizr.com - they prefer not to use this, because it is a watermark of the sketch if I don’t pay (my application is free).
  • I don’t know if this is possible, but perhaps the Android browser function is used for this. maybe go to the url in a hidden way while activity only shows a progress bar? :)

Can someone suggest something that works? maybe something more dear?

Rate any direction!

+3
source share
2 answers

Good question ... this is probably not good enough, but you can get the site icon from the website history via android.webkit.WebHistoryItem getFavicon () call: http://developer.android.com/reference/android/webkit/ WebHistoryItem.html AMB

0
source

You can try loading the icons with HttpClient. Fe :.

import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.StatusLine;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
..........

ArrayList<String> list; //list of target urls

HttpClient httpclient = new DefaultHttpClient();
final int size = list.size();
for (int i = 0; i < size; ++i) {
    try {
       String pure_url = extractPureUrl(url);
       HttpResponse response = httpclient.execute(new HttpGet(pure_url + "/favicon.ico"));

       if (response.getEntity() != null) { 
           StatusLine statusLine = response.getStatusLine();
           if (statusLine.getStatusCode() == HttpStatus.SC_OK) {
              ByteArrayOutputStream out = new ByteArrayOutputStream();
              response.getEntity().writeTo(out);
              out.close();

              byte[] image_bytes = out.toByteArray();
              bmp = BitmapFactory.decodeStream(new ByteArrayInputStream(image_bytes));
              if (bmp != null) {
                  //do smth with received bitmap
              }         
           } else {
              response.getEntity().consumeContent();
           }
       }
  } catch (ClientProtocolException e) {
    //log error
  } catch (IOException e) {
    //log error
  }
}

private static final String EXTENDED_HTTP_PREFIX = "http://www.";
private static final String HTTP_PREFIX = "http://";
/** Converts http://abc.com/xxx and http://www.abc.com/xxx to http://abc.com */
private static String extractPureUrl(String srcUrl) {
    String sw = srcUrl.toLowerCase();
    String s = sw.startsWith(EXTENDED_HTTP_PREFIX) 
   ? sw.substring(EXTENDED_HTTP_PREFIX.length()) 
   : sw.startsWith(BookmarkInfo.HTTP_PREFIX) 
      ? sw.substring(BookmarkInfo.HTTP_PREFIX.length()) 
      : sw;
    int n = s.indexOf('/');
    if (n == -1) return srcUrl;
    return HTTP_PREFIX + s.substring(0, n);
}

There are potential problems here - iico format is not officially supported by Android . In practice, BitmapFactory decodes the ico format on most devices without any problems. Anyway, I'm not sure if it will decode ico on abs of all devices.

0
source

Source: https://habr.com/ru/post/1787954/


All Articles