AsyncTask返回结果
我目前无法从从 JSON 连接获取数据的 AsyncTask 获取值。我看过一些示例,但大多数情况下我只看到了 AsyncTask 的发布结果。
首先,我有一个名为 Dog 的对象,它只有一个字符串、名称。我正在尝试从服务器获取狗的名字。 在 oncreate 中使用以下代码,我启动 DogAsyncTask,同时输入名为 n 的 URL 和 Dog d_in。
Dog d_in = new Dog("DogName");
DogAsyncTask task = new DogAsyncTask(d_in);
String n = "www.dog.com";
task.execute(n);
Log.e("Out", d_in.getName());
我的 AsyncTask 如下:
private class DogAsyncTask extends AsyncTask<String, Void, String> {
Dog d = null;
DogAsyncTask(Dog d){
this.d = d;
}
ProgressDialog mProgressDialog;
@Override
protected void onPostExecute(String result) {
d.setName(result);
Log.e("Dog", d.getName());
mProgressDialog.dismiss();
}
@Override
protected void onPreExecute() {
mProgressDialog = ProgressDialog.show(AsyncTestActivity.this, "Loading...", "Data is Loading...");
}
@Override
protected String doInBackground(String... name) {
//Go to url = name and then gets String below.
String outfromjson = "new dog name"; //This will be a function that gets a name from JSON
return outfromjson;
}
}
我尝试使用类似的东西 Log.e("Out", task.d.getName()); 但我不断得到狗的默认名称“DogName”。如何从 AsyncTask 中取出值?
I am currently having trouble getting a value from an AsyncTask that gets data from a JSON connection. I have looked at a few examples, but mostly I have only seen posting results from AsyncTask.
First I have an object called Dog that only has a String, Name. I am trying to get the Name of the dog from the server.
Using the following code in my oncreate, I start the DogAsyncTask while assing in an URL called n and a Dog d_in.
Dog d_in = new Dog("DogName");
DogAsyncTask task = new DogAsyncTask(d_in);
String n = "www.dog.com";
task.execute(n);
Log.e("Out", d_in.getName());
My AsyncTask is as follows:
private class DogAsyncTask extends AsyncTask<String, Void, String> {
Dog d = null;
DogAsyncTask(Dog d){
this.d = d;
}
ProgressDialog mProgressDialog;
@Override
protected void onPostExecute(String result) {
d.setName(result);
Log.e("Dog", d.getName());
mProgressDialog.dismiss();
}
@Override
protected void onPreExecute() {
mProgressDialog = ProgressDialog.show(AsyncTestActivity.this, "Loading...", "Data is Loading...");
}
@Override
protected String doInBackground(String... name) {
//Go to url = name and then gets String below.
String outfromjson = "new dog name"; //This will be a function that gets a name from JSON
return outfromjson;
}
}
I tried using something like
Log.e("Out", task.d.getName());
but I keep getting the default name of the dog which is "DogName". How to I carry values out of AsyncTask?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
好的,这里发生的事情是:
您想要传递
Dog
作为引用(pointers
如 C / C++ / Objective-C 中的那样),并且您想使用其他变量反映原始变量的变化。
但请记住,Java 不通过引用传递对象,Java 通过值传递对象(仅传输值,创建另一个对象并为其赋值)。
因此,您可以做的是再次将该结果持有者对象作为值分配给原始对象。
OK, The thing what is happening here is:
You want to pass
Dog
as reference (pointers
as in C / C++ / Objective-C),and you want to reflect change in original variable using other variable.
but keep in mind that Java doesn't pass object by reference, Java passes objects by value (only value is transferred, another object is created and assigned value).
So what you can do is you can assign that result-holder object again as a value to the original object.
您可以执行以下操作,
在活动中声明并实现一个接口,然后使用它的方法作为 onPostExecute 方法的回调。
You can do the following,
Declare and implement an interface in your activity and then use it's methods as a callback from the onPostExecute method.