omeliiii
omeliiii

Reputation: 31

How to download HTML source of an Instagram page

in my app i need to download HTML source of an Instagram profile and parse it to get some information (media and followed-by count). That's my code (it works for all sites i tested, except for Instagram):

try {
            InputStream in;
            URL url = new URL(urlString);

            URLConnection conn = url.openConnection();
            if(!(conn instanceof HttpURLConnection))
                throw new NoConnectionException("not instanceof http");

            HttpURLConnection httpConn = (HttpURLConnection) conn;
            httpConn.setAllowUserInteraction(false);
            httpConn.setInstanceFollowRedirects(true);
            httpConn.setRequestMethod("GET");

            in = httpConn.getInputStream();

            BufferedReader br = new BufferedReader(new InputStreamReader(in));
            String line;
            String source = "";
            while((line = br.readLine()) != null)
                source += line;
            br.close();
} catch(Exception e) {}

When i debug it with LogCat, String source is empty.

Upvotes: 1

Views: 2738

Answers (1)

Zeeshan Shabbir
Zeeshan Shabbir

Reputation: 7114

Use Jsoup for HTML parsing. It is quite easy and handy. Take start from this answer and follow documents link

Upvotes: 2

Related Questions