2015-12-21 32 views
1

我需要下載Instagram資料的HTML源代碼並解析它以獲取一些信息(媒體和後續計數)。 這是我的代碼(它適用於我測試的所有站點,除了Instagram的):如何在我的應用程序中下載Instagram頁面的HTML源代碼

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) {} 

當我的logcat調試它,絃樂來源是空的。

+0

你可以張貼鏈接 – Mohit

+1

首先「把httpclinet拿走並使用改造或凌空」 –

+0

@sadeghsaati你在哪裏看到'HttpClient'這裏? – Henry

回答

2

使用Jsoup進行HTML解析。這是非常容易和方便的。 就拿從這個答案開始,並按照文件link

+0

好的,但爲什麼我的代碼可以與我測試的所有網站一起使用,並且不適用於Instagram? – genialFactory

+0

你的代碼看起來也不錯。但是Jsoup非常簡單。只需一行代碼,你就擁有了所有的HTML。試試看。我已經完成了它的簡單 –

+0

它爲我工作!非常感謝你!! – genialFactory

相關問題