Asynctask does not return string, but does not contain a reported error

Asynctask does not return string, but does not contain a reported error

Problem Description:

I’m passing a URL to an ASYNC class (wsbRedirTest) and I expect to receive the content of the page. However, it does not return any results.

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

    private Object[] values;

    protected void onPreExecute() {


    protected String doInBackground(String... par1) {
        try {

            String myUrl;

            URL obj = new URL(myUrl);
            HttpURLConnection conn = (HttpURLConnection) obj.openConnection();
            conn.addRequestProperty("Accept-Language", "en-US,en;q=0.8");
            conn.addRequestProperty("User-Agent", "Mozilla");
            conn.addRequestProperty("Referer", "");

            System.out.println("Request URL ... " + myUrl);

            boolean redirect = false;

            // normally, 3xx is redirect
            int status = conn.getResponseCode();
            if (status != HttpURLConnection.HTTP_OK) {
                if (status == HttpURLConnection.HTTP_MOVED_TEMP
                        || status == HttpURLConnection.HTTP_MOVED_PERM
                        || status == HttpURLConnection.HTTP_SEE_OTHER)
                    redirect = true;

            System.out.println("Response Code ... " + status);

            if (redirect) {

                // get redirect url from "location" header field
                String newUrl = conn.getHeaderField("Location");

                // get the cookie if need, for login
                String cookies = conn.getHeaderField("Set-Cookie");

                // open the new connnection again
                conn = (HttpURLConnection) new URL(newUrl).openConnection();
                conn.setRequestProperty("Cookie", cookies);
                conn.addRequestProperty("Accept-Language", "en-US,en;q=0.8");
                conn.addRequestProperty("User-Agent", "Mozilla");
                conn.addRequestProperty("Referer", "");

                System.out.println("Redirect to URL : " + newUrl);


            BufferedReader in = new BufferedReader(
                    new InputStreamReader(conn.getInputStream()));
            String inputLine;
            StringBuffer html = new StringBuffer();

            while ((inputLine = in.readLine()) != null) {

            System.out.println("URL Content... n" + html.toString());
            return html.toString();

        } catch (Exception e) {

        return null;

I believe the problem is here. I’m probably not calling it properly. But I did several tests and did not get the result I wanted. I am calling from MainActivity.

 String par = "";
 String content = new wsbRedirTest().execute(par);

Solution – 1

First off- AsyncTask is deprecated. Don’t use it. Even when it wasn’t, it was a bad choice for web requests (despite how many examples you see using it) as due to how they work on a single thread any other asynctask would be blocked on that request.

Secondly- that’s not how asynchronous code works. It’s asynchronous. It doesn’t return a value, because it won’t know the value to return until much later. Any code that needs the value you want it to return should instead be put in onPostExecute. The execute function doesn’t return the result of your operation, it returns an instance of the AsyncTask itself.

Rate this post
We use cookies in order to give you the best possible experience on our website. By continuing to use this site, you agree to our use of cookies.