我有以下asynctask类,它不在活动中.在我正在初始化asynctask的活动中,我希望asynctask将回调报告回我的活动.可能吗?或者asynctask是否必须与活动位于同一个类文件中?
protected void onProgressUpdate(Integer... values)
{
super.onProgressUpdate(values);
caller.sometextfield.setText("bla");
}
Run Code Online (Sandbox Code Playgroud)
像这样的东西?
一个简单的问题:是否可以返回值AsyncTask?
//AsyncTask is a member class
private class MyTask extends AsyncTask<Void, Void, Void>{
protected Void doInBackground(Void... params) {
//do stuff
return null;
}
@Override
protected void onPostExecute(Void result) {
//do stuff
//how to return a value to the calling method?
}
}
Run Code Online (Sandbox Code Playgroud)
然后在我的Activity/ Fragment:
// The task is started from activity
myTask.execute()
// something like this?
myvalue = myTask.getvalue()
Run Code Online (Sandbox Code Playgroud)
编辑:这是很久以前我不熟悉Java的问题,现在我对它更好了,我会做一个简短的总结:
异步任务的重点是任务是asynchronous,这意味着在您调用execute()任务之后,任务开始在自己的线程上运行.从asynctask返回一个值是没有意义的,因为原始调用线程已经继续做其他事情(因此任务是异步的).
想想时间:在某个时间点,您启动了一个与主线程并行运行的任务.当并行运行任务完成时,主线程上也经过了时间.并行任务无法及时返回以将值返回给主线程.
我是从C来的,所以我对此并不了解.但似乎很多人都有同样的问题,所以我想我会稍微澄清一下.
我对编程很新,我有些疑惑.
我有一个AsyncTask我称之为RunInBackGround.
我开始这个过程就像:
new RunInBackGround().execute();
Run Code Online (Sandbox Code Playgroud)
但是我希望等到这个调用完成它的执行,然后继续执行其他代码语句.
我怎样才能做到这一点?
有什么办法吗?
我有以下课程:
public class getURLData extends AsyncTask<String, Integer, String>{
@Override
protected String doInBackground(String... params) {
String line;
try {
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(params[0]);
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
line = EntityUtils.toString(httpEntity);
} catch (UnsupportedEncodingException e) {
line = "<results status=\"error\"><msg>Can't connect to server</msg></results>";
} catch (MalformedURLException e) {
line = "<results status=\"error\"><msg>Can't connect to server</msg></results>";
} catch (IOException e) {
line = "<results status=\"error\"><msg>Can't connect to server</msg></results>";
}
return line;
}
@Override …Run Code Online (Sandbox Code Playgroud) 我正在使用AsyncTask具有以下签名的类:
public class ApiAccess extends AsyncTask<List<NameValuePair>, Integer, String> {
...
private String POST(List<NameValuePair>[] nameValuePairs){
...
return response;
}
}
protected String doInBackground(List<NameValuePair>... nameValuePairs) {
return POST(params);
}
Run Code Online (Sandbox Code Playgroud)
我试图从其他类通过以下方式调用它:
ApiAccess apiObj = new ApiAccess (0, "/User");
// String signupResponse = apiObj.execute(nameValuePairs);
String serverResponse = apiObj.execute(nameValuePairs); //ERROR
Run Code Online (Sandbox Code Playgroud)
但在这里我得到这个错误:
Type mismatch: cannot convert from AsyncTask<List<NameValuePair>,Integer,String> to String
Run Code Online (Sandbox Code Playgroud)
为什么我String在Class扩展行中指定了第三个参数?
我正在尝试使用Android解压缩文件夹AsyncTask.该类(称为Decompress)是一个内部类,Unzip其中Unzip本身是一个非Activity类.伪代码是:
public class Unzip {
private String index;
private String unzipDest; //destination file for storing folder.
private Activity activity;
private boolean result; //result of decompress.
public void unzip(String loc) {
Decompress workThread = new Decompress(loc, activity);
workThread.execute();
if(unzip operation was successful) {
display(index);
}
//Class Decompress:
class Decompress extends AsyncTask<Void, Integer, Boolean> {
private ProgressDialog pd = null;
private Context mContext;
private String loc;
private int nEntries;
private int entriesUnzipped;
public Decompress(String location, Context c) { …Run Code Online (Sandbox Code Playgroud) 我一直在使用异步任务来访问Web服务器并使用结果更新控件.这有缺点,即它使异步方法特定于控制并使我再次使用返回的字符串.
如何从异步调用onPostExecute返回结果字符串?我怎么称呼它?我似乎无法让我的代码能够做到这一点.线程应该没有问题,因为我有一个冻结UI的对话框,直到完成工作.
我的典型asyncTask代码如下
class GetDataFromServer extends AsyncTask<String, String, String>
{
* */
// Progress Dialog
private ProgressDialog qDialog;
private Context context;
private String dialogString;
private ArrayList<String[]> newLoginResult;
// JSON parser class
String url_newGame ="http://xxxxxx.php";
public myAsyncMethos(String dialogMessage, Context con)
{
this.qDialog = new ProgressDialog(con);
this.dialogString = dialogMessage;
this.context = con;
}
/**
* Before starting background thread Show Progress Dialog
* */
@Override
protected void onPreExecute()
{
super.onPreExecute();
qDialog = new ProgressDialog(this.context);
qDialog.setMessage(this.dialogString);
qDialog.setIndeterminate(false);
qDialog.setCancelable(false);
qDialog.show();
}
@Override
protected JSONObject …Run Code Online (Sandbox Code Playgroud) 我遇到了一个问题.我需要使用asynctask来检索JSON数据,在移动到程序的下一部分之前我需要这些数据.但是,当我使用AsyncTask的get()方法时,在看到数据显示之前,我有5到8秒的黑屏.我想在数据检索期间显示进度对话框,但由于黑屏,我无法执行此操作.有没有办法放入另一个线程?这是一些代码
的AsyncTask
public class DataResponse extends AsyncTask<String, Integer, Data> {
AdverData delegate;
Data datas= new Data();
Reader reader;
Context myContext;
ProgressDialog dialog;
String temp1;
public DataResponse(Context appcontext) {
myContext=appcontext;
}
@Override
protected void onPreExecute()
{
dialog= new ProgressDialog(myContext);
dialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
dialog.setCancelable(false);
dialog.setMessage("Retrieving...");
dialog.show();
};
@Override
protected Data doInBackground(String... params) {
temp1=params[0];
try
{
InputStream source = retrieveStream(temp1);
reader = new InputStreamReader(source);
}
catch (Exception e)
{
e.printStackTrace();
}
Gson gson= new Gson();
datas= gson.fromJson(reader, Data.class);
return datas;
}
@Override
protected void …Run Code Online (Sandbox Code Playgroud) 我在网上搜索了很长时间.也许我在这里做的事情是错的.
我在MainActivity.java的单独文件中编写了一个线程类.因为线程和主要活动都相对较长,所以我决定将它们分成不同的文件.
我想将线程类生成的一些值传递给主活动.最初我想使用处理程序.但是因为线程与主要活动属于不同的类.它不知道我在主活动中定义的处理程序.
public class mythread implements Runnable{
@Override
public void run(){
result = result_from_some_task();
}
}
Run Code Online (Sandbox Code Playgroud)
这是我的线程类的基本结构,我想将结果传递回主活动.我看了很多例子,其中大多数是主要活动类中的线程,并且可以很容易地引用定义的处理程序.
意图似乎不适用.有没有人知道如何进行这样的操作?
提前致谢.
我已经看到了这个答案: 如何将OnPostExecute()的结果导入主活动,因为AsyncTask是一个单独的类?
但这不是我要问的,因为我的调用类有多个调用AsyncTask.
我想创建一个通用AsyncTask类,它将接受URL并从给定的URL下载内容并返回下载的内容.
例:
public class MyAsyncTask extends AsyncTask<String, Void, String>{
ProgressDialog dialog;
protected void onPreExecute() {
super.onPreExecute();
dialog = ProgressDialog.show(context, "Loading", "Please wait...", true);
}
protected String doInBackground(String... params) {
// do download here
}
protected void onPostExecute(String result) {
super.onPostExecute(result);
dialog.dismiss();
}
}
Run Code Online (Sandbox Code Playgroud)
我有另一个类AsyncTask在不同的场景中调用它
public class MyClass {
public method1(String url) {
String result = new MyAsyncTask().execute(url).get();
}
public method2(String url) {
String result = new MyAsyncTask().execute(url).get();
}
}
Run Code Online (Sandbox Code Playgroud)
我知道使用get() …