java – 从Asynctask返回结果

如果我在我的Android应用程序中有这个后台工作文件并且从我的数据库获取数据,我怎么能将字符串’result’传递给另一个类?
后台工作者连接到我的服务器,然后使用PHP连接到数据库.

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

}

解决方法:

后台线程获取回调的最佳方法是使用接口作为AsyncTask的回调,例如:

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

public interface ResponseCallback {

    void onRespond(String result);
}

调用asynckTask之前定义它如下:

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

并将cpk传递给asynckTask的构造函数,并在onPostExecute中调用它,如下所示:

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

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

相关文章

Android性能优化——之控件的优化 前面讲了图像的优化,接下...
前言 上一篇已经讲了如何实现textView中粗字体效果,里面主要...
最近项目重构,涉及到了数据库和文件下载,发现GreenDao这个...
WebView加载页面的两种方式 一、加载网络页面 加载网络页面,...
给APP全局设置字体主要分为两个方面来介绍 一、给原生界面设...
前言 最近UI大牛出了一版新的效果图,按照IOS的效果做的,页...