我是Android Development
的新手,我正在尝试开发我的第一个Android应用,该应用使用public APIs
从某些Android Volley
获取数据。
我正在使用在启动器活动中初始化的singleton Volley Request Queue
。当我在Fragment layout/view (uses RecyclerView & CardView)
内设置RecyclerView
个适配器时,我成功地能够解析JSON内容并在Volley's JsonObjectRequest
上显示它们。
以下代码确实显示数据,但遭遇时间竞争条件。 注意:RvJoiner是一个库,它合并多个适配器,并按照FIRST COME FIRST SERVE基础订购一个适配器。
My Fragment类如下:
public class Fragment1 extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.recylcer_main, container, false);
ParseJSON parseJSON = new ParseJSON(v);
parseJSON.makeRequest1();
parseJSON.makeRequest2();
return v;
}
}
我的ParseJSON类如下
public class ParseJSON {
private static final String URL1 = "some url";
private static final String URL2 = "some other url";
private static final String TAG = "ParseJSON";
private RequestQueue requestQueue;
private boolean FLAG_REQUEST1_FETCHED;
private boolean FLAG_REQUEST2_FETCHED;
private ArrayList<status1> status1ArrayList;
private ArrayList<status2> status2ArrayList;
private Context context;
private RvJoiner rvJoiner;
private View view;
ProgressDialog pd;
ParseJSON (View v){
this.view= v;
this.context=v.getContext();
pd = ProgressDialog.show(v.getContext(), "Please Wait", "Getting Data from APIs", true);
requestQueue = AppController.getInstance(v.getContext()).getRequestQueue();
rvJoiner = new RvJoiner();
}
public void makeRequest1() {
JsonObjectRequest request1 = new JsonObjectRequest(Request.Method.GET, URL1,
null, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
/* Parsing Stuff and storing it in status1ArrayList */
FLAG_REQUEST1_FETCHED=true;
Status1Adapter status1Adapter = new Status1Adapter(status1ArrayList);
RecyclerView recList = (RecyclerView) view.findViewById(R.id.cardList);
recList.setLayoutManager(new LinearLayoutManager(context));
rvJoiner.add(new JoinableAdapter(status1Adapter));
recList.setAdapter(rvJoiner.getAdapter());
pd.dismiss();
}
} catch (JSONException e) {}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {}
});
AppController.getInstance(context).addToRequestQueue(request1);
}
public void makeRequest2() {
JsonObjectRequest request2 = new JsonObjectRequest(Request.Method.GET, URL2,
null, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
/* Parsing stuff and storing it inside ArrayList status2ArrayList */
FLAG_REQUEST2_FETCHED=true;
Status2Adapter status2Adapter = new Staus2Adapter(status2ArrayList);
RecyclerView recList = (RecyclerView) view.findViewById(R.id.cardList);
recList.setLayoutManager(new LinearLayoutManager(context));
rvJoiner.add(new JoinableAdapter(status2Adapter));
}
} catch (JSONException e) {}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {}
});
AppController.getInstance(context).addToRequestQueue(request2);
}
public boolean isStatusFetched(){
return FLAG_REQUEST1_FETCHED && FLAG_REQUEST2_FETCHED;
}
public ArrayList<status1> getstatus1ArrayList() {
return status1ArrayList;
}
public ArrayList<status2> getstatus2ArrayList() {
return status2ArrayList;
}
}
在上面的代码中,我遇到了竞争条件。由于Volley
网络调用本质上是异步的,因此我无法控制哪个请求将首先完成并显示在Fragment CardView
上。 (即任何rvJoiner.add()
个请求都可以先执行)
我想让我的UI保持一致,即我希望首先将Request1适配器添加到RvJoiner然后再添加到Request2。
如果可能的话,我想移动所有设置适配器的代码,并将它们从JsonObjectRequest连接到我的Fragment的onCreateView方法。所以,通过这种方式,我可以控制适配器的顺序。但是,我需要一种方法,通过FLAG_REQUEST1_FETCHED
方法连续检查FLAG_REQUEST2_FETCHED
和isStatusFetched
的值。
Fragment类的代码将是
public class Fragment1 extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.recylcer_main, container, false);
ParseJSON parseJSON = new ParseJSON(v);
parseJSON.makeRequest1();
parseJSON.makeRequest2();
while(!parseJSON.isDataFetched()){
/* I want to wait here till both status1ArrayList and status2ArrayList gets populated with data in ParseJSON. In this way I can control the order in which adapters are added inside RvJoiner. If I don't wait here I will get NullPointerException on runtime since Volley calls are asynchronous and getStatus1ArrayList/getStatus2ArrayList will most probably return null. But how to wait here without consuming too much CPU power? */
}
ArrayList<status1> status1ArrayList = parseJSON.getstatus1ArrayList();
ArrayList<status2> status2ArrayList = parseJSON.getstatus2ArrayList();
Status1Adapter status1Adapter = new Status1Adapter(status1ArrayList);
Status2Adapter status2Adapter = new Status2Adapter(status2ArrayList);
RecyclerView recList = (RecyclerView) v.findViewById(R.id.cardList);
recList.setLayoutManager(new LinearLayoutManager(v.getContext()));
RvJoiner rvJoiner = new RvJoiner();
/* Problem solved as I'm adding adapters in the order I want */
rvJoiner.add(new JoinableAdapter(status1Adapter));
rvJoiner.add(new JoinableAdapter(status2Adapter));
recList.setAdapter(rvJoiner.getAdapter());
return v;
}
}
一种解决方案可以使用回调。我在某处读到了关于它们的信息,但我不确定它是否能解决我在保持秩序的同时“多次请求”的问题。
另一个解决方案是限制我的Volley Queue一次只处理一个请求,但这会增加获取和提供数据所需的时间。这是我的最后选择。
我几乎没有想法,希望有人帮助我,这样我就可以控制设置适配器的顺序并保持一致的UI。如果您需要任何其他信息,请告诉我。
感谢。
答案 0 :(得分:2)
这就是避免两个请求的竞争条件一般工作的方式。你应该使用回调。 onResponse方法的实现是回调,因为在完成一个请求后会调用这些方法。响应处理适用于UI线程吗?因此,响应可以一个接一个地处理。 这意味着你只需维持那里的秩序。在收到一个回复后,提取您想要做的工作。您需要一些布尔标志来指示您的请求是否已完成。伪代码看起来像这样:
request1Done = false;
request2Done = false;
doRequest1();
doRequest2();
onResponse1() {
doWorkForRequest1(); // always start handling the response
request1Done = true;
if (request2Done) { // if this is true, request2 was faster than request1
doWorkForRequest2();
}
};
onResponse2() {
request2Done = true;
if (request1Done) { // request1 did its work, no its request2's turn
doWorkForRequest2();
}
};
所以基本上你应该修复你的onReponse方法。希望这会帮助你。 :)