如何重新加载Volley数据以从json获取当前项目

时间:2016-11-11 12:51:01

标签: android rest android-recyclerview android-volley swiperefreshlayout

我有这个rec​​yclelerview,它通过json api从服务器加载数据。服务器上的数据将经常更新,所以我有一个刷卡刷新"在我的申请中。我使用volley从服务器获取数据,我想以这样的方式制作,当用户滑动刷新时,volley将从服务器重新加载json数据,以便recyclerview可以显示新添加的项目

这是我的onSwipeListener

swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
        @Override
        public void onRefresh() {
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        adapter.notifyDataSetChanged();
                        swipeRefreshLayout.setRefreshing(false);
                    }
                }, 3000);
        }
    });

这就是齐射请求数据的方式

StringRequest stringRequest = new StringRequest(Request.Method.GET, url,
            new Response.Listener<String>() {

                public ArrayList<Deal> userlist;
                public DealAdapter adapter;

                @Override
                public void onResponse(String response) {
                    MainActivity.userList = this.userlist;
                    userList = new JsonConverter<Deal>().
                            toArrayList(response, Deal.class);
                    Collections.reverse(userList);

                    adapter = new DealAdapter(getApplicationContext(), userList);
                    MainActivity.adapter= this.adapter;
                    recyclerView.setAdapter(adapter);

                }

但是我无法弄清楚在调用onSwipeListener时如何刷新凌空响应数据。

这是我如何将请求添加到队列

        MySingleton.getInstance(getApplicationContext()).addToRequestQueue(stringRequest);

2 个答案:

答案 0 :(得分:0)

我认为问题在于您正在重新分配Activity适配器,但从未将新适配器设置回Activity的RecyclerView。

当然,你有recyclerView.setAdapter(adapter),但不清楚该引用的来源。

同样,Activity的userList为空。

    MainActivity.userList = this.userlist; // This is null
    userList = new JsonConverter<Deal>().     // This should have been first
            toArrayList(response, Deal.class);

所以,无论如何,正如我在评论中所说的那样,静态变量实际上并不是“在Java类墙上投放数据”的好方法。

假设你有这个班来处理所有的Volley电话。请注意,Response.Listener接口作为参数传递。

public class JsonApi {

    private Context mContext;
    private static JsonApi instance;

    private JsonApi(Context c) { this.mContext = c; }

    public static JsonApi getInstance(Context context) {
        if (instance == null) instance = new JsonApi(context);
        return instance;
    }

    public void getSomeData(Response.Listener<String> success) {

        StringRequest stringRequest = new StringRequest(Request.Method.GET, url, success);
        MySingleton.getInstance(this.mContext).addToRequestQueue(stringRequest);

    }
}

现在,在Activity或Adapter中,您可以获得此类并在任何时候重新执行Volley请求。

(对于任何拼写错误道歉。这是未经测试的,但它只是给出了如何布局代码的示例)。

public class MainActivity implements 
      SwipeRefreshLayout.OnRefreshListener, 
      Response.Listener<String> {

    private List<Deal> userList;
    private DealAdapter adapter;
    private RecyclerView recyclerView;
    private SwipeRefreshLayout swipeRefreshLayout;

    private JsonApi api;

    @Override
    public void onResponse(String response) {
        userList.clear();
        userList.addAll(new JsonConverter<Deal>().
                toArrayList(response, Deal.class));
        Collections.reverse(userList);

        // Handle adapter updates here
        if (swipeRefreshLayout.isRefreshing()) {
            swipeRefreshLayout.setRefreshing(false);
        }

        recyclerView.notifyDataSetChanged();
    }

    @Override
    public void onRefresh() {
            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    // Start an API call to refresh data
                    api.getSomeData(MainActivity.this); 
                }
            }, 3000);
    }

    @Override
    protected void onCreate(Bundle b) {
        super.onCreate(b);
        setContentView(R.layout.activity_main);

        api = JsonApi.getInstance(MainActivity.this);

        userList = new ArrayList<Deal>();
        adapter = new DealAdapter(MainActivity.this, userList);

        recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
        // set layout manager ... etc.
        recyclerView.setAdapter(adapter);

        api.getSomeData(MainActivity.this); // start the first call when the activity starts

        swipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.swipeRefresh);
        swipeRefreshLayout.setOnRefreshListener(MainActivity.this);
    }

}

答案 1 :(得分:0)

好的,这对我有用。我不得不在OnRefreshListener中再次使用new变量调用StringRequest,以便当用户滑动时,volley再次请求json数据并将其传递给适配器,如下所示。

swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
        @Override
        public void onRefresh() {
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        StringRequest mrequest = new StringRequest(Request.Method.GET, url,
                                new Response.Listener<String>() {

                                    public ArrayList<Deal> userlist;
                                    public DealAdapter adapter;

                                    @Override
                                    public void onResponse(String response) {
                                        MainActivity.userList = this.userlist;
                                        userList = new JsonConverter<Deal>().
                                                toArrayList(response, Deal.class);
                                        Collections.reverse(userList);

                                        adapter = new DealAdapter(getApplicationContext(), userList);
                                        MainActivity.adapter= this.adapter;
                                        recyclerView.setAdapter(adapter);

                                    }
                                },
                                new Response.ErrorListener() {
                                    @Override
                                    public void onErrorResponse(VolleyError error) {
                                        Log.d(TAG, error.toString());
                                        Toast.makeText(getApplicationContext(), "Something Went wrong: " + "'" + error + "'", Toast.LENGTH_LONG).show();

                                    }
                                });

                        MySingleton.getInstance(getApplicationContext()).addToRequestQueue(mrequest);
                        adapter.notifyDataSetChanged();
                        swipeRefreshLayout.setRefreshing(false);

                    }
                }, 3000);
        }
    });