-1

I have cleaned and rebuild my code as well but still the issue is not solved.

Below is the code:

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        String result = null;
        String stringUrl = "https://www.ecowebhosting.co.uk/";
        DownloadTask downloadTask = new DownloadTask();

        downloadTask.execute(stringUrl);
    }

    public class DownloadTask extends AsyncTask<String, Void, String> {
        @Override
        protected String doInBackground(String... urls) {
            String result = "";
            URL url;
            HttpURLConnection httpURLConnection = null;
            try {
                url = new URL(urls[0]);
                //It is like opening a browser
                httpURLConnection = (HttpURLConnection) url.openConnection();
                InputStream in = httpURLConnection.getInputStream();
                InputStreamReader reader = new InputStreamReader(in);
                int data = reader.read();
                while (data != -1) {
                    char currentChar = (char) data;
                    result = result + currentChar;
                    data = reader.read();
                }
                return result;
            } catch (Exception e) {
                e.printStackTrace();
                return "Failed";
            }
        }
        @Override
        protected void onPostExecute(String s) {
            super.onPostExecute(s);

            Log.i("Results",s);
        }
    }
}

The code is running fine but nothing is printed in the logs. The following is the log:

screenshot of the logs

hata
  • 11,633
  • 6
  • 46
  • 69
Asif Ali Khan
  • 49
  • 1
  • 12
  • Check the [AsyncTask example code](https://developer.android.com/reference/android/os/AsyncTask). It is the `onPostExecute` part that I would recommend. – Tigger Mar 14 '19 at 09:42
  • i have updated the code using onPostExecute() but still doesnt work – Asif Ali Khan Mar 14 '19 at 10:01

2 Answers2

0

AsyncTask is an asynchronous process. So when you call Log.i("Result:", result);, the AsyncTask is not finished and result is still empty.

You should print your result from onPostExecute() method. You can look on this page.

Here is some examples on how to implement an AsyncTask correctly:

Best

hata
  • 11,633
  • 6
  • 46
  • 69
Maxouille
  • 2,729
  • 2
  • 19
  • 42
0

You only have to change code inside your doInBackGround

public class DownloadTask extends AsyncTask<String, Void, String> {
    @Override
    protected String doInBackground(String... urls) {
        String result;
        String inputLine;
        try {
            URL myUrl = new URL(urls[0]);
            HttpURLConnection connection =(HttpURLConnection)
                    myUrl.openConnection();
            connection.setReadTimeout(150000); 
            connection.setConnectTimeout(15000); 
            connection.setRequestMethod("GET");
            connection.connect();
            InputStreamReader streamReader = new InputStreamReader(connection.getInputStream());
            //Create a new buffered reader and String Builder
            BufferedReader reader = new BufferedReader(streamReader);
            StringBuilder stringBuilder = new StringBuilder();
            //Check if the line we are reading is not null
            while((inputLine = reader.readLine()) != null){
                stringBuilder.append(inputLine);
            }
            //Close our InputStream and Buffered reader
            reader.close();
            streamReader.close();
            //Set our result equal to our stringBuilder
            result = stringBuilder.toString();
        } catch (Exception e) {
            e.printStackTrace();
            return "error";
        }
        return result;
    }

    @Override
    protected void onPostExecute(String s) {
        super.onPostExecute(s);

        Log.i("Results",s);
    }
}
hata
  • 11,633
  • 6
  • 46
  • 69
Mukul Bhardwaj
  • 562
  • 5
  • 16