2

On facebook, if we share some url, facebook automatically fetch an image and some initial text from the url. Any idea how to achieve this inside android app(getting image and some text from the url automatically).

Any help would be highly appreciated. Thanks !

Vipul J
  • 6,641
  • 9
  • 46
  • 61

2 Answers2

0

You can make a simple HttpRequest for the url and then filter the HTML code to get the required content.

Sample code for HttpRequest -

HttpClient httpclient = new DefaultHttpClient(); // Create HTTP Client
HttpGet httpget = new HttpGet("http://example.com"); // Set the action you want to do
HttpResponse response = httpclient.execute(httpget);
HttpEntity entity = response.getEntity(); 
InputStream is = entity.getContent();
String line = null;
while ((line = reader.readLine()) != null) 
     { sb.append(line + "\n"); }

String resString = sb.toString(); 
is.close();

Facebook,Google+ use meta tags for the short info that is shared with the image. For the image look for the first <img> tag and then strip of the src="" from it to get the url. Then perform a AsyncDownload from the given image url to produce a drawable -

try
    {
       InputStream is = (InputStream) new URL(src).getContent();
       Drawable d = Drawable.createFromStream(is, "src name");
        return d;
    }catch (Exception e) {  
        return null;
    }
Community
  • 1
  • 1
shreks7
  • 422
  • 4
  • 10
0

You can try to use Aquery (similar to JQuery but for mobile), this library will give you lazy loading technique

                                    OR

You can make a webservice that can return you data in the form of JSON and then you have to just parse JSON and get your image as well as text !!!

Hope this helps!!!!!

ChArAnJiT
  • 238
  • 1
  • 7
  • 23
  • JSON parsing is what I am doing already. But for that I have to make individual network calls for downloading image and text. Also I dont want complete text, I just need some initial text, like facebook, google+ does when you share some url. – Vipul J Oct 19 '12 at 05:16