【问题标题】:Get a return value from OnPostExecute android [duplicate]从 OnPostExecute android 获取返回值 [重复]
【发布时间】:2017-09-04 12:50:35
【问题描述】:

首先我要说的是,我已经读过了:

How to get the return value from onpostexecute method in android?

还有:

How do I return a boolean from AsyncTask?

没有帮助,也许我不明白他们在说什么。 这是我的问题: 我只需要取回我的 OnPostExecute 方法中的字符串值。 不是来自 doInBackground,我需要它来自我的 OnPostExecute。

我的代码:

 public void max(String build,String cls)
{

    BackgroundTask3 myTask = new BackgroundTask3();
    myTask.execute(build,cls);


    Toast.makeText(getApplicationContext(),"fcur is" + Fcur,Toast.LENGTH_LONG).show();

}

和:

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

    String capacity_url;
    @Override
    protected void onProgressUpdate(Void... values) {
        super.onProgressUpdate(values);
    }

    @Override
    protected void onPostExecute(String result) {
    String finalValue;
      json_capacity = result;
        finalValue=parseMax();
        Toast.makeText(getApplicationContext(),"finalValue is" + finalValue,Toast.LENGTH_LONG).show();
        super.onPostExecute(result);

    }

    @Override
    protected void onPreExecute() {
        capacity_url = "http://www.skedge.co.il/ANCurrentCapacity.php";
    }

    @Override
    protected String doInBackground(String... params) {
        String cls,build;
        build= params[0];
        cls = params[1];

        try {
            URL url = new URL (capacity_url);
            HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
            httpURLConnection.setRequestMethod("POST");
            httpURLConnection.setDoOutput(true);
            OutputStream outputStream = httpURLConnection.getOutputStream();
            BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream,"UTF-8"));
            String data_string = URLEncoder.encode("Fclass","UTF-8")+"="+URLEncoder.encode(cls,"UTF-8")+"&"+
                    URLEncoder.encode("building","UTF-8")+"="+URLEncoder.encode(build,"UTF-8")+"&"+
                    URLEncoder.encode("hour","UTF-8")+"="+URLEncoder.encode(hour,"UTF-8");
            bufferedWriter.write(data_string);
            bufferedWriter.flush();
            bufferedWriter.close();
            //here is the new
            InputStream inputStream = httpURLConnection.getInputStream();
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream,"UTF-8"));
            StringBuilder stringBuilder = new StringBuilder();
            while ((JSON_STRING = bufferedReader.readLine())!= null)
            {
                stringBuilder.append(JSON_STRING+"\n");
            }
            outputStream.close();
            inputStream.close();
            httpURLConnection.disconnect();
            return stringBuilder.toString().trim();


        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        return null;

    }
}

我想做的就是: 将 OnPostExecute 内部 finalValue 字符串中的值分配给 max 函数内部的 Fcur(公共字符串)。

看起来很简单,但由于某种原因我无法完成。

【问题讨论】:

    标签: java android android-asynctask return return-value


    【解决方案1】:

    从后台线程获取回调的最佳方法是使用 interfaces 作为来自 AsyncTask 的回调,例如:

    创建一个可以在onPostExecute()中调用的接口

    public interface ResponseCallback {
    
        void onRespond();
    }
    

    在调用 asynckTask 之前定义如下:

       ResponseCallback callback = new ResponseCallback() {
                @Override
                public void onRespond() {
                  //code to be done after calling it from onPostExecute
                }
            };
    

    并将callback 传递给asynckTaskconstructor 并在onPostExecute 中调用它


    当然,您可以将interface 的签名修改为您想要的任何内容。

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-09-15
    • 1970-01-01
    • 2021-12-24
    • 2015-08-19
    • 2014-07-31
    • 2019-06-18
    • 1970-01-01
    相关资源
    最近更新 更多