package com.example.jianming.Tasks;

import android.os.AsyncTask;
import android.util.Log;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.net.HttpURLConnection;
import java.net.URL;

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

    @Override
    protected String doInBackground(String... urls) {
        try {
            return downloadUrl(urls[0]);
        } catch (IOException e) {
            e.printStackTrace();
            return "";
        }
        
    }



    private String downloadUrl(String myurl) throws IOException {
        InputStream is = null;
        int len = 500;
        try {
            URL url = new URL(myurl);
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setReadTimeout(10000);
            conn.setConnectTimeout(15000);
            conn.setRequestMethod("GET");
            conn.setDoInput(true);
            conn.connect();

            int response = conn.getResponseCode();
            Log.d("network", "The response is: " + response);
            is = conn.getInputStream();
            return readIt(is, len);

        } finally {
            if (is != null) {
                is.close();
            }
        }
    }

    private String readIt(InputStream is, int len) throws IOException {
        Reader reader = new InputStreamReader(is, "UTF-8");
        char[] buffer = new char[len];
        String content = "";
        int readLen;
        do {
            readLen = reader.read(buffer);
            if (readLen > 0) {
                content += new String(buffer).substring(0, readLen);
            }
        } while (readLen > 0);
        return content;
    }
}


@@ -610,19 +610,20 @@
 (String 
-myu
+strU
 rl) thro
@@ -674,31 +674,8 @@
 ll;%0A
-        int len = 500;%0A
     
@@ -718,11 +718,12 @@
 URL(
-myu
+strU
 rl);
@@ -1038,24 +1038,79 @@
 onseCode();%0A
+            int contentLen = conn.getContentLength();%0A%0A
             
@@ -1155,24 +1155,90 @@
  response);%0A
+            Log.d(%22network%22, %22Content length is: %22 + contentLen);%0A
             
@@ -1295,17 +1295,24 @@
 dIt(is, 
-l
+contentL
 en);%0A%0A  

read content length from response