问题描述
我要回应导致凌空要求。但因为这是异步的。我需要等待结果我再继续。如果我不我会得到nullobjects。
我如何设置的标志,等到标志是关闭。
categoryslashid =新的JSONObject [Category_IDs.size(); //包含所有类别列表的大小 taskfinished =新的布尔[Category_IDs.size()]; //布尔被初始化为假
//请求类/ {ID}获得
的for(int i = 0; I< Category_IDs.size();我++)
{makevolleyrequesforCategorySlashID(Const.URL_FULL_STOREURL + Const.URL_PRODUCT_GET_CATEGORY_ID,我);
}
公共无效makevolleyrequesforCategorySlashID(URL,ID)
{
//凌空实施
公共无效onResponseoverride
{
categoryslashid [I] =响应;
taskfinished [I] = TRUE;
}}
现在我必须继续进行后,我得到的任务全部完毕布尔成为真正的。
公共布尔areAllTrue()
{ 对于(布尔乙:taskfinished)(!b)如返回false;
返回true;
}
实现一个接口,并用它来打电话回来时,你的数据已准备就绪。事情是这样的:
公共接口OnDownloadTaskCompleted {
公共无效onTaskCompleted(列表< ofSomething>列表中,布尔错误,字符串消息);
}
然后,你应该通过这个实例你的要求,并覆盖 onTaskCompleted()
私人无效downloadData(){ 最后DownloadUsingVolley下载=新DownloadUsingVolley(getActivity());
downloader.retrieveData(新OnDownloadTaskCompleted(){
@覆盖
公共无效onTaskCompleted(列表< ofSomething>列表中,布尔错误,字符串消息){
//做一些与您的数据
}
});
}
我假设你有你的地方凌空实现的东西(DownloadusingVolley),并且还呼吁,并做出自己的请求(retrieveData)方法的类。
retrieveData可以这样实现的:
私人无效retrieveData(字符串URL,最终OnDownloadTaskCompleted taskCompleted){
最后JsonObjectRequest要求=新JsonObjectRequest(URL,空,新Response.Listener<&JSONObject的GT;(){
@覆盖
公共无效onResponse(JSONObject的响应){
尝试{
//解析
taskCompleted.onTaskCompleted(结果是,假,空);
}赶上(JSONException E){
taskCompleted.onTaskCompleted(0,真,e.getMessage());
}
}
},新Response.ErrorListener(){
@覆盖
公共无效onErrorResponse(VolleyError volleyError){
taskCompleted.onTaskCompleted(0,真,volleyError.getMessage());
}
});
//申请加入到队列中
。ApplicationClass.getInstance()addToRequestQueue(请求ATAG);
}
在这里您可以找到有关凌空一个很好的教程:
在Android的异步HTTP请求使用乱射
I have to respond to result of volley request. but because it is asynchronous. I need to wait for the results before I proceed. If I don't I will get nullobjects.
How do I set flags and wait until flags are off.
categoryslashid = new JSONObject[Category_IDs.size()];//size of the list containing all categories
taskfinished = new boolean[Category_IDs.size()];
//boolean is initialized to false
//Request to category/{id} to get
for(int i= 0;i<Category_IDs.size();i++)
{ makevolleyrequesforCategorySlashID(Const.URL_FULL_STOREURL+Const.URL_PRODUCT_GET_CATEGORY_ID,i);
}
public void makevolleyrequesforCategorySlashID(URL,id)
{
//volley implementation
public void onResponseoverride
{
categoryslashid[i]=response;
taskfinished[i]=true;
}
}
Now I must proceed after I get all the booleans in task finished become true.
public boolean areAllTrue()
{
for(boolean b : taskfinished) if(!b) return false;
return true;
}
Implement an Interface and use it to call back when your data is ready. Something like this:
public interface OnDownloadTaskCompleted {
public void onTaskCompleted(List<ofSomething> list, boolean error, String message);
}
Then you should pass an instance of this to your request and override onTaskCompleted()
private void downloadData(){
final DownloadUsingVolley downloader = new DownloadUsingVolley(getActivity());
downloader.retrieveData(new OnDownloadTaskCompleted() {
@Override
public void onTaskCompleted(List<ofSomething> list, boolean error, String message) {
//do something with your data
}
});
}
I'm assuming that you have a class where you implemented volley stuff (DownloadusingVolley) and a method do call on it and make the request itself (retrieveData).
retrieveData can be implemented like this:
private void retrieveData(String url, final OnDownloadTaskCompleted taskCompleted){
final JsonObjectRequest request = new JsonObjectRequest(url, null, new Response.Listener<JSONObject>(){
@Override
public void onResponse(JSONObject response) {
try {
//parse
taskCompleted.onTaskCompleted(result,false,null);
}catch (JSONException e){
taskCompleted.onTaskCompleted(0,true,e.getMessage());
}
}
},new Response.ErrorListener(){
@Override
public void onErrorResponse(VolleyError volleyError) {
taskCompleted.onTaskCompleted(0,true,volleyError.getMessage());
}
});
//adding request into the queue
ApplicationClass.getInstance().addToRequestQueue(request,"aTag");
}
Here you can find a nice tutorial about volley:Asynchronous HTTP Requests in Android Using Volley
这篇关于凌空,等待响应返回等到使用标志的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!