簡體   English   中英

Asynctask 不返回字符串,但不包含報告的錯誤

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

我將 URL 傳遞給 ASYNC class (wsbRedirTest),我希望收到頁面的內容。 但是,它不返回任何結果。

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;
    }

我相信問題就在這里。 我可能沒有正確調用它。 但是我做了幾次測試並沒有得到我想要的結果。 我從 MainActivity 調用。

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

首先 - AsyncTask 已棄用。 不要使用它。 即使不是,對於 web 請求(盡管您看到了多少使用它的示例)來說,這也是一個糟糕的選擇,因為它們在單個線程上的工作方式,任何其他異步任務都會在該請求上被阻止。

其次——這不是異步代碼的工作方式。 它是異步的。 它不返回值,因為它要到很久以后才會知道要返回的值。 任何需要您希望它返回的值的代碼都應該放在 onPostExecute 中。 execute function 不返回操作結果,它返回 AsyncTask 本身的一個實例。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM