简体   繁体   中英

Problem in downloading webcontent in with Asynctask android java

I am working for the first time in android studio with java.I created this code for downloading web content and I used Asynctask, but DownloadTask class doesn't return results.I don't have any idea where is the problem. And it shows the below messages in Logcat

D/NetworkSecurityConfig: No Network Security Config specified, using platform default

I/zygote: Waiting for a blocking GC ProfileSaver

I/zygote: WaitForGcToComplete blocked ProfileSaver on HeapTrim for 11.085ms

Main_activity

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

    @Override
    protected String doInBackground(String... urls) {

        String dataResult = "";
        URL url;
        HttpsURLConnection urlConnection = null;
        try {

            url = new URL(urls[0]);

            urlConnection = (HttpsURLConnection) url.openConnection();

            InputStream in = urlConnection.getInputStream();
            InputStreamReader reader = new InputStreamReader(in);
            int data = reader.read();
            while (data != -1) {

                char current = (char) data;
                dataResult += current;
                data = reader.read();
            }
            return dataResult;
        } catch (Exception e) {
            e.printStackTrace();
            return "failed";
        }
    }
}

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

    DownloadTask tasks = new DownloadTask();
    String result = null;
    try {
        result = tasks.execute("https://www.imdb.com/list/ls052283250/").get();

        Log.i("downloaded The URL",result);

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

}

Although it is a repeated question, I'll answer this. I faced this issue too (not an issue anymore). It is the waiting time for the android studio to process the response character by character.

I suggest you to use BufferedReader for faster reading.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM