如何使用Android的Volley HTTP GET呼叫响应减少延迟

时间:2019-09-12 21:37:43

标签: java android arraylist android-recyclerview android-volley

使用Volley,我能够对我想点击的API进行异步HTTP GET调用。但是,获取数据并在UI中使用Recycler View的交错网格布局 花费的时间为 15到20秒

对于我进行的每个搜索调用,大约要花费很多时间,这是很糟糕的时间。如何减少此延迟?我还添加了RequestQueue,但没有运气。

在进行搜索调用后,我正在初始化适配器。在那做是一个好方法吗?还是建议您在活动的onCreate方法中执行此操作。谁能指导导致这种延迟的原因是什么?

这是代码的摘录

public class StaggeredSearchActivity extends AppCompatActivity {

    private RecyclerView recyclerView;
    private StaggeredGridLayoutManager staggeredGridLayoutManager;
    private StaggeredGridAdapter staggeredGridAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_staggered_search);

        Intent intent = getIntent();
        String searchText = intent.getStringExtra("searchText");
        getSearchData(searchText);

        recyclerView = findViewById(R.id.staggered_recycler_view);
        staggeredGridLayoutManager = new StaggeredGridLayoutManager(2, LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(staggeredGridLayoutManager);
    }

    private void getSearchData(String searchText) {
        RequestQueue requestQueue = Volley.newRequestQueue(getApplicationContext());

        String url = "https://my-json-server.typicode.com/typicode/demo/comments";

        JsonObjectRequest jsonObjectRequest = new JsonObjectRequest
                (Request.Method.GET, url, null, new Response.Listener<JSONObject>() {
                    ArrayList<StaggeredCustomCard> dataset = new ArrayList<>();

                    @Override
                    public void onResponse(JSONObject response) {
                        try {
                              JSONArray array = response.getJSONArray("tweets");
                            for (int i = 0; i < array.length(); i++) {
                                JSONObject jsonObject = array.getJSONObject(i);
                                String body = jsonObject.getString("body");
                                String postId = jsonObject.getString("postId");
                                dataset.add(new StaggeredCustomCard(body, postId);
                            }
                            staggeredGridAdapter = new StaggeredGridAdapter(StaggeredSearchActivity.this, dataset);
                            recyclerView.setAdapter(staggeredGridAdapter);

                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }

                }, new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        // TODO: Handle error
                        ...
                    }
                });

        jsonObjectRequest.setRetryPolicy(new DefaultRetryPolicy(50000, 5, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
        requestQueue.add(jsonObjectRequest);
    }

}

0 个答案:

没有答案