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 !

2 Answers 2

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;
    }
Sign up to request clarification or add additional context in comments.

Comments

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!!!!!

1 Comment

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.