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 返回结果
基础教程推荐
- Java:带有char数组的println给出乱码 2022-01-01
- 减少 JVM 暂停时间 >1 秒使用 UseConcMarkSweepGC 2022-01-01
- 如何使用 Java 创建 X509 证书? 2022-01-01
- Java Keytool 导入证书后出错,"keytool error: java.io.FileNotFoundException &拒绝访问" 2022-01-01
- 降序排序:Java Map 2022-01-01
- “未找到匹配项"使用 matcher 的 group 方法时 2022-01-01
- FirebaseListAdapter 不推送聊天应用程序的单个项目 - Firebase-Ui 3.1 2022-01-01
- 无法使用修饰符“public final"访问 java.util.Ha 2022-01-01
- 设置 bean 时出现 Nullpointerexception 2022-01-01
- 在 Libgdx 中处理屏幕的正确方法 2022-01-01