Cannot fetch content from url with AsyncTask in android?

43 Views Asked by At

Trying to download content from https://www.forbesindia.com/lists/2019-celebrity-100/1819/all

    public class DownloadTask extends AsyncTask<String, Void, String> {

    @Override
    protected String doInBackground(String... strURLs) {
        String strPageHTML = "";
        URL objURL;
        HttpURLConnection objHttpURLConn = null;
        try {
            objURL = new URL(strURLs[0]);
            objHttpURLConn = (HttpURLConnection)objURL.openConnection();
            InputStream objInputStream = objHttpURLConn.getInputStream();
            InputStreamReader objInputStreamReader = new InputStreamReader(objInputStream);
            int intData = objInputStreamReader.read();
            while (intData!=-1){
                char chrCurrent = (char) intData;
                strPageHTML += chrCurrent;
                intData = objInputStreamReader.read();
            }
            //Log.i("Page:", strPageHTML);
            return strPageHTML;

        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }

    }
}



@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    String strPageHTML = null;
    DownloadTask objDT = new DownloadTask();
    imgvwCeleb = findViewById(R.id.imgvwCelebrity);

    try {
        strPageHTML = objDT.execute("https://www.forbesindia.com/lists/2019-celebrity-100/1819/all").get();
        
        String [] strarrPageHTML = strPageHTML.split("<div class=\"celeb-grid  last-entry\">");

        String strRequiredHTML = strarrPageHTML[0];
        ....

        }

The problem is, it's missing the main content that I need.

      //getting everything above this
               <!-- Celebrity Consumption -->
           <div class="list-consumption">
              <div class="celeb-grid-wrap flex-celeb" id="list_rank">

               //#### Here should be the content that I need....... 

               </div>
           </div>
         <!-- <div class="note-section">
              <p>
                 <span>NOTE :</span>
                 Click here for Methodology and Disclaimer
              </p>

           </div>-->
    //getting everything below this

What is missing is a list of Celebrities (please check the page source for the above link). Is the content missing because its being loaded dynamically through Ajax? Or am I doing something wrong?

0

There are 0 best solutions below