从 Asynctask 返回结果

Returning result from Asynctask(从 Asynctask 返回结果)

本文介绍了从 Asynctask 返回结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我的 android 应用程序中有这个后台工作文件并且它从我的数据库中获取数据,我如何将字符串result"传递给另一个类?后台工作人员连接到我的服务器,然后使用 php 连接到数据库.

If I have this background worker file in my android application and it gets data from my database how can I pass the string 'result' to another class? The background worker connects to my server and then using php it connects to a database.

public class BackgroundWorker extends AsyncTask<String,Void,String> {
    Context context;
    AlertDialog alertDialog;
    BackgroundWorker (Context ctx) {
        context = ctx;
    }

    @Override
    public String doInBackground(String... params) {
        String type = params[0];
        String specials_url = "";

        if(type.equals("venue click")) {
            try {
                //String user_name = params[1];
                URL url = new URL(specials_url);
                HttpURLConnection httpURLConnection = (HttpURLConnection)url.openConnection();
                httpURLConnection.setRequestMethod("POST");
                httpURLConnection.setDoOutput(true);
                httpURLConnection.setDoInput(true);
                OutputStream outputStream = httpURLConnection.getOutputStream();
                BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream, "UTF-8"));
              //  String post_data = URLEncoder.encode("user_name","UTF-8")+"="+URLEncoder.encode(user_name,"UTF-8");
               // bufferedWriter.write(post_data);
                bufferedWriter.flush();
                bufferedWriter.close();
                outputStream.close();
                InputStream inputStream = httpURLConnection.getInputStream();
                BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream,"iso-8859-1"));
                String result="";
                String line="";
                while((line = bufferedReader.readLine())!= null) {
                    result += line;
                }
                bufferedReader.close();
                inputStream.close();
                httpURLConnection.disconnect();
                return result;
            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }

        }
        return null;
    }

    @Override
    protected void onPreExecute() {
        alertDialog = new AlertDialog.Builder(context).create();
        alertDialog.setTitle("Info");
    }

    @Override
    protected void onPostExecute(String result) {
        alertDialog.setMessage(result);
        alertDialog.show();

       // String temp = "login success";

      //  if (result.equals(temp)) {
       //     Intent intent = new Intent(context, Register.class);
         //   context.startActivity(intent);
      //  }


    }

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

}

推荐答案

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

Best way to get a callback from background thread is to use interfaces as a callback from AsyncTask for example:

创建一个可以在onPostExecute()

public interface ResponseCallback {

    void onRespond(String result);
}

在调用 asynckTask 之前像这样定义它:

and before calling asynckTask define it like this:

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

并将cpk 传递给asynckTaskconstructor 并在onPostExecute 中像这样调用它:

and pass cpk to the constructor of of the asynckTask and call it in onPostExecute like that:

if(cpk!=null){
  cpk.onRespond(result);
}

<小时>

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


of course you can modify the signature of the interface to what ever you want.

这篇关于从 Asynctask 返回结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:从 Asynctask 返回结果

基础教程推荐