Returning result from 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
传递给asynckTask
的constructor
并在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 返回结果


- value & 是什么意思?0xff 在 Java 中做什么? 2022-01-01
- Java包名称中单词分隔符的约定是什么? 2022-01-01
- 从 finally 块返回时 Java 的奇怪行为 2022-01-01
- Jersey REST 客户端:发布多部分数据 2022-01-01
- Spring Boot连接到使用仲裁器运行的MongoDB副本集 2022-01-01
- Eclipse 插件更新错误日志在哪里? 2022-01-01
- Safepoint+stats 日志,输出 JDK12 中没有 vmop 操作 2022-01-01
- 如何使用WebFilter实现授权头检查 2022-01-01
- 将log4j 1.2配置转换为log4j 2配置 2022-01-01
- C++ 和 Java 进程之间的共享内存 2022-01-01