我从json url读取数据时出现问题,链接工作正常,我添加了getURL
方法的日志,当我点击Monitor的链接时,数据就是浏览器准确.. !!但反应不是......!
我想从请求中过滤已完成的订单,它会使用此代码正常过滤,但响应数据未更新..!例如,如果我从后端更改状态或为此用户添加了新订单,则新更改不会显示..!链接本身有正确的数据,但链接的响应不是,获取新的正确数据的唯一方法是,如果我手动清除应用程序数据,然后再次登录...只有这时响应才会更新..!?< / p>
以下代码可能无法帮助您,因为我怀疑问题来自他们......!
任何想法......!?
OrderRepository类,它有一个发出请求的方法
public static void fetchOrders(final Context context, final DataFetch fetcher) {
final Customer customer;
customer = Hawk.get("user");
int userID = customer.getID();
final RequestQueue requestQueue;
requestQueue = Volley.newRequestQueue(context);
final JsonObjectRequest jsObjRequest = new JsonObjectRequest(Request.Method.GET, getURL(FETCH_ORDERS_URI, userID), null,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
Gson gson = new Gson();
Type listType = new TypeToken<List<OrderPresenter>>() {}.getType();
try {
List<OrderPresenter> TheOrders;
TheOrders = gson.fromJson(response.getJSONArray("orders").toString(), listType);
ArrayList<OrderPresenter> result = new ArrayList<>();
for (OrderPresenter record : TheOrders) {
Log.i("Statuses_Gson1", record.getStatus());
while (record.getStatus().equals("completed")) {
result.add(record);
fetcher.onResponse(result);
break;
}
}
}else {
Toasty.error(context, context.getResources().getString(R.string.error_noOrders), Toast.LENGTH_SHORT).show();
}
}
}
} catch (JSONException e) {
fetcher.onErrorResponse(e);
Toast.makeText(context, "catched", Toast.LENGTH_SHORT).show();
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
fetcher.onErrorResponse(error);
}
});
requestQueue.add(jsObjRequest);
}
DataFeach Calss(这将是一个帮助类)
abstract public class DataFetch {
public abstract void onResponse(ArrayList<OrderPresenter> response);
public void onErrorResponse(Exception exception){
};
}
我的片段类,其中包含recyclerview
.....
...
.
List<OrderPresenter> modelData = new ArrayList<>();
RecyclerView recyclerView = (RecyclerView) rootView.findViewById(R.id.recyclerView);
recyclerView.setLayoutManager(new LinearLayoutManager(this.getActivity()));
OrderRepository.fetchOrders(getActivity(), new DataFetch() {
@Override
public void onResponse(ArrayList<OrderPresenter> data) {
OrdersDataAdapter adapter = new OrdersDataAdapter(data, getActivity());
recyclerView.setAdapter(adapter);
adapter.setMode(Attributes.Mode.Single);
adapter.notifyDataSetChanged();
// recyclerView.invalidate();
}
});
....
...
.
-----更新------
似乎在发出新请求时,缓存的数据不会被清除..!所以我用这行=&gt;
解决了这个问题 requestQueue.getCache().clear();
答案 0 :(得分:1)
好吧,你可以制作一个每1分钟或类似的请求计时器
并且不要忘记在每个请求中更新适配器adapter.notifyDataSetChanged
<强>更新强>
为了避免太多要求, 有一些叫GCM这是一个谷歌云消息,我有旧数据谷歌云消息已经用firebase实现,好吧,使用GCM然后在你的Android应用程序使用BroadCast接收器,以处理来自服务器的消息< / p>
答案 1 :(得分:1)
我不认为你的过滤器是正确的......你只能抓住一个项目
final Customer customer = Hawk.get("user");
int userID = customer.getID();
final Gson gson = new Gson();
final Type listType = new TypeToken<List<OrderPresenter>>() {}.getType();
final RequestQueue requestQueue =
Volley.newRequestQueue(context);
final JsonObjectRequest jsObjRequest = new JsonObjectRequest(Request.Method.GET, getURL(FETCH_ORDERS_URI, userID), null,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
List<OrderPresenter> orders = gson.fromJson(response.getJSONArray("orders").toString(), listType);
ArrayList<OrderPresenter> result = new ArrayList<>();
for (OrderPresenter record : orders) {
Log.i("Statuses_Gson1", record.getStatus());
if (record.getStatus().equals("completed")) {
result.add(record);
}
}
fetcher.onResponse(result); // this is after the for loop
你的while循环是没有意义的,你似乎有一些随机的其他声明,没有if
您也不应该在每个响应上创建新的适配器。
final List<OrderPresenter> modelData = new ArrayList<>();
final RecyclerView recyclerView = (RecyclerView) rootView.findViewById(R.id.recyclerView);
recyclerView.setLayoutManager(new LinearLayoutManager(this.getActivity()));
OrdersDataAdapter adapter = new OrdersDataAdapter(modelData, getActivity());
adapter.setMode(Attributes.Mode.Single);
recyclerView.setAdapter(adapter);
OrderRepository.fetchOrders(getActivity(), new DataFetch() {
@Override
public void onResponse(ArrayList<OrderPresenter> data) {
modelData.clear();
modelData.addAll(data);
adapter.notifyDataSetChanged();
// recyclerView.invalidate();
}
});