Home > Back-end >  Asynctask does not return string, but does not contain a reported error
Asynctask does not return string, but does not contain a reported error

Time:12-07

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() {

    }

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

            String myUrl;
            myUrl=par1[0];

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

            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", "google.com");

                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) {
                html.append(inputLine);
            }
            in.close();

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

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

        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 = "www.google.com";
 String content = new wsbRedirTest().execute(par);

CodePudding user response:

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.

  • Related