简体   繁体   English

Asynctask 不返回字符串,但不包含报告的错误

[英]Asynctask does not return string, but does not contain a reported error

I'm passing a URL to an ASYNC class (wsbRedirTest) and I expect to receive the content of the page.我将 URL 传递给 ASYNC class (wsbRedirTest),我希望收到页面的内容。 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.我从 MainActivity 调用。

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

First off- AsyncTask is deprecated.首先 - AsyncTask 已弃用。 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.即使不是,对于 web 请求(尽管您看到了多少使用它的示例)来说,这也是一个糟糕的选择,因为它们在单个线程上的工作方式,任何其他异步任务都会在该请求上被阻止。

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.任何需要您希望它返回的值的代码都应该放在 onPostExecute 中。 The execute function doesn't return the result of your operation, it returns an instance of the AsyncTask itself. execute function 不返回操作结果,它返回 AsyncTask 本身的一个实例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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