Android 从其成员的 none 片段调用 AsyncTask - doInbackground、onpreexecute、onpostexecute

Android AsyncTask called from fragment none of its members called - doInbackground, onpreexecute, onpostexecute

我正在尝试读取 returns 0 或 -1 的 uri。 异步任务从片段内部调用。 基本上,当用户单击一个按钮时,它是一个登录,在远程服务器上检查输入。

在调试器中,任务显示为 运行 但从未完成,同时调用了覆盖成员的 none,这可以解释为什么它什么都不做。

登录码是:

// Validate the user.
    String uri = HLConsts.SERV_URI + "ValidateLoginUser?uname=" + szUserName + "&password=" + szPassword + "&android=1";
    ReadUri rd = new ReadUri(uri);
    int cnt =0;
    rd.execute();
    while ( rd.getSzReturn() == null  || cnt > 5) {
        try {
            Thread.sleep(500);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        cnt++;
    }
    if(cnt > 5) {
        errors++;
    }

    String szValid = rd.getSzReturn();

        Snackbar.make(root, "Name = " + szUserName + " Pass = " + szPassword , Snackbar.LENGTH_LONG)
            .setAction("Action", null).show();

============================ 异步任务是:

public class ReadUri  extends AsyncTask< Void , Void, String> {
 //   private Context mContext;
    private String mUrl;
    private String szReturn = null;



    public String getSzReturn() {
        return szReturn;
    }

    public ReadUri(  String url) {
      //  mContext = context;
        mUrl = url;
    }

    @Override
    protected void onPreExecute() {
        super.onPreExecute();


     //   dynamictext = (TextView)   myView.findViewById(R.id.textview_first);
    }

    @Override
    protected String doInBackground(Void... params) {
        String resultString = null;
        resultString = getString(mUrl);

        return resultString;
    }

    @Override
    protected void onPostExecute(String strings) {
        super.onPostExecute(strings);
        szReturn =strings;
    }


    //======================================================================================
    private String getString(String InUrl) {

        String ret = "Nothing";


        URL url = null;
        try {
            url = new URL(InUrl);
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }


        HttpURLConnection httpURLConnection = null;
        try {
            httpURLConnection = (HttpURLConnection) url.openConnection();
        } catch (IOException e) {
            e.printStackTrace();
        }

        // The line below is where the exception is called
        int response = 0;
        try {
            response = httpURLConnection.getResponseCode();
        } catch (IOException e) {
            e.printStackTrace();
        }
        Log.i("Response", String.valueOf(response));

        if (response == HttpURLConnection.HTTP_OK) {
            // Response successful
            InputStream inputStream = null;
            try {
                inputStream = httpURLConnection.getInputStream();
            } catch (IOException ex) {
                ex.printStackTrace();
            }

            // Parse it line by line
            BufferedReader bufferedReader = new BufferedReader(
                    new InputStreamReader(inputStream));
            String line;
            StringBuffer sb = new StringBuffer();
            while (true) {
                try {
                    //             if (!((temp = bufferedReader.readLine()) != null)) {
                    while((line=bufferedReader.readLine())!=null)
                    {
                        //   sb.append(line);      //appends line to string buffer
                        //   sb.append("\n");     //line feed
                        ret += line;
                        Log.w("MyApp",line);
                    }
                } catch (IOException ex) {
                    ex.printStackTrace();
                }
                // Parsing of data here

            }
        } else {
            Log.e("SAMES", "INCORRECT RETURN CODE: " + response);
        }

        httpURLConnection.disconnect();

        //  urlConnection.disconnect();
        try {
            Thread.sleep(500);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return ret;
    }
}

请你帮忙。我已经设置了断点,其中 none 个断点被触发并且 doInBackground 从未被调用。

谢谢 杰夫

这已通过不使用外部 class 到 运行 异步任务来解决。

示例如下: AsyncTask Android example