Android:从回调中获取结果(网络KOUSH ION) [英] Android: get result from callback (networking KOUSH ION)
问题描述
对于我的应用程序,我需要从我们的服务器联系我们的 API,它返回一些 JSON.
For my app I need to contact our API from our server which returns some JSON.
在下载 JSON 时,它应该显示一个进度条.
While downloading the JSON, it should display a progressbar.
我想我应该使用 Android 的 AsyncTask
在进行网络操作时处理 GUI,所以我在我的 Activity
中编写了以下内容:
I figured I should use Android's AsyncTask
to handle the GUI while doing network operations, so I wrote the following within my Activity
:
class DownloadManager extends AsyncTask<String, Void, Boolean> {
@Override
protected void onPreExecute() {
super.onPreExecute();
mLoadingSpinner.setVisibility(View.VISIBLE);
}
@Override
protected Boolean doInBackground(String... params) {
String id = params[0];
downloadUtility.getId(id);
return true;
}
@Override
protected void onPostExecute(Boolean result) {
super.onPostExecute(result);
mLoadingSpinner.setVisibility(View.INVISIBLE);
}
}
基本上,onPreExecute
显示加载微调器,doInBackGround
下载一些 JSON,onPostExecute
停止微调器.
Basically, onPreExecute
shows the loading spinner, the doInBackGround
downloads some JSON, and onPostExecute
stops the spinner.
问题是,在 downloadUtility.getId(id)
中,我需要:
The question is, within the downloadUtility.getId(id)
I need to either:
- 如果下载成功,则打开一个新的意图.
- 如果下载失败,则保持相同的活动并显示错误提示.
- Open a new intent if the download succeeded.
- Stay on the same activity and display an error toast if the download failed.
getId
的代码:
public Future getId(final String id) {
// set url
String url = IPAddress.PRODUCTION + Variables.get_id+ id;
downloading = Ion.with(context)
.load("GET", url)
.asJsonObject()
.withResponse()
.setCallback(new FutureCallback<Response<JsonObject>>() {
@Override
public void onCompleted(Exception e, Response<JsonObject> response) {
//try catch here for null getHeaders
if (response != null) {
if (response.getHeaders().code() == 200) {
//SUCCESS !! Open new intent!
} else {
//FAIL!! Show TOAST!
}
}
}
});
return downloading;
}
如您所见,我将返回一个 future 对象.我如何从未来的对象中知道 onCompleted
(void) 是成功还是失败,以便我可以在 asynctask 中处理结果(成功:打开新意图,失败:toast)?
As you can see, I'm returning a future object. How do I know from the future object if the onCompleted
(void) either gave a success or fail, so I can handle the result (success: open new intent, fail: toast) in the asynctask?
推荐答案
在这里,您正在另一个异步任务中运行一个异步任务,这不是您可以在活动中直接调用 getId 方法的正确方法,不需要另一个异步任务因为下面的代码本身就是一个异步任务.
Here you are running one asynctask inside another asyctask this is not a proper way you can call your getId method directly in your activity it won't be required another asynctask because the following code it self a asynctask.
downloading = Ion.with(context)
.load("GET", url)
.asJsonObject()
.withResponse()
.setCallback(new FutureCallback<Response<JsonObject>>() {
@Override
public void onCompleted(Exception e, Response<JsonObject> response) {
//try catch here for null getHeaders
if (response != null) {
if (response.getHeaders().code() == 200) {
//SUCCESS !! Open new intent!
} else {
//FAIL!! Show TOAST!
}
}
}
});
//添加新答案
如果您想将整个下载代码与您的活动分开,那么您可以在您的下载实用程序类中创建自定义回调.它将充当活动和您的下载类之间的通信器.我只是在下面提供了一种完成此任务的方法.
If you want to separate entire download code from your activity then you can create custom callBack in your download Utility class. It will acts like a communicator between activity and your Download class. I just give a way to do this task on bellow.
DownloadUtility 类接缝如下图
public class DownloadUtility {
//DO Your all other Stuff
/**
* Custom Callback
*/
public interface customCallBack {
void onCompleted(Exception e, Response<JsonObject> response);
}
/**
* Your getID code
*
* @param context
* @param id
* @param mLoadingSpinner
* @param callBack
*/
public static void getId(Activity context,final String id, Spinner mLoadingSpinner, final customCallBack callBack) {
// set url
mLoadingSpinner.setVisibility(View.VISIBLE);
String url = IPAddress.PRODUCTION + Variables.get_id + id;
downloading = Ion.with(context)
.load("GET", url)
.asJsonObject()
.withResponse()
.setCallback(new FutureCallback<Response<JsonObject>>() {
@Override
public void onCompleted(Exception e, Response<JsonObject> response) {
mLoadingSpinner.setVisibility(View.GONE);
if(callBack != null)
callBack.onCompleted(e,response);
}
}
});
}
}
拨打您的活动
DownloadUtility.getId(this, "ID", spinnerObj, new DownloadUtility.customCallBack() {
@Override
public void onCompleted(Exception e, Response<JsonObject> response) {
if (response != null) {
if (response.getHeaders().code() == 200) {
//SUCCESS !! Open new intent!
} else {
//FAIL!! Show TOAST!
}
}
});
这篇关于Android:从回调中获取结果(网络KOUSH ION)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!